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.

716 ❘ ChaPTer 26 interOp<br />

};<br />

]<br />

coclass DotnetComponent {<br />

[default] interface _DotnetComponent;<br />

interface _Object;<br />

interface IWelcome;<br />

interface IMath;<br />

};<br />

[<br />

odl,<br />

uuid(2B36C1BF-61F7-3E84-87B2-EAB52144046D),<br />

hidden,<br />

dual,<br />

oleautomation,<br />

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

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

]<br />

interface _DotnetComponent : IDispatch {<br />

};<br />

There are quite a few defaults for generating the type library. However, often it is advantageous to change<br />

some of the default .<strong>NET</strong> to COM mappings. This can be done with several attributes in the System.<br />

Runtime.InteropServices namespaces.<br />

Com interop attributes<br />

Applying attributes from the namespace System.Runtime.InteropServices to classes, interfaces, or<br />

methods allows you to change the implementation of the CCW. The following table lists these attributes <strong>and</strong><br />

their descriptions.<br />

aTTribuTe<br />

Guid<br />

ProgId<br />

ComVisible<br />

desCriPTion<br />

This attribute can be assigned to the assembly, interfaces, <strong>and</strong> classes. Using the<br />

Guid as an assembly attribute defines the type-library ID, applying it to interfaces<br />

defines the interface ID (IID), <strong>and</strong> setting the attribute to a class defines the class ID<br />

(CLSID). The unique IDs that must be defined with this attribute can be created with the<br />

utility guidgen. The CLSID <strong>and</strong> type-library IDs are changed automatically with every<br />

build. If you don’t want to change them with every build, you can fix this by using this<br />

attribute. The IID is changed only if the signature of the interface changes; for example,<br />

if a method is added or removed, or some parameters are changed. Because with<br />

COM the IID should change with every new version of this interface, this is a very good<br />

default behavior, <strong>and</strong> usually there’s no need to apply the IID with the Guid attribute.<br />

The only time you want to apply a fixed IID for an interface is when the .<strong>NET</strong> interface<br />

is an exact representation of an existing COM interface, <strong>and</strong> the COM client already<br />

expects this identifier.<br />

This attribute can be applied to a class to specify what name should be used when the<br />

object is configured in the registry.<br />

With the Assembly Information settings of the Project properties you can configure<br />

if all the types of the assembly should be visible by COM. By default this setting<br />

is false which is a good default that makes it necessary to explicitly mark the<br />

classes, interfaces, <strong>and</strong> delegates with the ComVisible attribute to create a COM<br />

representation. In case the default setting is changed to make all types visible by<br />

COM, you can set that ComVisible attribute to false for the types where a COM<br />

representation should not be created.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!