Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the .NET COM Callable Wrapper generate IIDs?

Tags:

.net

c++-cli

ccw

Viewing the generated TLB file created by the CCW though the OLE/COM Object Viewer shows the IID remains constant unless I change the design of the interface (which is correct behaviour), my concern is that if I compile this same code on another machine a completely different IID will be generated despite the interface not changing and hence breaking existing COM clients.

  1. How are the COM Interface IDs generated by the COM Callable Wrapper?
  2. How does the CCW know if the interface has changed and needs to generate a new IID?
  3. Would it be safer to just generate my own and declare in the source file?
like image 370
TownCube Avatar asked Jul 03 '11 15:07

TownCube


People also ask

What is COM callable wrapper in C#?

COM callable wrappers are invisible to other classes running within the . NET runtime. Their primary purpose is to marshal calls between managed and unmanaged code; however, CCWs also manage the object identity and object lifetime of the managed objects they wrap.

Which of the following directory is used to create the runtime callable wrapper while using a COM component in a. net assembly?

To create a runtime callable wrapper using Visual Studio In the Add Reference dialog box, click the COM tab, select the component you want to use, and click OK. In Solution Explorer, note that the COM component is added to the References folder in your project.

Which wrapper is useful when .NET client talks to COM?

So, to communicate with a COM component, the COM component should be wrapped in such a way that the.NET client application can understand the COM component. This wrapper is known as Runtime Callable Wrapper (RCW).

What is a .NET wrapper?

A Wrapper class is one that "encapsulates" a resource, or another class in order to simplify or restrict the interface between the outside world and the encapsulated object. For example, a wrapper class might encapsulate a COM object, providing .


1 Answers

The guid for a type is not specific to COM interop, all .NET types have a guid. You can obtain it from the Type.GUID property. The code in the CLR that generates it is available from the SSCLI20 distribution. You can have a look-see at the algorithm by checking out the code in clr/scr/vm/methodtable.cpp, MethodTable::GetGuid() method.

Summarizing the algorithm:

  • if the type has the [Guid] attribute then return that
  • if the type is an interface type then generate a stringized version of the interface type definition. This is done by GetStringizedItfDef() in interoputil.cpp. It starts with the fully qualified type name and appends a string version of each member definition.
  • for all other types, generate a stringized version of the class. It starts with the fully qualified type name, appends the class name and appends the fully qualified assembly name.
  • the resulting string is then hashed into a Guid by helper function CorGuidFromNameW().

This is enough info to answer your questions:

How are the COM Interface IDs generated by the COM Callable Wrapper?

It uses the Type.GUID as generated with the algorithm above. None of the elements in the algorithm are specific to the machine it is executed on so you don't have to fear getting different IIDs and CLSIDs on different build machines.

How does the CCW know if the interface has changed and needs to generate a new IID?

It doesn't. It purely relies on the algorithm producing different GUIDs for different interface definitions.

Would it be safer to just generate my own and declare in the source file?

Not really. There's no documented failure mode for this algorithm. Using your own [Guid] attribute significantly increases the odds that you'll forget to change it when you have to. This is a shortcut that's taken frequently and the primary source of DLL Hell. The nasty kind, the kind that makes the client crash with near-impossible to diagnose hardware exceptions. As opposed to the still-hard-but-not-impossible kind of E_NOINTERFACE. There are really only two disadvantages I can think of by relying on the auto-generated Guid: it tends to cause registry pollution on your dev machine when you forget to unregister assemblies before rebuilding them. And it slows you down a bit when you're troubleshooting COM errors because you don't readily know the guids. Admittedly, the registry pollution is good enough reason for me.

like image 102
Hans Passant Avatar answered Oct 27 '22 00:10

Hans Passant