COM Interop - Localserver32 registry entries?
The responses to the poster included such things as "use RegAsm.exe" and "don't do it that way!"
I'm here to say that you can!
First off, if you want to expose a .NET object to COM, you should understand how COM/Automation works: MSDN COM Documentation. Read through the Hello Sample article to see the magic in context.
The MSDN documentation is pretty verbose and the majority of sample code isn't necessary in the .NET world, but it does demonstrate what hooks are needed. I'll highlight the functions that are involved:
- CoRegisterClassObject
- RegisterActiveObject
- RevokeActiveObject
To access RegisterActiveObject and RevokeActiveObject via .NET you'll need to use P/Invoke:
Expand|Select|Wrap|Line Numbers
- [DllImport("oleaut32.dll")]
- public static extern int RegisterActiveObject([MarshalAs(UnmanagedType.IUnknown)] object punk,
- ref Guid rclsid, uint dwFlags, out int pdwRegister);
- [DllImport("oleaut32.dll")]
- public static extern int RevokeActiveObject(int dwRegister, object pvReserved);
In my sample code the call to RegisterActiveObject is as follows:
Expand|Select|Wrap|Line Numbers
- Application app = new Application(); // can be any COMVisible class
- int dwCookie;
- Guid clsidApp = new Guid("9A10580E-0B90-407d-AE0B-8BDD8EA85892");
- int retVal = NativeMethods.RegisterActiveObject(app, ref clsidApp, 0, out dwCookie);
Expand|Select|Wrap|Line Numbers
- RegistrationServices regServices = new RegistrationServices();
- int dwCookieType;
-
dwCookieType = regServices.RegisterTypeForComClients(
typeof(Application), RegistrationClassContext.LocalServer, RegistrationConnectionType.SingleUse);
Expand|Select|Wrap|Line Numbers
- NativeMethods.RevokeActiveObject(dwCookie, null);
- regServices.UnregisterTypeForComClients(dwCookieType);
Expand|Select|Wrap|Line Numbers
- [HKEY_CLASSES_ROOT\AutomationApp.Application]
- @="AutomationApp.Application"
- [HKEY_CLASSES_ROOT\AutomationApp.Application\CLSID]
- @="{9A10580E-0B90-407D-AE0B-8BDD8EA85892}"
- [HKEY_CLASSES_ROOT\CLSID\{9A10580E-0B90-407D-AE0B-8BDD8EA85892}]
- @="AutomationApp.Application"
- [HKEY_CLASSES_ROOT\CLSID\{9A10580E-0B90-407D-AE0B-8BDD8EA85892}\LocalServer32]
- @="C:\PATH\TO\EXE\AutomationApp.exe"