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

I registered a COM component. And I want to call him.

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 clsid successfully, but where can I get iid?

I used the OLE VIEWER search 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 with an error.

+7
source share
3 answers

In your COM class, some interfaces are implemented, and each interface has its own IID . Therefore, you need to get it from the implementation of the COM component. This is your code, and you must provide an identifier that determines exactly which interface you are requesting.

Some COM classes implement a well-known interface, especially. IDispatch , whose identifier is IID_IDispatch , or __uuidof(IDispatch) .

UPD Since you find that the Isesoft interface is of 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 for C ++ code, you will need to import definitions, which are usually either two:

  • an additional vendor SDK includes, for example, #include "isesoft\sdk.h"
  • or #import "libid:..." with type library identifier (namespace and other attributes are used)

If you have HRESULT codes indicating malfunctions, be sure to post the values.

+6
source

You need to know the interface you want on your object, call it IMyInterface .

 IMyInterface* pItf = NULL; hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IMyInterface, (void**)&pItf); 
0
source

Finding the IID of the interface is not enough, you also need the LIBID for typelib, as Roman pointed out. Since you know the interface IID, open regedit and type Computer \ HKEY_CLSSES_ROOT \ Interface {F3F54BC2-D6D1-4A85-B943-16287ECEA64C} \ TypeLib, use a backslash. You will get a LIBID, suppose the value is {6F88B941-D87E-4B5E-BAE2-01cc21900dd8}. In your program, add the #import "libid:6F88B941-D87E-4B5E-BAE2-01cc21900dd8" operator #import "libid:6F88B941-D87E-4B5E-BAE2-01cc21900dd8" , there are no curly braces. Create a project, then, usually in the x64 \ Release or x64 \ Debug folder, you will find the se.tlh file, named after the server side of ProgID. This file contains announcements and IIDs of all open interfaces, and, hopefully, IceSoft interface (by the way, a funny name). You also looked in the regedit or oleview Imysoft interface because this would be the common name of the default CoClass dispatch interface with ProgID se.mysoft.

Now change your code:

 #import "libid:6F88B941-D87E-4B5E-BAE2-01cc21900dd8"//replace correct LIBID … later: CLSID clsid; RIID iid; CComPtr<IceSoft> pIceSoft; // no * HRESULT hr = CLSIDFromProgID(OLESTR("se.mysoft"),&clsid); hr = IIDFromString(OLESTR("{F3F54BC2-D6D1-4A85-B943-16287ECEA64C}"), &iid); HRESULT hr1 = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, (void **)&pIceSoft); 

One final note: since your hr1 returns an error that it should not do, I assume that something is wrong with the CLSID. The error code for hr1 will be 800401F3 (CO_E_CLASSSTRING: invalid class string). Get the value from the CLSIDFromProgID(OLESTR("se.mysoft"),&clsid); and in regedit type Computer \ HKEY_CLASSES_ROOT \ se.mysoft to compare it with the CLSID subkey value.

However, with your code you only get a useless IDispatch pointer, because that is what you are asking for.

0
source

Source: https://habr.com/ru/post/1500280/


All Articles