title: IRunningObjectTable::Register throws E_INVALIDARG while registering a class object
body: <p>I'm trying to register a COM class object, which represents the application I want to automate, to the Running Object Table (ROT). However, the returned HRESULT is <code>E_INVALIDARG</code>, though all the supplied arguments match the <a href="https://docs.microsoft.com/en-us/windows/desktop/api/objidl/nf-objidl-irunningobjecttable-register" rel="nofollow noreferrer">documented</a> types.</p>
  
  <p>The application type is EXE and it's registered in the Windows Registry with a CLSID and its type there is LocalServer.</p>
  
  <p>The reason of my attempt relates to a similarly asked <a href="https://stackoverflow.com/questions/27444419/add-items-to-the-rot-running-objects-table">question</a>.</p>
  
  <p>I usually connect with some applications using the <a href="https://pythonhosted.org/comtypes/" rel="nofollow noreferrer">comtypes</a> package of python, most of them succeed with the call to <code>GetActiveObject()</code>. However, when calling <code>GetActiveObject()</code> with the CLSID of this application I get an error</p>
  
  <blockquote>
    <p>WindowsError: [Error -2147221021] Operation unavailable</p>
  </blockquote>
  
  <p>The effective part of the code of the MFC Application is the following</p>
  
  <pre class="lang-cpp prettyprint-override"><code>CoInitialize(NULL);
  
  IMoniker *appmnk;
  CLSID appclsid;
  IUnknown* app_ptr = NULL; //in CoGetClassObject, the pointer to the exe application
  
  DWORD dwrdptr;
  
  IRunningObjectTable *rot; //fetch the pointer using GetRunningObjectTable
  HRESULT hr;
  HRESULT hr0;
  HRESULT hr1;
  HRESULT hr2;
  HRESULT hr3;
  
  hr0 = CLSIDFromProgID(OLESTR("TheApp.Application.1"), &amp;appclsid);
  // "TheApp.Application.1" is a hypothetical ProgID
  // In the actual code, it's replaced with the actual ProgID
  
  if (SUCCEEDED(hr0))
  {
      hr1 = CoGetClassObject(appclsid, CLSCTX_LOCAL_SERVER, NULL, IID_IUnknown, (void **)&amp;app_ptr);
      if (SUCCEEDED(hr1))
      {
          hr2 = CreateItemMoniker(L"!", OLESTR("myappmnk"), &amp;appmnk);
          if (SUCCEEDED(hr2))
          {
              hr3 = GetRunningObjectTable(0, &amp;rot);
              if (SUCCEEDED(hr3))
              {
                  hr = rot-&gt;Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, app_ptr, appmnk, &amp;dwrdptr);
                  rot-&gt;Release();
              }
              appmnk-&gt;Release();
          }
          app_ptr-&gt;Release();
      }
  }
  
  CoUninitialize();
  </code></pre>
  
  <p>The end result I'm seeking is to successfully register the application's COM object to ROT, in order to get a successful result from <code>GetActiveObject</code>.</p>
  
  <p>Any help and any insight will be truly appreciated.</p>