Partilhar via


Exposing .NET Compact Framework Components to COM 

You can expose .NET Compact Framework components to native code, but there are differences in this interoperability as compared to the full .NET Framework.

Exposing Managed Components

You can provide managed .NET Compact Framework components to native applications. The process involves the following tasks:

  • Design your managed types while considering COM programming. For example, do not use generic types.

  • Define the functionality you wish to expose in a managed interface, and have your managed object implement this interface. The following is recommended:

    • Use the GUID, InterfaceType, and ComVisible attributes for your managed interface.

    • Use the ClassInterface(ClassInterfaceType.None) attribute on your managed object.

  • Create native definitions of your managed types.

  • Pass your managed object to native code as a COM interface. You can then call managed functions implemented by your managted object from native code by using the COM interface.

.NET Compact Framework Differences

To call managed .NET Compact Framework functions from native code, you must first use the runtime to marshal a managed interface or a delegate down to native code. This means you must always start out in managed code (with a managed executable) in order to expose managed components to native code.

The following interoperability with native code is not supported by the .NET Compact Framework:

  • Using an assembly registration tool, such as Regasm.exe, for COM clients to use a managed component as a COM class.

  • Activating managed types from COM.

  • Hosting the runtime from native code.

  • Hosting ActiveX controls. The .NET Compact Framework does not support the AxHost class and you cannot use Microsoft Visual Studio 2005 and the designer to parent an ActiveX control to your managed form. However, given the COM interop support in .Net Compact Framework, the infrastructure exists to build your own ActiveX container host.

  • Initializing COM to use single-threaded apartments.

  • Auto generating class interfaces. You must wrap the functionality you’d like to expose to native code in an explicit interface.

  • Auto generating GUIDs. You must give each interface an explicit guid in order to expose it to native code.

Design-Time Attributes for Exposed Managed Components

You can provide design-time attributes for exposing managed components to COM. The .NET Compact Framework supports a subset of the attributes available in the full .NET Framework, including the following:

See Also

Other Resources

Interoperability in the .NET Compact Framework