How do you use ruby's WIN32OLE to get an instance of a non "IDispatch" interface?

571 Views Asked by At

I want to access the CUIAutomation object via Ruby. OLE/COM object viewer reports the following details:

[
  uuid(FF48DBA4-60EF-4201-AA87-54103EEF594E),
  version(1.0),
  helpstring("The Central Class for UIAutomation")
]
coclass CUIAutomation {
    [default] interface IUIAutomation;
};

I tried accessing it using the UUID

 WIN32OLE.new('{FF48DBA4-60EF-4201-AA87-54103EEF594E}')
WIN32OLERuntimeError: failed to create WIN32OLE object from `{FF48DBA4-60EF-4201-AA87-54103EEF594E}'
    HRESULT error code:0x80004002
      No such interface supported

Looking at the implementation of WIN3OLE.new it attempts to grab the IDispatch interface, but fails.

...
/* get IDispatch interface */
hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
                      &IID_IDispatch, &p);
...

In the Microsoft examples the code uses the IID_IUIAutomation interface directly

#include <uiautomation.h>

// CoInitialize must be called before calling this function, and the  
// caller must release the returned pointer when finished with it.
// 
HRESULT InitializeUIAutomation(IUIAutomation **ppAutomation)
{
    return CoCreateInstance(CLSID_CUIAutomation, NULL,
        CLSCTX_INPROC_SERVER, IID_IUIAutomation, 
        reinterpret_cast<void**>(ppAutomation));
}

Do I need to patch and rebuild Win32OLE? How else can I get an instance of CUIAutomation?

1

There are 1 best solutions below

1
On

The short answer is - you don't. Win32Ole requires the capabilities that IDispatch provides. If a COM object does not expose access to IDispatch then it is not compatible with Win32Ole. The only option would be to develop your own IDispatch wrapper using another language, like C/C++, and then you can use that with Win32Ole.