When a COM client calls a DotNet object, the DotNet framework will create a COM callable wrapper (CCW). COM clients use the CCW as a proxy for the managed object.
Their primary purpose of CCW is to marshal calls between managed and unmanaged code and also to manage the object identity and object lifetime of the managed objects they wrap.
The primary goal of this article is to create a demo project to show how to call a DotNet component from COM client and to implement events raised by the DotNet component.
The example uses a DotNet component and COM client extending the functionality of the DotNet component using delegation.
This is a follow on article and we will be using the same demo project the only difference would be the client now is COM using DotNet component.
Step 1
Lets start by creating a Strong Name for the DotNet component we would be creating in next steps. Strong name is a unique name created by hashing a 128-bit encryption key against the name of the Assembly (ComInterOp in our case). The strong name is created using SN.exe, that would create ComInterOp.snk file, which we would use while creating the DotNet Assembly.
The command line instruction to create a strong name sn k ComInterOp.snk
Step 2
Now lets create a DotNet Assembly. Our assembly has only one class CEmp that exposes properties (FirstName, LastName, DOB) and raises an event (Senior). Raising an event from DotNet Component and consuming it in COM component is done by defining event sink interface in managed code and then apply the ComSourceInterfacesAttribute to connect the event sink interface to the managed class.
The command line instruction to create an assembly using the strong name (for vbc switch info see MSDN) vbc /out:ComInterOp.dll /t:library /keyfile:ComInterOp.snk CEmp.vb
VB.Net CEmp class code
Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface evtSenior
Sub Senior()
End Interface
<ComSourceInterfacesAttribute("evtSenior")> _
Public Class CEmp
Private mstrFirstName As String
Private mstrLastName As String
Private mdtDOB As Date
Public Event Senior()
Public Property FirstName() As String
Get
FirstName = mstrFirstName
End Get
Set(ByVal Value As String)
mstrFirstName = Value
End Set
End Property
Public Property LastName() As String
Get
LastName = mstrLastName
End Get
Set(ByVal Value As String)
mstrLastName = Value
End Set
End Property
Public Property DOB() As Date
Get
DOB = mdtDOB
End Get
Set(ByVal Value As Date)
mdtDOB = Value
If DateDiff(DateInterval.Year, Value, Now) > 60 Then
RaiseEvent Senior()
End If
End Set
End Property
End Class
Step 3
Once the assembly is created we have to create a Type library so the COM client can use the Assembly seamlessly. We can package an assembly for COM by following methods.
Type Library Exporter
Using the Type Library Exporter (Tlbexp.exe) the classes and interfaces contained in an assembly are converted into to a COM type library. Once the TypeLib is created COM clients can create an instance of the .NET class and call the methods of that object, just as if it were a COM object.
TypeLibConverter Class
The TypeLibConverter Class of the System.Runtime.InteropServices namespace provides methods to convert an assembly to a TypeLib. This API produces same output as Tlbexp.exe
Upendra Pratap ShahiPosted Jun 26, 2015, 5:38 AM
good one