Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Expose a .NET Assembly via COM - without RegASM.exe

I kept promising myself I would write a post on how to expose a .NET assembly to COM without having to use RegASM.exe. Someone asked me today for the very code to do this, so I thought now would be as good as time as any to write my post.

Microsoft provided a wonderful thing when they invented COM Interop. Most developers know about calling existing COM components from inside a C# or VB.NET application, but fewer developers know about operating the system in reverse - calling .NET from VB6 or C++ via COM.

My code, attached below, emulates exactly what RegASM.exe does, but provides the developer better control over what happens to the registry and GAC. To successfully expose a .NET class, contained in an assembly, via COM the following things have to happen:

• Said .NET assembly must be added to the GAC (Global Assembly Cache). There is debate on whether this is true. However, it's generally a good idea to install an assembly in a fixed location when adding registry hooks to that location, and what better place than the GAC. Non-GAC'd assemblies are liable to break when moved or overwritten by newer non-backwards compatible versions. The GAC will ensure the correct version of an assembly is maintained and contained (look, it even rhymes).

• Registry entries must be added for each exposed class and interface. Regular COM classes refer to in-proc or out-proc dynamic link library servers. When exposing a .NET assembly the registry entries refer to the .NET framework core (mscoree.dll) as the server. The .NET framework will intercept all COM calls and load the .NET assembly on behalf of the calling process.

• A type library is to be generated to provide calling applications with a map of exposed interfaces, methods and properties.

The code attached provides all of the above:

Installer.cs - Provides methods to add and remove .NET assemblies to and from the GAC, and to install and uninstall registry entries.
RegistryValue.cs - Helper class for registry value entries.
TypeLibGen.cs - Helper class for type lib generation.

At the end I have included example code to install and remove assemblies, as well as what attributes need to be added to a .NE assembly to make it exportable via COM.

A note on TypeLib generation:

I haven't been able to find some nice C# code to generate type libraries from .NET assemblies using reflection, so I had to use the built in TypeLibConverter method - ConvertAssemblyToTypeLib. Unfortunately this method doesn't allow me to specify what classes, methods and attributes are exported to the type library, it exports all public classes and types. This can be confusing - the registry may not contain registry entries for some of these exported types, depending on how you attribute your code.

To avoid this problem, attribute all public classes and types with [ComVisible(False)] if not intended to be callable from COM. I show this in my example at the bottom of the code.

Download code here.
Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Monday, January 24, 2005 3:07 PM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

TC said:

I need to expose a .NET control as an ActiveX control to an ActiveX control container. From what I've been able to find Microsoft removed any support for this in Beta 2. Nathan's book says it's possible "at your own risk" & gives an example. Tried it with my control to no avail. Other articles mention workarounds & manual methods. Any insight or examples you might have? Thanks.
August 1, 2005 10:24 AM
 

Rob Garrett said:

I've not yet played with exposing .NET code as COM controls in Ver. 2 Beta 2. I shall put aside some time and see if I can help you with this.
August 1, 2005 1:30 PM
 

CR said:

Microsoft Interop Forms Toolkit 2.0a allows you to easy build and wrap .NET UserControls and Winforms with COM Interop which allows them to be treated as ActiveX/COM components for use in VB6 and VBA. Its a VS2005 Add-in and it works flawlessly. I have successfully created and deployed user controls to an MS Access VBA mdb project. The control gets built into the target machine's mscoree.dll, and will appear in the list of ActiveX controls inside of the vba environment. I dropped the component on an access form, and it runs perfectly. All of the logic code was pure .net code, but you can also expose events and properties to the com container by adding Shadows scoped Events and Properties. quite slick. http://msdn2.microsoft.com/en-us/vbasic/bb419144.aspx
December 19, 2007 10:18 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems