Search code examples
c#comc++-clirhapsody

Marshal.GetActiveObject throws error from C++/CLI but not C#


I am trying to get access to an running instance of an application, Rational Rhapsody, through COM. I am trying to use the C++/CLI COM calls.
From C++ calling:

rhapsody::RPApplication^ App = safe_cast<rhapsody::RPApplication^>( Marshal::GetActiveObject("Rhapsody.Application"));

Causes a COM Exception : 800401E3 (Operation Unavailable)

But, using Marshal::GetActiveObject("Word.Application") works just fine. Using gcnew rhapsody::RPApplication() works fine to make a new instance and the same code in C#:

rhapsody.RPApplication App = (rhapsody.RPApplication) Marshal.GetActiveObject("Rhapsody.Application")

works just fine. Any ideas why it doesn't work from C++/CLI?


Solution

  • Is your main() routine in C++/CLI flagged with [STAThread]? It's commonly required, especially when dealing with COM objects.

    [STAThread]
    int main(array<System::String^>^args)
    {
         // code here...
    }