15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

702 ❘ ChaPTer 26 interOp<br />

using a Com ComPonenT from a .neT ClienT<br />

To see how a .<strong>NET</strong> application can use a COM component, you fi rst have to create a COM component.<br />

Creating COM components is not possible with <strong>C#</strong> or Visual Basic 2010; you need either Visual Basic 6.0<br />

or C++ (or any other language that supports COM). This chapter uses the Active Template Library (ATL)<br />

<strong>and</strong> C++ with Visual Studio 2010.<br />

Here we will start creating a simple COM component <strong>and</strong> use this from a runtime callable wrapper (RCW).<br />

We will also use the component with the new <strong>C#</strong> 4 dynamic language extensions. Threading issues are<br />

discussed, <strong>and</strong> fi nally COM connection points are mapped to .<strong>NET</strong> events.<br />

A short note about building COM components with Visual Basic 9.0 <strong>and</strong> <strong>C#</strong>: With<br />

Visual Basic 10.0 <strong>and</strong> <strong>C#</strong> it is possible to build .<strong>NET</strong> components that can be used as<br />

COM objects by using a wrapper that is the real COM component. It would make no<br />

sense for a .<strong>NET</strong> component that is wrapped from a COM component to be used by a<br />

.<strong>NET</strong> client with COM interop.<br />

Because this is not a COM book, it does not discuss all aspects of the code but only<br />

what you need to build the sample.<br />

Creating a Com Component<br />

To create a COM component with ATL <strong>and</strong> C++, create a new ATL Project. You can fi nd the ATL<br />

Project Wizard within the Visual C++ Projects group when you select File New Project. Set the name to<br />

COMServer. With the Application Settings, select Dynamic Link Library <strong>and</strong> click Finish.<br />

Because a build step registers the COM component in the registry, which requires<br />

admin privileges, Visual Studio should be started in elevated mode to write ATL<br />

COM objects.<br />

The ATL Project Wizard just creates the foundation<br />

for the server. A COM object is still needed.<br />

Add a class in Solution Explorer <strong>and</strong> select ATL<br />

Simple Object. In the dialog that starts up, enter<br />

COMDemo in the Short name fi eld. The other<br />

fi eld s w i l l b e fi l l e d i n autom at i c a l ly, but c h a n g e<br />

the interface name to IWelcome <strong>and</strong> the Prog ID<br />

to COMServer.COMDemo (see Figure 26 - 7). Click<br />

Finish to create the stub code for the class <strong>and</strong> the<br />

interface.<br />

The COM component offers two interfaces, so that<br />

you can see how QueryInterface() is mapped<br />

from .<strong>NET</strong>, <strong>and</strong> just three simple methods, so that<br />

you can see how the interaction takes place. In class<br />

view, select the interface IWelcome <strong>and</strong> add the<br />

method Greeting() (see Figure 26 - 8) with these<br />

parameters:<br />

figure 26-7<br />

HRESULT Greeting([in] BSTR name, [out, retval] BSTR* message);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!