Using a COM Component from a .NET Client C# Help

To see how a .NET application can use a COM component, you first have create a COM component, Creating COM components is not possible with C# or Visual Basic 2005 you need either visual Basic 6.0 or C++ (or any other language that supports COM). This chapter uses the Active Template library (A’TL) and C++, A short note about building COM components with Visual Basic 9.0 and C#: With Visual ‘Basic 9.0 and C# it is possible to build .NET components that can be used as COM objects by using a wrapper that is the real COM component, It would make no sense for a .NET component that is wrapped from a component to be used by a .NET client with a COM interop, Because this is not a COM book,  does not discuss all aspects of the code but only what ‘you need to build the sample.

CreatIng a COM Component

To create a COM component with ATL and C++, create a new ATL Project, You can find the AIL Project Wizard within the Visual C++ Projects group when you select File→New→ Project, Set the name to COMServer.

With the Application Settings, select Dynamic Link Library and click finish, The ATLProject Wizard creates the foundation for the server.

ACOM object is still needed, Add a class in Solution Explorer and select ATLSimple Object, In the dialog that starts up, enter COMDemo in the field for the Short name, The other fields will be filled automatically, but change the interface name to IWelcome, Click Finish to create the stub code for the class and the interface.

Figure 24-7

Figure 24-7

The COM component offers two interfaces, so that you can see how Querylnterface () is mapped from .NET,and just three simple methods, so that you can see how the interaction takes place, In class view, select the interface IWelcome and add the method Greeting ()  with these parameters: HRESULT Greeting () with these parameters:

HRESULT Greeting([in] BSTR name, [out, retval BSTR* message);

Figure 24-8

Figure 24-8

The IDL file COMDemo.idl defines the interface of COM. Your wizard-generated code from COMDemo.idl should look similar to the following code. The unique identifiers (uui’ds) will interface IWelcome defines the Greeting () method. The brackets before the keyword _interface some attributes for the interface.

uuid defines the interface 10 and dual marks the type of interface:

The IDL file also defines the content of the type library, which is the COM object [coc l.as s] that implements the interface IWelcome:

With custom attributes, it is possible to change the name of the class and interfaces that are generated by a .NET wrapper class, You just have to add the attribute custom with the identifier OF21F3 59-ABB4-41eB-9A7B-36DllOE6D2F9, and the name under which it should appear within .NET, Add the custom attribute with the same identifier and the name csharpaid.COMInterop, Server  IWelcome to the header section of the IWelcome interface, Add the same attribute with a corresponding name to the class CCOMDemo:

Now add a second interface to the file COMDemo.

idl.

You can copy the header section of the Iweicorre interface to the header section of the new IMath interface, but be sure to change the unique identifier that is defined with the uuid keyword, You can generate such an ID with the guidgen utility.

The interface IMath offers the methods Add () and Sub ():

The coclass COMDemo must also be changed so that it implements both the interfaces IWelcome and IMath. The IWelcome interface is the default interface: ‘

Now add a second interface to the file COMDemo.

idl.

You can copy the header section of the IWelcome, Interface to the header section of the new 1Math interface, but be sure to change the unique identifier that is defined with the uuid keyword, You can generate such an ID with the guidgen utility.

The interface IMath offers the methods Add () and Sub ():

The coclass COMDemo must also be changed so that it implements both the interfaces IWelcome and IMath, The IWelcome interface is the default interface:

Now, you can set the focus away from the IDL file toward the C++ code, In the file COMDemo.h, you can find the class definition of the COM object, The class CCOMDemouses multiple inheritances to derive from the template classes CComObjectRootEx, CComCoClass, and IDisplatchlmpl. CComObjectRootEx offers an implementation of the !Unknown interface functionality such as AddRef and Release, CComCoClass creates a factory that instantiates objects of the template argument, which here is CCornDerno,and IDispatchlmpl offers an implementation of the methods from the IDispatch interface.

With the macros that are surrounded by BEGIN_COM_MAPand END_COMJIAP,a map is created to define all the COM interfaces that are implemented by the COM class.

This map is used by the implementation of the Querylnterface method.

With this class definition, you have to add the second interface, lMath, as well as the methods that are defined with the IMath interface:

Now, you can implement the three methods in the file COMDemo .cpp with the following code, The CComBSTR is an ATLclass that makes it easier to deal with BSTRS. In the Greeting () method, only a welcome message is returned, which adds the name passed in the first argument to the message that returned.

The Add () method just does a simple addition of two values, and the Sub () method does a subtraction and returns the result:

Now.

you can build the component.

The build process also configures the component in the registry.

Posted on October 27, 2015 in Interoperability

Share the Story

Back to Top
Share This