Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CoCreateInstance() to get a com object?

Tags:

c++

mingw

com

I had registered a COM component.And I want to call it.

CLSID clsid;
RIID iid;
HRESULT hr = CLSIDFromProgID(OLESTR("se.mysoft"),&clsid);
LPVOID *pRet;
HRESULT hr1 = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, pRet);

I can get the clsid successful, but where can I get the iid ?

I used OLE VIEWER find interface:

 [
 odl,
 uuid(F3F54BC2-D6D1-4A85-B943-16287ECEA64C),
 helpstring("Isesoft Interface"),
 dual,
 oleautomation
 ]
 interface Isesoft : IDispatch {

Then I changed my code:

CLSID clsid;
RIID iid;
IDispatch* pDispatch;
HRESULT hr = CLSIDFromProgID(OLESTR("se.mysoft"),&clsid);
HRESULT hr1 = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,  IID_IDispatch,(void **)&pDispatch);

But hr1 returned failed.

like image 752
CodeCat Avatar asked Sep 03 '13 10:09

CodeCat


People also ask

How do you make a COM object?

To create a COM object by using the COM class template The new project is displayed. Select Add New Item from the Project menu. The Add New Item dialog box is displayed. Select COM Class from the Templates list, and then click Add.

How do clients access COM objects?

A COM object exposes a set of interfaces that the COM client uses to access the services of the object. The client must communicate with a COM object through its interfaces. In the COM paradigm, the client never operates on a reference to an object, but only on a reference to one of its interfaces.

What does CoCreateInstance do?

The CoCreateInstance function provides a generic mechanism for creating objects. To understand CoCreateInstance, keep in mind that two COM objects can implement the same interface, and one object can implement two or more interfaces. Thus, a generic function that creates objects needs two pieces of information.


2 Answers

Your COM class implements some interfaces and each interface has its IID identifier. So you need to get it from your COM component implementation. It's your code and you are expected to provide the identifier which exactly specifies what interface you are requesting.

Some COM classes implement well known interface, esp. IDispatch, the identifier for which is IID_IDispatch, or __uuidof(IDispatch).

UPD. Since you found that the interface of interest is Isesoft, your code will be:

CLSID clsid;
RIID iid;
IDispatch* pDispatch;
HRESULT nResult1 = CLSIDFromProgID(OLESTR("se.mysoft"), &clsid);
HRESULT nResult2 = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
  IID_Isesoft, (void **) &pDispatch);

To get Isesoft and IID_Isesoft, __uuidof(Isesoft) available to C++ code, you will need to import the definitions, which is typically either of then two:

  • additional vendor SDK includes e.g. #include "isesoft\sdk.h"
  • or #import "libid:..." with type library identifier (namespace and other attributes apply)

When you have HRESULT codes indicating failures, make sure to post the values.

like image 73
Roman R. Avatar answered Sep 21 '22 01:09

Roman R.


I suppose that your CLSID is correct, since hr has a value of 0. From the extract of your idl.file, I conclude that the interface‘s ID is {F3F54BC2-D6D1-4A85-B943-16287ECEA64C} and its name Isesoft. Your present code provides a pointer to IDispatch and hr1 should be 0 if hr is 0. To get a raw COM pointer to this interface you must pass the CLSID and the IID as well as the address of a pointer to Isesoft.

Now change your code:

CLSID clsid;
RIID iid;
IseSoft* pIceSoft; 
HRESULT hr = CLSIDFromProgID(OLESTR("se.mysoft"),&clsid);
HRESULT hr2 = IIDFromString(OLESTR("{F3F54BC2-D6D1-4A85-B943-16287ECEA64C}"), &iid);
HRESULT hr3 = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, (void **)&pIseSoft);

A final remark: Since your hr1 returns a fail which it shouldn't, I presume that something is wrong with the CLSID. You can find the correct CLSID in the idl file from where you got the IID oft he interface.

Nevertheless, with your code all you would get is a useless IDispatch pointer, because that is what you are asking for.

like image 23
Dietrich Baumgarten Avatar answered Sep 19 '22 01:09

Dietrich Baumgarten