Set - 8

Question 41 :

I can't import the COM object that I have on my machine. Did you write that object?

Answer :

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.

The answer to (5) is only partially correct. You *can* import COM objects, even if they are not your own. It is recommended that you obtain the Primary Interop from the vendor, but not required. The most common problem importing a COM DLL is that it exposes objects that form part of a separate COM DLL. You can generate interops for these additional DLLs, and then refer to them when importing the problem DLL.

Question 42 :

How do you call unmanaged methods from your .NET code through PInvoke?

Answer :

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.

Question 43 :

Can you retrieve complex data types like structs from the PInvoke calls?

Answer :

Yes, just make sure you re-declare that struct, so that managed code knows what to do with it.

Question 44 :

I want to expose my .NET objects to COM objects. Is that possible?

Answer :

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.

Question 45 :

Can you inherit a COM class in a .NET application?

Answer :

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.