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 E_INVALIDARG, though all the supplied arguments match the documented types.
The application type is EXE and it's registered in the Windows Registry with a CLSID and its type there is LocalServer.
The reason of my attempt relates to a similarly asked question.
I usually connect with some applications using the comtypes package of python, most of them succeed with the call to GetActiveObject(). However, when calling GetActiveObject() with the CLSID of this application I get an error
WindowsError: [Error -2147221021] Operation unavailable
The effective part of the code of the MFC Application is the following
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"), &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 **)&app_ptr);
if (SUCCEEDED(hr1))
{
hr2 = CreateItemMoniker(L"!", OLESTR("myappmnk"), &appmnk);
if (SUCCEEDED(hr2))
{
hr3 = GetRunningObjectTable(0, &rot);
if (SUCCEEDED(hr3))
{
hr = rot->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, app_ptr, appmnk, &dwrdptr);
rot->Release();
}
appmnk->Release();
}
app_ptr->Release();
}
}
CoUninitialize();
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 GetActiveObject.
Any help and any insight will be truly appreciated.