Saturday, September 18, 2004

Exposing .NETAssemblies through COM Interop

1) Expose the functionality through an interface that is implemented by a concrete class.

2) Ensure you have a static assembly version number to work with. Having an * value in the version number means it is incremented with each build and will get out of sync with the version that has been registered.

3) Add a GuidAttribute to the class, interface and *any* other types exposed through COM else .Net will change it with each build.

4) Decorate the types you wish to expose with the ComVisible attribute and set the ComVisible attribute to false in assemblyinfo.cs so you are only exposing what you want.

5) Create a strong name key file using sn -k filename.key from the .Net command line.

6) set the AssemblyKeyFile property in the AssemblyInfo file or the Wrapper Assembly Key File project properties to the full filepath and name of the filename.key file.

7) Set the AssemblyKeyName project property like so:

sn.exe -i filename.key FileNameKey
AssemblyInfo.cs: AssemblyKeyName("FileNameKey")]

NOTE: If you build the project with the "Register for COM Interop" property set to true then regasm will be run each time you build the project, so if you need to make the assembly available from another location on your machine you will need to ensure that the registry settings are reset by running regasm /unregister on your project assembly then registering the assembly from its new location.
Remember, Visual Studio helpfully copies dependent assemblies around and you may need to search and destroy old versions.

8) If you wish, add it to the GAC like so; gacutil /i MyAssembly.dll

9) Register it in the system registry for use in COM like so; Regasm MyAssembly.dll /codebase /tlb:MyTyplelib.tlb

Light the blue touch paper and stand back

Who said "It just works"?

No comments: