1

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

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Aaron
  • 2,823
  • 9
  • 44
  • 57

4 Answers4

6

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.

Aamir
  • 14,882
  • 6
  • 45
  • 69
  • In the example, both the uuidof(..) calls should be preceeded by double underscores. I added that but somehow those are lost in formatting ( I should have escaped those probably) – Aamir Jan 09 '09 at 07:22
  • Aamir +1 for a good post, but rather than commenting on your own posts you are better to edit them. Keeps it clear for those just scannng through posts without looking at comments. – SmacL Jan 09 '09 at 07:50
  • Thanks Aamir, but - where is - IS8Simulation declared? How can I call methods from that interface, that contain parameters such as BSTR (illustrated above) – Aaron Jan 09 '09 at 10:25
  • well... using Oleview you can see the typelib information of the interface. This will tell you that from which binary the interface is coming from. It is normally something like win32 = 'something.dll'. This is the dll/tlb etc. which normally contains the declaration of this interface. – Aamir Jan 09 '09 at 10:40
  • I got the following compiler errors: `IS8Simulation' undeclared (first use this function) `pSim' undeclared (first use this function) `__uuidof' undeclared (first use this function) – Aaron Jan 09 '09 at 10:59
  • Don't forget to Release http://msdn.microsoft.com/en-us/library/ms682317(VS.85).aspx the interface pointer! Otherwise you will have memory leaks. – Alessandro Jacopson Jan 10 '09 at 16:23
0

It's a little vague what the actual problem is. Some code would be helpful. But to take a guess, do you need to QueryInterface?

bmm6o
  • 6,187
  • 3
  • 28
  • 55
0
 IS8Simulation* pSim = NULL;
 hr = pUnk->QueryInterface(__uuidof(IS8Simulation), (void)&pSim);

I'll attempt the above, but were is IS8Simulation declared - please excuss my lack of COM understanding

Furthermore, how to call the method, below using plain C++:

HRESULT Open([in] BSTR FileName)
Aaron
  • 2,823
  • 9
  • 44
  • 57
  • once you have pointer to your interface i.e., pSim in above example, you can call its methods simply like.. BSTR bstrFileName = ::SysAllocString(fileName); hr = pSim->Open(bstrFileName); Remember to free the BSTR after using by calling ::SysFreeString() And yes, this is plain C++ – Aamir Jan 09 '09 at 10:23
  • Aamir, thanks again!!! :) I'll attempt the above, and will post my finding - you've been a big help wow! – Aaron Jan 09 '09 at 10:30
0

You probably want #import "something.dll". This will give you C++ declarations for types like IS8Simulation, similar to what #include "something.h" would do.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I'm in the process of trying to compile IDL FILE using MIDL Compiler. I found type declarations like "single" (which is VB) - is this normal? – Aaron Jan 10 '09 at 14:19