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.

708 ❘ ChaPTer 26 interOp<br />

An RCW can also be created by using Visual Studio. To create a simple sample application, create a <strong>C#</strong><br />

console project. In Solution Explorer, add a reference to the COM server by selecting the COM tab in the<br />

Add Reference dialog, <strong>and</strong> scroll down to the entry COMServer 1.0 Type Library . Here are listed all<br />

COM objects that are confi gured in the registry. Selecting a COM component from the list creates an RCW<br />

class. With Visual Studio 2010, this wrapper class can be created in the main assembly of the project by<br />

setting the property Embed Interop Types to true , which is the default. Setting it to false creates a<br />

separate interop assembly that needs to be deployed with the application.<br />

using the rCW<br />

After creating the wrapper class, you can write the code for the application to instantiate <strong>and</strong> access the<br />

component. Because of the custom attributes in the C++ fi le, the generated namespace of the RCW class is<br />

Wrox.ProCSharp.COMInterop.Server . Add this namespace, as well as the namespace System.Runtime<br />

.InteropServices , to the declarations. From the namespace System.Runtime.InteropServices , the<br />

Marshal class will be used to release the COM object:<br />

using System;<br />

using System.Runtime.InteropServices;<br />

using Wrox.ProCSharp.Interop.Server<br />

namespace Wrox.ProCSharp.Interop.Client<br />

{<br />

class Program<br />

{<br />

[STAThread]<br />

static void Main()<br />

{<br />

code snippet DotnetClient/Program.cs<br />

Now, the COM component can be used similarly to a .<strong>NET</strong> class. obj is a variable of type COMDemo . COMDemo<br />

is a .<strong>NET</strong> interface that offers the methods of both the IWelcome <strong>and</strong> IMath interfaces. However, it is also<br />

possible to cast to a specifi c interface such as IWelcome . With a variable that is declared as type IWelcome ,<br />

the method Greeting() can be called.<br />

var obj = new COMDemo();<br />

IWelcome welcome = obj;<br />

Console.WriteLine(welcome.Greeting("Stephanie"));<br />

A l t ho u g h COMDemo is an interface, you can instantiate new objects of type COMDemo.<br />

Contrary to normal interfaces, you can do this with wrapped COM interfaces.<br />

If the object — as in this case — offers multiple interfaces, a variable of the other interface can be declared,<br />

<strong>and</strong> by using a simple assignment with the cast operator, the wrapper class does a QueryInterface() with<br />

the COM object to return the second interface pointer. With the I Math variable, the methods of the IMath<br />

interface can be called.<br />

IMath math;<br />

math = (IMath)welcome;<br />

int x = math.Add(4, 5);<br />

Console.WriteLine(x);<br />

If the COM object should be released before the garbage collector cleans up the object, the static method<br />

Marshal.ReleaseComObject() invokes the Release() method of the component, so that the component<br />

can destroy itself <strong>and</strong> free up memory:<br />

}<br />

}<br />

}<br />

Marshal.ReleaseComObject(math);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!