如何使用CoCreateInstance()获取com对象? [英] How to use CoCreateInstance() to get a com object?

查看:2770
本文介绍了如何使用CoCreateInstance()获取com对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册了一个COM组件,我想呼叫它。

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

我可以得到clsid成功,但在哪里可以得到iid?



我使用OLE VIEWER查找界面:

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

然后我更改了我的代码:

  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);

但hr1返回失败。

解决方案

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



一些COM类实现了众所周知的接口,esp。 IDispatch ,其标识符为 IID_IDispatch __ uuidof(IDispatch)



UPD。因为你发现感兴趣的接口是 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);

要获得 Isesoft IID_Isesoft __ uuidof(Isesoft)可用于C ++代码,您需要导入定义,通常是两个: / p>


  • 其他供应商SDK包括例如 #includeisesoft\sdk.h

  • #importlibid:... 类型库标识符属性适用)



当您拥有指示失败的 HRESULT 代码时,后的值。


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.

解决方案

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.

这篇关于如何使用CoCreateInstance()获取com对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