Creating a Runtime Callable Wrapper C# Help

You can now use the COM component from within .NET.

To make this possible, you must create a runtime callable wrapper (RCW).

Using the RCW,the .NET client sees a .NET object instead of the COM component; there is no need to deal with the COM characteristics because this is done by the wrapper.

An RCW hides the IUnknown and IDispatch interfaces and deals itself with the reference counts of the COM object.

Figure 24-9

Figure 24-9

IUnknown
-i
I IDispatch
.NET IMath i /.Ii, .• COM
RCW . IWelcpmel •
, J
: IWelcome I· I
Client IMath Object ,

The RCW can be created by using the command-line utility tlbimp or by using Visual Studio. Starting the command

bimp COMServer.dll lout:lnterop.COMServer.dll

creates the file Interop.COMServer.dll

that contains a .NET assembly with the wrapper class. In this generated assembly, you can find the namespace COMWrapper with the class CCOMDemoClass and the interfaces CCOMDemo, IMath, and IWelcome The name of the namespace can be changed by using options of the tlbimp utility.

The option Inamespace allows you to specify a different namespace, and with I asmversion you can define the version number of the assembly.

Another important option of this command-line utility is /keyfile, which is used for assigning a strong name to the generated assembly.

An RCW can also be created by using Visual Studio.

To create a simple sample application, create a C’ console project.

In Solution Explorer, add a reference,to the COM server by selecting the COM tab in the Add Reference dialog, and scroll down to the entry COMServer 1.0 Type Library.

If the COM object should be released before the garbage collector cleans up the object, the static method Marshal.

ReleaseComObject () invokes the Release () method of the component, so that the
component can destroy itself and free up memory:

Marshal.ReleaseComObject(math);

Earlier you learned that the COM object is released as soon as the reference count is O. Marshal, ReleaseComObject () decrements the reference count by 1 by,invoking the Release () method, Because the RCW does just one call toAddRef () to increment the reference count, a single call to Marshal, ReleaseComObject () is enough to release the object no matter how many references to the RCW you keep.

After releasing the COM object using Marshal. ReleaseComObj ect ( ), you may not use any variable that references the object, In the example, the COM object is released by using the variable math.

The variable welcome, which references the same object, cannot be used after releasing the object. Otherwise,you will get an exception of type InvalidComObjectException.

Releasing COM objects when they are no longer needed is extremely important, COM objects make use of the native memory heap, whereas .NETobjeds make use of the managed memory heap.

The garbage ‘collector only deals with managed memory, As you can see, with a runtime callable wrapper, a COM component can be used similarly to a .NETobject.

A special case of a runtime callable wrapper is a primary interop assembly, which is discussed next.

Posted on October 27, 2015 in Interoperability

Share the Story

Back to Top
Share This