加载接口失败
Loading an interface fails
我想使用COM函数:CreateInstance
http://msdn.microsoft.com/en-us/library/k2cy7zfz%28v=vs.80%29.aspx
IPointer p=NULL;
HRESULT hr=p.CreateInstance(xxx);
但是我没有xxx的CLSID
,我只知道它的接口名称ISubPointer
当我用oleview
查看文件时,我可以在tlb文件中看到它的接口描述。如何使用CreateInstance
呢?
有两种方法:
1: a ClassFactory
,and
第二个:创建指针的辅助函数
我发现了这个:
int main()
{
IMath* pIMath;
HRESULT hr;
// 1. Initialize COM Library
CoInitialize(NULL);
// 2. Call CoCreateInstance to get the IMath interface pointer
hr = CoCreateInstance ( __uuidof(CMathComp), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMath), (void**) &pIMath );
if ( FAILED(hr) )
{
return 0;
}
// 3. Call the interface functions
int sum = pIMath->Add(1, 3);
printf("Sum = %d n", sum);
int sub = pIMath->Sub(4, 3);
printf("Sub = %d n", sub);
// 4. Release the interface pointer if you are done
pIMath->Release();
// 5. Un-Initialize COM Library
CoUninitialize();
return 0;
}
另见MSDN:
HRESULT CoCreateInstance(
_In_ REFCLSID rclsid,
_In_ LPUNKNOWN pUnkOuter,
_In_ DWORD dwClsContext,
_In_ REFIID riid,
_Out_ LPVOID *ppv
);
如果您可以从OLEVIEW收集CLSID
,请使用它,否则必须有关于此的文档。如果不暴露list CLSID
,就无法交付组件。
您有两个选项来获取您想要创建的对象的类ID。您可以使用OLE Viewer来生成头文件,也可以使用#import
指令直接将类型库导入源文件。您所引用的CreateInstance
函数是_com_ptr_t
的非静态成员,并且要求您使用它的一个实例。
下面的例子应该能帮到你。
#include <comip.h> // _com_ptr_t
#import "tlbname.tlb" // Change to the name of your type library
int main()
{
CoInitialize(NULL);
::_com_ptr_t<ISubPointer> ptr;
// CoISubPointer is the class ID specified in the type library
// you will need to change the name accordingly.
ptr.CreateInstance(__uuid(CoISubPointer), NULL, CLSCTX_INPROC_SERVER);
CoUninitialize();
return 0;
}
当main()
完成后,ptr
将自动释放它对ISubPointer
对象的引用。
不知道类ID就不能创建COM对象。我建议阅读本文中的COM基础知识http://www.codeproject.com/Articles/633/Introduction-to-COM-What-It-Is-and-How-to-Use-It
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 为什么导出的接口库find_dependency (GSL) 失败
- MFC功能区接口在本地化应用程序时发出断言失败
- 错误:(E112) 获取接口失败:端口未绑定 - SystemC
- 加载接口失败
- 为什么QueryInterface()会失败,当接口确实在Windows中实现并具有内置编组器时