Monday, May 21, 2007

.NET and COM interop questions

Define Runtime Callable Wrapper (RCW)?
RCW is a metadata wrapper allowing .NET application to call COM Components. The .NET application communicates with a COM component through a managed wrapper of the component called Runtime Callable Wrapper. It acts as managed proxy to the unmanaged COM component.
There are two ways to generate a managed metadata wrapper:
· Using Type Library Importer utility.
· VS.NET IDE
Type Library Importer (tlbimp.exe) is a command line syntax, which converts COM specific type definition in a COM type library into equivalent definitions for a .NET wrapper assembly. By default, the utility gives the wrapper assembly the same name as the COM DLL.

How do we implement COM Interoperability?
· Create Runtime Callable Wrapper out of a COM Component
· Reference the metadata assembly DLL in the project and use its methods and properties.

What is COM?
COM stands for Component Object Model, which is a binary specification for software code re-use. It imposes a standard for the interfaces through which client code talks to component classes. The component’s IUnknown interface helps to maintain a reference count of the number of clients using the component. When this count drops down to zero, the component is unloaded. All components should implement the IUnknown interface. The reference count is maintained through IUnknow::AddRef() and IUnknow::Release() methods, and interface discovery is handled through IUnknow::QueryInterface().

What is the need for Interoperability?
COM components have a different internal architecture from .NET components, hence they are not innately compatible. Most organizations, which have built their enterprise applications on COM objects for their middle tier services, cannot write off the investments on these solutions. These legacy components ought to be exploited by managed code in the .NET framework. This is where Interoperability pitches in; it’s a Runtime Callable Wrapper (RCW) that translates specific calls from managed clients into COM specific invocation requests on unmanaged COM components. The method call on RCW will make .NET components believe that they are talking to just another .NET component.

Describe the advantages of writing a managed code application instead of unmanaged one. What’s involved in certain piece of code being managed?
The advantages include automatic garbage collection, memory management, support for versioning and security. These advantages are provided through .NET FCL and CLR, while with the unmanaged code similar capabilities had to be implemented through third-party libraries or as a part of the application itself.

Are COM objects managed or unmanaged?
Since COM objects were written before .NET, apparently they are unmanaged.

So can a COM object talk to a .NET object?
Yes, through Runtime Callable Wrapper (RCW) or PInvoke.

How do you generate an RCW from a COM object?
Use the Type Library Import utility shipped with SDK. tlbimp COMobject.dll /out:.NETobject.dll or reference the COM library from Visual Studio in your project.

I can’t import the COM object that I have on my machine. Did you write that object?
You can only import your own objects. If you need to use a COM component from another developer, you should obtain a Primary Interop Assembly (PIA) from whoever authored the original object.

How do you call unmanaged methods from your .NET code through PInvoke?
Supply a DllImport attribute. Declare the methods in your .NET code as static extern. Do not implement the methods as they are implemented in your unmanaged code, you’re just providing declarations for method signatures.

Can you retrieve complex data types like structs from the PInvoke calls?
Yes, just make sure you re-declare that struct, so that managed code knows what to do with it.

I want to expose my .NET objects to COM objects. Is that possible?
Yes, but few things should be considered first. Classes should implement interfaces explicitly. Managed types must be public. Methods, properties, fields, and events that are exposed to COM must be public. Types must have a public default constructor with no arguments to be activated from COM. Types cannot be abstract.

Can you inherit a COM class in a .NET application?
The .NET Framework extends the COM model for reusability by adding implementation inheritance. Managed types can derive directly or indirectly from a COM coclass; more specifically, they can derive from the runtime callable wrapper generated by the runtime. The derived type can expose all the method and properties of the COM object as well as methods and properties implemented in managed code. The resulting object is partly implemented in managed code and partly implemented in unmanaged code.

Suppose I call a COM object from a .NET applicaiton, but COM object throws an error. What happens on the .NET end?
COM methods report errors by returning HRESULTs; .NET methods report them by throwing exceptions. The runtime handles the transition between the two. Each exception class in the .NET Framework maps to an HRESULT.

No comments: