473,395 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

COM servers implemented with .NET

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:
  • CoRegisterClassObject
  • RegisterActiveObject
  • RevokeActiveObject

To access RegisterActiveObject and RevokeActiveObject via .NET you'll need to use P/Invoke:
Expand|Select|Wrap|Line Numbers
  1. [DllImport("oleaut32.dll")]
  2. public static extern int RegisterActiveObject([MarshalAs(UnmanagedType.IUnknown)] object punk,
  3.                 ref Guid rclsid, uint dwFlags, out int pdwRegister);
  4. [DllImport("oleaut32.dll")]
  5.         public static extern int RevokeActiveObject(int dwRegister, object pvReserved);
  6.  
The output parameter, pdwRegister, is an identifier that is passed into RevokeActiveObject when the application is shutting down.

In my sample code the call to RegisterActiveObject is as follows:
Expand|Select|Wrap|Line Numbers
  1. Application app = new Application(); // can be any COMVisible class
  2. int dwCookie;
  3. Guid clsidApp = new Guid("9A10580E-0B90-407d-AE0B-8BDD8EA85892");
  4. int retVal = NativeMethods.RegisterActiveObject(app, ref clsidApp, 0, out dwCookie);
  5.  
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.RegistrationService s class:
Expand|Select|Wrap|Line Numbers
  1. RegistrationServices regServices = new RegistrationServices();
  2. int dwCookieType;
  3. dwCookieType = regServices.RegisterTypeForComClients(
    typeof(Application), RegistrationClassContext.LocalServer, RegistrationConnectionType.SingleUse);
When the application shuts down, you'll need to clean up:
Expand|Select|Wrap|Line Numbers
  1. NativeMethods.RevokeActiveObject(dwCookie, null);
  2. regServices.UnregisterTypeForComClients(dwCookieType);
  3.  
Once you've got these functions in place, the class must be registered in Windows. Here's what my reg file looks like:

Expand|Select|Wrap|Line Numbers
  1. [HKEY_CLASSES_ROOT\AutomationApp.Application]
  2. @="AutomationApp.Application"
  3.  
  4. [HKEY_CLASSES_ROOT\AutomationApp.Application\CLSID]
  5. @="{9A10580E-0B90-407D-AE0B-8BDD8EA85892}"
  6.  
  7. [HKEY_CLASSES_ROOT\CLSID\{9A10580E-0B90-407D-AE0B-8BDD8EA85892}]
  8. @="AutomationApp.Application"
  9.  
  10. [HKEY_CLASSES_ROOT\CLSID\{9A10580E-0B90-407D-AE0B-8BDD8EA85892}\LocalServer32]
  11. @="C:\PATH\TO\EXE\AutomationApp.exe"
  12.  
This is truly the very tip of an enormous iceberg, but the MSDN documentation won't lead you astray.
Feb 20 '08 #1
1 7610
hegge
1
Thanks for the great example molaughlen. I managed to get this to port this and get it to work in VB.NET 2005 Express. The EXE-file registers itself on launch and can be called with GetObject from any COM-compatible application

One problem though, when the EXE is not running, I'll instantiate it with CreateObject. The EXE COM Server is then launched as defined in LocalServer32.

The problem is that the COM Server takes command of the calling application (Excel for example) and won't let me work with Excel in the background until I close the COM Server again. Excel is giving me the error that it is waiting for an OLE-action to be completed in another application

Do I need to start a new thread or what is the problem? I want to be able to continue to work in the calling application (Excel) even when the EXE Server is running.

Thankful for any help with this issue,

Regards
/hegge
Apr 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Captain Ranger McCoy | last post by:
Hello! Suppose I have ten servers at ten ips: x.x.x.1 x.x.x.2 x.x.x.3 x.x.x.4 and so on Each server hosts 100+ photo galleries, all under a single domain name,
5
by: nmac | last post by:
Hi all, hopefully someone can offer some sagely advice regarding Production use of Jakarta's Tomcat. First, some brief background. My company have a servlet application that connects to a MySQL...
2
by: blomm | last post by:
This is probably more of a SQL server issue, but its also .net related I think... I have the client side of my application installed in the US, with the application and the database servers running...
12
by: glenn | last post by:
How do you write a COM server in C# VS 2003? I can't believe it but I can't even find the words COM in the help for VS related to COM servers. Did Microsoft Rename it again or can VS C# not...
0
by: Kuldeep | last post by:
Hi, I implemented Forms authentication for my apps (more than one). Since it'll be hosted on a webfarm, I set cookie.domain = "nnn.nnn.com". This works fine. I can signin and then go to other...
1
by: G.Fink.Nottle | last post by:
Hello, Being a bit of a SQL Server novice, need some advice with the following situation. Server A and Server B have SQLServer 2000 based databases. The vendor of the application/system has...
1
by: RichF | last post by:
Hi, I have read about using regasm to "register" .net class libraries (.dll) so they could be used through COM clients. However is it possible to create .net class libraries that are...
3
by: Ben Holness | last post by:
Hi all, I have a php/mysql website where people can upload their own graphics for the buttons and background of pages on the website. This used to run on one server, but I have now been asked...
13
by: Samir Chouaieb | last post by:
Hello, I am trying to find a solution to a login mechanism for different domains on different servers with PHP5. I have one main domain with the user data and several other domains that need...
2
by: CK | last post by:
I'm try to implement a CATCHPA script on a server that doesn't have GD extension. So i'm calling a PHP script on another server to return the image. Only problem is I need to return a variable...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.