C++ 如何使用 CoCreateInstance() 获取 com 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18590499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:03:19  来源:igfitidea点击:

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

c++commingw

提问by CodeCat

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

我已经注册了一个 COM 组件。我想调用它。

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 ?

我可以成功获取 clsid,但是我可以从哪里获取 iid ?

I used OLE VIEWER find interface:

我使用 OLE VIEWER 查找界面:

 [
 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.

但是hr1返回失败。

回答by Roman R.

Your COM class implements some interfaces and each interface has its IIDidentifier. 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.

您的 COM 类实现了一些接口,每个接口都有其IID标识符。所以你需要从你的 COM 组件实现中获取它。这是您的代码,您需要提供准确指定您请求的接口的标识符。

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

一些 COM 类实现了众所周知的接口,尤其是。IDispatch, 其标识符为IID_IDispatch, 或__uuidof(IDispatch)

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

更新。由于您发现感兴趣的接口是Isesoft,您的代码将是:

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 Isesoftand IID_Isesoft, __uuidof(Isesoft)available to C++ code, you will need to import the definitions, which is typically either of then two:

要获取可用于 C++ 代码的Isesoftand IID_Isesoft__uuidof(Isesoft)您需要导入定义,通常是以下两个之一:

  • additional vendor SDK includes e.g. #include "isesoft\sdk.h"
  • or #import "libid:..."with type library identifier (namespace and other attributes apply)
  • 其他供应商 SDK 包括例如 #include "isesoft\sdk.h"
  • #import "libid:..."使用类型库标识符(命名空间和其他属性适用)

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

当您有HRESULT指示失败的代码时,请确保发布这些值。

回答by Michael M.

You should know the interface you want on your object, let call it IMyInterface.

你应该知道你的对象上你想要的接口,让我们称之为IMyInterface

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

回答by Dietrich Baumgarten

I suppose that your CLSIDis correct, since hrhas 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 Icesoft. Your present code provides a pointer to IDispatchand hr1should be 0 if hris 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 Icesoft.

我想你CLSID是对的,因为hr它的值为 0。从你的 idl.file 的摘录中,我得出结论,接口的 ID 是 {F3F54BC2-D6D1-4A85-B943-16287ECEA64C} 和它的 name Icesoft。您当前的代码提供了一个指向0的指针,如果为 0 IDispatchhr1则应hr为 0。要获得指向此接口的原始 COM 指针,您必须传递 CLSID 和 IID 以及指向 的指针地址Icesoft

Now change your code:

现在更改您的代码:

CLSID clsid;
RIID iid;
IceSoft* 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 **)&pIceSoft);

A final remark: Since your hr1returns 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.

最后一句话:由于您hr1返回了一个不应该的失败,我认为 CLSID 有问题。您可以在从接口获取 IID 的 idl 文件中找到正确的 CLSID。

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

尽管如此,使用您的代码,您只会得到一个无用的IDispatch指针,因为这就是您所要求的。