Calling a .NET assembly from COM by placing the K2Rom.dll in a COM wrapper

  • 24 February 2022
  • 0 replies
  • 25 views

Userlevel 3
Badge +8
 

Calling a .NET assembly from COM by placing the K2Rom.dll in a COM wrapper

LEGACY/ARCHIVED CONTENT
This article has been archived, and/or refers to legacy products, components or features. The content in this article is offered "as is" and will no longer be updated. Archived content is provided for reference purposes only. This content does not infer that the product, component or feature is supported, or that the product, component or feature will continue to function as described herein.

This document describes how to place the K2Rom.dll in a COM wrapper and then to call the DLL from a COM based application written in VB6.

 

Introduction 

This article describes how to create a .NET assembly containing a public class which can be called from by a COM application:

 
  1. Open Microsoft Visual Studio .NET and click New Project on the Start Page
  2. Select Visual Basic Projects / Visual C# Projects from the tree view on the left-hand side of the screen
  3. Select Class Library as the project template
  4. Set the name of the application to NetK2Rom and click OK to create the project
  5. Highlight the class called Class1.vb in the Solution Explorer window and rename it to NetK2Rom.vb / NetK2Rom.cs
  6. Add a reference to the K2ROM assembly (in the Solution Explorer window, right click on References, select Add Reference, and browse to K2ROM.dll, located in the bin folder in the K2.net 2003 installation folder)
  7. Select the code for Class1 in the NetK2Rom.vb / NetK2Rom.cs file (this will be an empty class definition) and replace it with the following code:

Visual Basic Code Sample:

 

Imports SourceCode
Public Interface iK2ROM
   Function CreateProcess() As Boolean
   Property Project() As String
   Property Process() As String
   Property Server() As String
   Property Folio() As String
   Property Data() As String
   Property DataFieldsCommaDelStrNames() As String
   Property DataFieldsCommaDelStrValues() As String
End Interface

Public Class NetK2Rom
   Implements iK2ROM
   'Properties
   Private m_Server As String
   Private m_Project As String
   Private m_Process As String
   Private m_Folio As String
   Private m_Data As String
   Private m_DataNames As String
   Private m_DataValues As String

