我正在尝试使用VC ++ 2005中的COM DLL。我使用ATL创建了一个TestCOMlib.dll,创建了一个简单的接口ISimple并添加了一个属性(类型为LONG,名称为Property01)和一个方法(名称为Method01)。 / p>
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正确显示界面,在注册表中条目似乎很好。
我做错了什么?有什么想法吗?
此致
答案 0 :(得分: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(简单)有效。