15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using a CoM Component from a .neT Client ❘ 705<br />

};<br />

[id(1)] HRESULT Add([in] LONG val1, [in] LONG val2,<br />

[out, retval] LONG* result);<br />

[id(2)] HRESULT Sub([in] LONG val1, [in] LONG val2,<br />

[out, retval] LONG* result);<br />

The coclass COMDemo must also be changed so that it implements both the interfaces IWelcome <strong>and</strong><br />

Math. The IWelcome interface is the default interface:<br />

importlib("stdole2.tlb");<br />

[<br />

uuid(ACB04E72-EB08-4D4A-91D3-34A5DB55D4B4),<br />

helpstring("COMDemo Class"),<br />

custom(0F21F359-AB84-41e8-9A78-36D110E6D2F9,<br />

"Wrox.ProCSharp.Interop.Server.COMDemo")<br />

]<br />

coclass COMDemo<br />

{<br />

[default] interface IWelcome;<br />

interface IMath;<br />

};<br />

Now, you can set the focus away from the IDL file toward the C++ code. In the file COMDemo.h, you can find<br />

the class definition of the COM object. The class CCOMDemo uses multiple inheritances to derive from the<br />

template classes CComObjectRootEx, CComCoClass, <strong>and</strong> IDisplatchImpl. The CComObjectRootEx class<br />

offers an implementation of the IUnknown interface functionality such as implementation of the AddRef()<br />

<strong>and</strong> Release() methods. The CComCoClass class creates a factory that instantiates objects of the template<br />

argument, which here is CComDemo. IDispatchImpl offers an implementation of the methods from the<br />

IDispatch interface.<br />

With the macros that are surrounded by BEGIN_COM_MAP <strong>and</strong> END_COM_MAP, a map is created to define all<br />

the COM interfaces that are implemented by the COM class. This map is used by the implementation of the<br />

QueryInterface method.<br />

class ATL_NO_VTABLE CCOMDemo:<br />

public CComObjectRootEx,<br />

public CComCoClass,<br />

public IDispatchImpl<br />

{<br />

public:<br />

CCOMDemo()<br />

{<br />

}<br />

DECLARE_REGISTRY_RESOURCEID(IDR_COMDEMO)<br />

BEGIN_COM_MAP(CCOMDemo)<br />

COM_INTERFACE_ENTRY(IWelcome)<br />

COM_INTERFACE_ENTRY(IDispatch)<br />

END_COM_MAP()<br />

DECLARE_PROTECT_FINAL_CONSTRUCT()<br />

HRESULT FinalConstruct()<br />

{<br />

return S_OK;<br />

}<br />

void FinalRelease()<br />

{<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!