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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 +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.
Thanks Aamir, but - where is - IS8Simulation declared? How can I call methods from that interface, that contain parameters such as BSTR (illustrated above)
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.
I got the following compiler errors: IS8Simulation' undeclared (first use this function) pSim' undeclared (first use this function) `__uuidof' undeclared (first use this function)
|
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?

Comments

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)

2 Comments

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, thanks again!!! :) I'll attempt the above, and will post my finding - you've been a big help wow!
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.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.