This is response to some poor guidance in response to this post:
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:
To access RegisterActiveO bject and RevokeActiveObj ect via .NET you'll need to use P/Invoke:
The output parameter, pdwRegister, is an identifier that is passed into RevokeActiveObj ect when the application is shutting down.
In my sample code the call to RegisterActiveO bject is as follows:
But wait there's more! In addition to registering your .NET object instance in the ROT, you need to register the Type of the object via .NET v2.0's System.Runtime. InteropServices .RegistrationSe rvices class:
When the application shuts down, you'll need to clean up:
Once you've got these functions in place, the class must be registered in Windows. Here's what my reg file looks like:
This is truly the very tip of an enormous iceberg, but the MSDN documentation won't lead you astray.
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:
- CoRegisterClass Object
- RegisterActiveO bject
- RevokeActiveObj ect
To access RegisterActiveO bject and RevokeActiveObj ect via .NET you'll need to use P/Invoke:
Code:
[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 RegisterActiveO bject is as follows:
Code:
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);
Code:
RegistrationServices regServices = new RegistrationServices(); int dwCookieType; dwCookieType = regServices.RegisterTypeForComClients([INDENT]typeof(Application), RegistrationClassContext.LocalServer, RegistrationConnectionType.SingleUse); [/INDENT]
Code:
NativeMethods.RevokeActiveObject(dwCookie, null); regServices.UnregisterTypeForComClients(dwCookieType);
Code:
[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"
Comment