1

我正在尝试使用 VC++ 2005 中的 COM DLL。我使用 ATL 创建了一个 TestCOMlib.dll,创建了一个简单的接口 ISimple,并添加了一个属性(LONG 类型,名称 Property01)和一个方法(名称 Method01)。

DLL 似乎已在系统中正确注册(我使用 OleView 检查条目)。

我创建了一个简单的 MFC 对话框应用程序来使用 COM dll。我正在使用#import 指令来合并来自类型库的信息。Visual Studio 为我创建了 tlh 和 tli 文件。

然后我尝试获取 ISimple 接口,但我得到了错误 0x80040154。我在测试应用程序中运行的代码如下:

HRESULT hr = S_OK;
hr = CoInitialize(NULL);

ISimplePtr myRef(__uuidof(ISimple));

// Test prop and method
myRef->Property01 = 5;
LONG test = myRef->Property01;
LONG ret = myRef->Method01(_T("Test input"));
ret = myRef->Method01(NULL);

myRef = NULL;
CoUninitialize();

返回错误 0x80040154 的行是ISimplePtr myRef(__uuidof(ISimple))。OleView 正确显示界面,并且在注册表中的条目似乎很好。

我究竟做错了什么?任何的想法?

问候

4

1 回答 1

4

这些智能 COM 指针的基础类是 _com_ptr_t。您正在尝试使用此构造函数:

// Constructs a smart pointer given the CLSID of a coclass. This 
// function calls CoCreateInstance, by the member function
//  CreateInstance, to create a new COM object and then queries for 
// this smart pointer's interface type. If QueryInterface fails with 
// an E_NOINTERFACE error, a NULL smart pointer is constructed.
explicit _com_ptr_t( 
   const CLSID& clsid,  
   IUnknown* pOuter = NULL,  
   DWORD dwClsContext = CLSCTX_ALL 
);

关键是你必须传递coclass 的 CLSID,你传递的是接口的 IID。这就是 __uuidof(Simple) 起作用的原因。

于 2011-02-23T00:27:55.133 回答