3

How do I programmatically restart a COM+ application running on a remote server from code in .NET?

Alfred Myers
  • 6,384
  • 1
  • 40
  • 68
Daniel Fortunov
  • 43,309
  • 26
  • 81
  • 106

1 Answers1

7

You have to use the ComAdmin API through COM interop.

Put a reference on Windows\System32\Com\ComAdmin.dll, then:

COMAdmin.COMAdminCatalog catalog = new COMAdmin.COMAdminCatalogClass();
catalog.Connect(servername);
catalog.ShutdownApplication(AppNameOrAppID);

You can find the ComAdmin reference in MSDN here.

It's a COM API, and kind of wierd. Eg. you cannot instantiate a COMAdminCatalog, because it's an interface, not a class, so you have to use COMAdminCatalogClass to create a new instance. Use Visual Studio's Object Browser to look around in the COMAdmin namespace to find out these kinds of pitfalls.

EDIT (some note):

Actually, you CAN write

COMAdmin.COMAdminCatalog catalog = new COMAdmin.COMAdminCatalog();

and it works which is surprising because COMAdminCatalog is an interface. But it must be a trick of VStudio or the C# compiler, because the resulting assembly contains the following IL:

newobj instance void [Interop.COMAdmin]COMAdmin.COMAdminCatalogClass::.ctor()

So it somehow found out that the COMAdminCatalogClass must be instantiated, which is strange enough and a bit confusing too. If someone knows how it happens please comment.

Vizu
  • 1,871
  • 1
  • 15
  • 21
  • Excellent answer. Thanks for giving me all the information I need to get underway with tihs. – Daniel Fortunov Jun 05 '09 at 10:03
  • 2
    It has to do with the CoClass attribute that is applied to the COM Interop's interface. Back in 2009 I found out some unintended uses for the attribute and posted a question about it at http://stackoverflow.com/q/1303717/151249 See the links on Marc Gravell's answer for details – Alfred Myers Jun 02 '11 at 20:25