Microsoft Visual C++ .NET 2003 Kick Start

Using a COM Component from Managed C++

Managed code can use a COM component just as though it was a .NET object. It does so through a special piece of code called a Runtime-Callable Wrapper, or RCW. This wrapper (also called an interop assembly ) accepts .NET calls through the runtime, and inside it holds all it needs to know about your COM component. When the calls come in, the wrapper calls methods of the COM component to handle them. It marshals parameters if need be, and translates error HRESULT values from the COM component into .NET exceptions. You don't need to do any work to get all this translated for you.

How do you get a Runtime-Callable Wrapper? There are two ways, and they're both very easy: either someone gives you one, or Visual Studio can generate it for you. The vendor that supplied your COM component may ship a Runtime-Callable Wrapper with it. In this case the interop assembly is known as a Primary Interop Assembly, or PIA. Primary refers to the fact that it was written by the supplier of the COM component, and therefore may take advantage of the internal mechanisms of the COM Component. Many of the Microsoft COM components , including large parts of BizTalk 2002 and Office 2003, ship with PIAs. If nobody gives you an RCW, Visual Studio can make one for you, and that's the approach used in this chapter.

To test using a COM component from managed code, create a C++ WinForms application called ManagedPhoneTest . Drag controls onto the form and edit their properties, as follows :

  1. A textbox. Name it Number and change the Text property to no text.

  2. A label. Name it Error and change the Text property to no text.

  3. Another label. Name it Message and change the Text property to no text.

  4. A button. Name it ValidateButton and change its Text to Validate .

  5. Another button. Name it CloseButton and change its Text to Close .

Double-click the Close button and add this line of code to the handler that is generated for you:

Close();

Before you can code the handler for the Validate button, your code needs a reference to the Runtime-Callable Wrapper for the COM component. Assuming you are still using the same computer as you used to develop that component, you don't need to copy it anywhere or do anything to be ready to use it. It's registered on your machine, and COM will use the Registry to find it. To generate the RCW and add a reference to it, right-click the References node in Solution Explorer and choose Add Reference. On the Add Reference dialog box, select the COM tab. After a small delay, all the COM components on your system are listed. Scroll down to PhoneFormat 1.0 Type Library, highlight it, click Select and then OK.

Double-click the Validate button on the dialog box and then enter this code for the handler:

try { Interop::PhoneFormat::CPhoneNumberClass* pf = new Interop::PhoneFormat::CPhoneNumberClass(); String* errormessage; unsigned char errorcode; pf->ValidatePhoneNumber(Number->Text, &errorcode, &errormessage); Error->Text = errorcode.ToString(); Message->Text = errormessage; } catch (Exception* e) { Error->Text = "99"; Message->Text = e->Message; }

Run the application and test with some good and bad phone numbers as before. Everything should work smoothly.

FINDING THE CLASS NAME

How do you know what class to create? If you double-click PhoneFormat under the References node in Solution Explorer, the Object Browser opens to show you the contents of the reference assembly. Expand Interop.PhoneFormat and you'll see a namespace (identified with brace brackets) called Interop.PhoneFormat . Expand that namespace and you'll see a class called CPhoneNumber and an interface called IPhoneNumber .

Click on CPhoneNumber in the object browser, and you'll see the Associated COM Class is Interop.PhoneFormat.CPhoneNumberClass , and it has one method, ValidatePhoneNumber() . Writing the line of code is just a matter of replacing the dots (which VB and C# use) with :: for C++.

Now compare the amount of code you had to write to use this COM component from a WinForms application to the amount of code required to use it from an MFC application, even with the #import macro simplifying the MFC application considerably from what it otherwise would have been. Building applications on the .NET Framework is fast and simple, and those applications have full access to all the COM components you've already built.

Категории