Search code examples
.netcomcomadmin

How do I restart a COM+ application on a remote server from .NET?


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


Solution

  • 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.