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.
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.
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.
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.
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:
#include "isesoft\sdk.h"
#import "libid:..."
with type library identifier (namespace and other attributes apply)When you have HRESULT
codes indicating failures, make sure to post the values.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With