我正在尝试使用来自VC++ 2005的COM DLL。我用ATL创建了一个TestCOMlib.dll,创建了一个简单的接口ISimple,并添加了一个属性(输入LONG,name Property01)和一个方法(名称Method01)。
DLL似乎在系统中正确注册(我使用OleView检查条目)。
我创建了一个简单的MFC对话框应用程序来使用COM dll。我正在使用#import指令来合并来自类型库的信息。Visual为我创建了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正确地显示了接口,在注册表中,条目似乎很好。
我做错了什么?有什么想法吗?
问候
发布于 2011-02-23 00:27:55
这些智能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(简单)工作的原因。
https://stackoverflow.com/questions/5084734
复制相似问题