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.

718 ❘ ChaPTer 26 interOp<br />

Com registration<br />

Before the .<strong>NET</strong> component can be used as a COM object, it is necessary to configure it in the registry. Also,<br />

if you don’t want to copy the assembly into the same directory as the client application, it is necessary to<br />

install the assembly in the global assembly cache. The global assembly cache itself is discussed in Chapter 18.<br />

To install the assembly in the global assembly cache, you must sign it with a strong name (using Visual<br />

Studio 2010, you can define a strong name in properties of the solution). Then you can register the assembly<br />

in the global assembly cache:<br />

gacutil -i DotnetServer.dll<br />

Now, you can use the regasm utility to configure the component inside the registry. The option /tlb<br />

extracts the type library <strong>and</strong> also configures the type library in the registry:<br />

regasm DotnetServer.dll /tlb<br />

The information for the .<strong>NET</strong> component that is written to the registry is as follows. The All COM<br />

configuration is in the hive HKEY_CLASSES_ROOT (HKCR). The key of the ProgID (in this example, it is<br />

Wrox.DotnetComponent) is written directly to this hive, along with the CLSID.<br />

The key HKCR\CLSID\{CLSID}\InProcServer32 has the following entries:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

mscoree.dll — mscoree.dll represents the CCW. This is a real COM object that is responsible for<br />

hosting the .<strong>NET</strong> component. This COM object accesses the .<strong>NET</strong> component to offer COM behavior<br />

for the client. The file mscoree.dll is loaded <strong>and</strong> instantiated from the client via the normal COM<br />

instantiation mechanism.<br />

ThreadingModel=Both — This is an attribute of the mscoree.dll COM object. This component is<br />

programmed in a way that offers support both for STA <strong>and</strong> MTA.<br />

Assembly=DotnetServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5cd57c9<br />

3b4d9c41a — The value of the Assembly stores the assembly full name, including the version number<br />

<strong>and</strong> the public key token, so that the assembly can be uniquely identified. The assembly registered here<br />

will be loaded by mscoree.dll.<br />

Class=Wrox.ProCSharp.Interop.Server.DotnetComponent — The name of the class will also<br />

be used by mscoree.dll. This is the class that will be instantiated.<br />

RuntimeVersion=v4.0.20826 — The registry entry RuntimeVersion specifies the version of the<br />

.<strong>NET</strong> runtime that will be used to host the .<strong>NET</strong> assembly.<br />

In addition to the configurations shown here, all the interfaces <strong>and</strong> the type library are configured with their<br />

identifiers, too.<br />

Creating a Com Client application<br />

Now, it’s time to create a COM client. Start by creating a simple C++ Win32 Console application project,<br />

<strong>and</strong> name it COMClient. You can leave the default options selected <strong>and</strong> click Finish in the Project Wizard.<br />

At the beginning of the file COMClient.cpp, add a preprocessor comm<strong>and</strong> to include the <br />

header file <strong>and</strong> to import the type library that you created for the .<strong>NET</strong> component. The import statement<br />

creates a “smart pointer” class that makes it easier to deal with COM objects. During a build process, the<br />

import statement creates .tlh <strong>and</strong> .tli files that you can find in the debug directory of your project, which<br />

includes the smart pointer class. Then add using namespace directives to open the namespace std that will<br />

be used for writing output messages to the console, <strong>and</strong> the namespace DotnetComponent that is created<br />

inside the smart pointer class:<br />

// COMClient.cpp: Defines the entry point for the console application.<br />

//<br />

#include "stdafx.h"<br />

#include <br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!