Search code examples
c++comoleview

Access a COM Interface method C++


Both:

  • CLSID
  • IID

Having specified the above, and using:

  • CoCreateInstance()

To returning a single uninitialised object of the class specified by the CLSID above.

How can I then access an Interface's method from C++? Without:

  • ATL
  • MFC
  • Just plain C++

Afterwards, I use CreateInstance()

I'm having trouble, using CreateInstance() - with the last parameter - ppv

Using oleview, I can see methods of the specified IIDabove IID above, such as:

interface IS8Simulation : IDispatch {
    HRESULT Open([in] BSTR FileName);
};

How can I then access the above? Examples/guidance - please

Regards


Solution

  • By doing a CoCreateInstance you get an interface pointer. Through QueryInterface(...) method you can get the interface pointer of some other interface easily. e.g.,

    
    IUnknown* pUnk = NULL;
    HRESULT hr = ::CoCreateInstance(clsid,NULL,CLSCTX_ALL,__uuidof(IUnknown),(void**)&pUnk);

    IS8Simulation* pSim = NULL; hr = pUnk->QueryInterface(__uuidof(IS8Simulation), (void**)&pSim);

    After doing this, you will get the pointer to IS8Simulation in pSim and through that you can call methods of that interface. Remember you need to provide a valid clsid in the CoCreateInstance call.