Wednesday, September 12, 2007

Conditional build events

In my current C# interop project, I unregister the existing COM dll using a pre-build event:

regasm /u $(TargetPath)

However, this fails if the file is not present (after a clean), so I use the following command to only unregister it if it is present.

path= $(TargetDir) for %f in ($(TargetFileName)) do C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe /u %f

Exposing COM events from .Net to Javascript in web browser

A lot has already been written about this so I won't repeat all the details here. These two links give a good guide to how to do it.

COM Interop Exposed - Part 2

HOW TO: Sink Managed C# Events in Internet Explorer Script

This post is about troubleshooting some common mistakes that may occur. The key thing to note is that if you fail to follow the steps listed in these articles:
  1. you will NOT get a compile time error
  2. you will NOT get an error when registering the component
  3. you will NOT get an error at runtime when you attach the event handler.
Mistakes that I made were:
  1. Did not put a Dispid attribute on the methods in my event interface.
  2. Had name mismatches between the events in my main class and the method names in my interface.
I'll add some test code here later.

Friday, September 07, 2007

COM component not registering properly

Related to my previous post, I was also having trouble getting the new .net COM object to register properly, even when I had removed all references to earlier versions. I had used:

>RegAsm assemblyName

but was unable to invoke the component in a web page. When I tried to examine it in OleView it initially seemed to be fine. However, when I tried to expand the class name node (under .Net category) I got the following error:

CoGetClassObject failed.
The system cannot find the file specified.

severity: SEVERITY_ERROR, facility: FACILITY_WIN32 ($80070002)

Registering it with:

>RegAsm assemblyName /codebase

fixed the issue

Regasm /u not working

I had a strange problem when trying to unregister a COM visible .Net component recently. I had built and registered several versions but when I tried to unregister them I could still see them listed when using OleView. It turns out that the command:

RegAsm /u assemblyName

will only remove the entries that match the version number of your assembly. If you have been incrementing the version number of your component and registering the new one without unregistering the previous version then you will end up with entries for each version. To clear them out you can just set the version number of the assembly, compile it and then unregister it with RegAsm. Do this for each version that you need to unregister.