我正在尝试使用来自 VC++ 2005 的 COM DLL。我使用 ATL 创建了一个 TestCOMlib.dll,创建了一个简单的接口(interface) ISimple 并添加了一个属性(类型 LONG,名称 Property01)和一个方法(名称 Method01)。
DLL 似乎已在系统中正确注册(我使用 OleView 检查条目)。
我创建了一个简单的 MFC 对话框应用程序来使用 COM dll。我正在使用#import 指令合并来自类型库的信息。 Visual Studio 为我创建了 tlh 和 tli 文件。
然后我尝试获取 ISimple 接口(interface),但获取错误 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 正确显示界面并且在注册表中的条目似乎是好的。
我做错了什么?有什么想法吗?
问候
最佳答案
这些智能 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
);
关键是您必须传递组件类的CLSID,您传递的是接口(interface)的IID。这就是 __uuidof(Simple) 起作用的原因。
关于c++ - #import 导致 HRESULT 0x80040154 "Not registered class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5084734/