Public Class NetK2Rom
Implements iK2ROM
'Properties
Private m_Server As String
Private m_Project As String Private
m_Process As String Private
m_Folio As String Private
m_Data As String Private
m_DataNames As String Private
m_DataValues As String Public Function
CreateProcess() As Boolean Implements iK2ROM.CreateProcess
   Dim DataFieldNames As Array
   DataFieldNames = Split(DataFieldsCommaDelStrNames, ",")
   Dim DataFieldValues As Array
   DataFieldValues = Split(DataFieldsCommaDelStrValues, ",")
   Dim cn As New K2ROM.Connection
   Dim PInstance As K2ROM.ProcessInstance
   Dim WLItem As K2ROM.WorklistItem
   Dim WList As K2ROM.Worklist
   Dim i As Integer
   Dim bReturn As Boolean = True
   Try
   'Start New Process Instance
    cn.Open(Server)
   PInstance = cn.CreateProcessInstance(Project & """ & Process)
   'Data Fields Array
   For i = 0 To UBound(DataFieldNames)
    PInstance.DataFields(DataFieldNames(i)).Value = DataFieldValues(i)
   Next
   If Trim(m_Folio) = "" Then
   PInstance.Folio = Now
 Else
   PInstance.Folio = m_Folio
 End If
   cn.StartProcessInstance(PInstance, True)
'Get Process Instance ID
Dim PIID As String = PInstance.ID

'Open Worklist
WList = cn.OpenWorklist("ASP")
For Each WLItem In WList
   Dim pid As String = WLItem.ProcessInstance.ID.ToString
 If pid = PIID Then
   WLItem.Open()
   Data = WLItem.Data
   Exit For
 End If
 Next
  Catch ex As Exception
  bReturn = False
  End Try
Return bReturn

End Function

Public Property Data() As String Implements iK2ROM.Data
 Get
   Data = m_Data
  End Get
 Set(ByVal Value As String)
   m_Data = Value
 End Set
End Property

Public Property Folio() As String Implements iK2ROM.Folio
 Get
   Folio = m_Folio
 End Get
 Set(ByVal Value As String)
   m_Folio = Value
 End Set
End Property

Public Property Process() As String Implements iK2ROM.Process
Get
  Process = m_Process
End Get
Set(ByVal Value As String)
 m_Process = Value
 End Set
End Property

Public Property Project() As String Implements iK2ROM.Project
 Get
  Project = m_Project
 End Get
 Set(ByVal Value As String)
  m_Project = Value
 End Set
End Property

Public Property Server() As String Implements iK2ROM.Server
 Get
  Server = m_Server
 End Get
 Set(ByVal Value As String)
  m_Server = Value
 End Set
End Property

Public Property DataFieldsCommaDelStrNames() As String Implements iK2ROM.DataFieldsCommaDelStrNames
 Get
  DataFieldsCommaDelStrNames = m_DataNames
 End Get
 Set(ByVal Value As String)
  m_DataNames = Value
 End Set
End Property

Public Property DataFieldsCommaDelStrValues() As String Implements iK2ROM.DataFieldsCommaDelStrValues
  Get
  DataFieldsCommaDelStrValues = m_DataValues
 End Get
 Set(ByVal Value As String)
  m_DataValues = Value
 End Set
End Property
End Class
 

 

C# Code Sample:

 

using System;
interface iK2Rom
{ bool CreateProcess(); string Project
 {
   get;
   set;
 }
 string Process
 {
   get;
   set;
 }
 string Server
 {
   get;
   set;
 }
 string Folio
 {
   get;
   set;
 }
 string Data
 {
   get;
   set;
 }
 string DataFieldsCommaDelStrNames
 {
   get;
   set;
 }
 string DataFieldsCommaDelStrValues
 {
   get;
   set;
 }
}
namespace NetK2Rom
{
  public class NetK2Rom:iK2Rom
 {
   private string m_Server;
   private string m_Project;
   private string m_Process;
   private string m_Folio;
   private string m_Data;
   private string m_DataNames;
   private string m_DataValues;

   #region iK2Rom Members
    public bool CreateProcess()
       {

       string delimStr = ",";
       char [] delimiter = delimStr.ToCharArray();
       string [] DataFieldNames = null;
       DataFieldNames = DataFieldsCommaDelStrNames.Split(delimiter);
       string [] DataFieldValues = null;
       DataFieldValues = DataFieldsCommaDelStrValues.Split(delimiter);
       SourceCode.K2ROM.Connection cn = new SourceCode.K2ROM.Connection();
       SourceCode.K2ROM.ProcessInstance PInstance;
       SourceCode.K2ROM.Worklist WList;
       bool bReturn = true;

       try
       {
           //Start New Process Instance
           cn.Open(Server);
           PInstance = cn.CreateProcessInstance(Project + @""" + Process);
           //Data Fields Array
           for (int i = 0; i <= DataFieldNames.GetUpperBound(0) - 1; i++)
           {
           PInstance.DataFields[DataFieldNames[i]].Value = DataFieldValues[i];
           }
           if (Folio.Trim() == "")
           {
           PInstance.Folio = DateTime.Now.ToString();
           }
           else
            {
            PInstance.Folio = Folio;
            }
           
           cn.StartProcessInstance(PInstance, true);
           //Get Process Instance ID
           string PIID = PInstance.ID.ToString();
           //Open Worklist
           WList = cn.OpenWorklist("ASP");
           foreach (SourceCode.K2ROM.WorklistItem WLItem in WList)
           {
           string pid = WLItem.ProcessInstance.ID.ToString();
           if (pid == PIID)
           {
           WLItem.Open();
           Data = WLItem.Data;
           break;
           }
       }
       }
       catch (Exception ex)
       {
       bReturn = false;
       }
       return bReturn;
       }

    public string Project
    {
      get
    {
      return m_Data;
    }
      set
    {
      m_Data = value;
    }
    }
 
    public string Process
    {
    get
    {
     &nbspreturn m_Process;
    }
    set
    {
     &nbspm_Process = value;
    }
    }
    public string Server
    {
    get
    {
     &nbspreturnreturn m_Server;
    }
    set
    {
     &nbspreturn m_Server = value;
    }
    }

    public string Folio
    {
    get
    {
     &nbspreturnreturn m_Folio;
    }
    set
    {
     &nbspreturnm_Folio = value;
    }
    }
 
    public string Data
    {
    get
    {
     &nbspreturnreturn return m_Data;
    }
    set
    {
      m_Data = value;
    }
    }

    public string DataFieldsCommaDelStrNames
    {
    get
    {
      return m_DataNames;
    }
    set
    {
      m_DataNames = value;
    }
    }
 
    public string DataFieldsCommaDelStrValues
    {
    get
    {
      return m_DataValues;
    }
    set
    {
      m_DataValues = value;
    }
    }

   #endregion
  }
 }
 

 

  1. Create a strong name key file for this assembly. To do this, open the Microsoft Visual Studio .NET 2003 Command Prompt. (Click Start, then Programs, then Microsoft Visual Studio .NET 2003, then Visual Studio .NET Tools, and then Visual Studio .NET Command Prompt). Navigate to the bin folder of the NetK2Rom project.
  2. Type in the following in the Microsoft Visual Studio .NET Command Prompt: sn -k NetK2Rom.snk
  3. In the Solution Explorer in Visual Studio .NET, double-click the AssemblyInfo.vb / AssemblyInfo.cs file to open it in the editing window.
  4. In the Assembly Attributes section at the top of this file, modify the AssemblyTitle and AssemblyDescription lines to read:

Visual Basic

 

<Assembly: AssemblyTitle("NetK2Rom")>
<Assembly: AssemblyDescription(".Net to COM K2Rom")>
 

 

C#

 

[assembly: AssemblyTitle("NetK2Rom ")]
[assembly: AssemblyDescription("(".Net to COM K2Rom ")]
 

 

  1. Add the following line to the AssemblyInfo file:

Visual Basic

 

<Assembly: AssemblyKeyFile("#FullPathToBin#NetK2Rom.snk")>
 

 

C#

 

[assembly: AssemblyKeyFile("#FullPathToBin#NetK2Rom.snk")]
 

 

Note: #FullPathToBin# must be replaced with the path to the bin folder contained in the project directory of the NetK2Rom project where the newly created NetK2Rom.snk file resides.
  1. Build the assembly either by selecting "Build > Build Solution" from the menu, or by hitting Ctrl+Shift+B.
  2. The assembly must be registered and a Type Library must be created. In the Microsoft Visual Studio v.NET Command Prompt (that should still be open and in the bin directory of the NetK2Rom project) type the following: regasm /tlb:NetK2Rom.tlb NetK2Rom.dll
  3. The assembly and the K2ROM.dll must be added to the Global Assembly Cache (GAC). Again, in the Microsoft Visual Studio .NET Command Prompt execute the following two commands:

    gacutil /I NetK2Rom.dll
    gacutil /I K2ROM.dll

  4. The assembly will now be callable from COM applications.

 


0 replies

Be the first to reply!

Reply