Requirement:
1.创建myCom.dll,该COM只有一个组件,两个接口IGetRes--方法Hello(),IGetResEx--方法HelloEx();
2.在工程中导入组件或类型库
#import "组件所在目录myCom.dll" no_namespace 或
#import "类型库所在目录myCom.tlb"
using namespace MYCOM;
方式1
#import "类型库所在目录myCom.tlb"
using namespace MYCOM;
CoInitialize(NULL);
CLSID clsid;
CLSIDFromProgID(OLESTR("myCom.GetRes"), //
&clsid); //
CComPtr<IGetRes> pGetRes; // 智能指针
pGetRes.CoCreateInstance(clsid);
pGetRes->Hello();
pGetRes.Release(); // 小心哦,请看最后的“注意”
CoUninitialize();
CLSID clsid;
CLSIDFromProgID(OLESTR("myCom.GetRes"), //
&clsid); //
CComPtr<IGetRes> pGetRes; // 智能指针
pGetRes.CoCreateInstance(clsid);
pGetRes->Hello();
pGetRes.Release(); // 小心哦,请看最后的“注意”
CoUninitialize();
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(OLESTR("myCom.GetRes"),
&clsid);
IGetRes *ptr = NULL;
hr=CoCreateInstance(clsid,
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IGetRes),
(LPVOID*)&ptr);
ptr->Hello();
CoUninitialize();
CLSID clsid;
HRESULT hr = CLSIDFromProgID(OLESTR("myCom.GetRes"),
&clsid);
IGetRes *ptr = NULL;
hr=CoCreateInstance(clsid,
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IGetRes),
(LPVOID*)&ptr);
ptr->Hello();
CoUninitialize();
CoInitialize(NULL);
HRESULT hr;
CLSID clsid;
hr = CLSIDFromProgID(OLESTR("myCom.GetRes"),
&clsid);
IGetRes* ptr = NULL;
IGetResEx* ptrEx = NULL;
// 使用CoCreateClassObject创建一个组件(特别是mutilThreads)的多个对象的时候,效率更高.
IClassFactory* p_classfactory = NULL;
hr=CoGetClassObject(clsid,
CLSCTX_INPROC_SERVER,
NULL,
IID_IClassFactory,
(LPVOID*)&p_classfactory);
p_classfactory->CreateInstance(NULL,
__uuidof(IGetRes),
(LPVOID*)&ptr);
p_classfactory->CreateInstance(NULL,
__uuidof(IGetResEx),
(LPVOID*)&ptrEx);
ptr->Hello();
ptrEx->HelloEx();
CoUninitialize();
HRESULT hr;
CLSID clsid;
hr = CLSIDFromProgID(OLESTR("myCom.GetRes"),
&clsid);
IGetRes* ptr = NULL;
IGetResEx* ptrEx = NULL;
// 使用CoCreateClassObject创建一个组件(特别是mutilThreads)的多个对象的时候,效率更高.
IClassFactory* p_classfactory = NULL;
hr=CoGetClassObject(clsid,
CLSCTX_INPROC_SERVER,
NULL,
IID_IClassFactory,
(LPVOID*)&p_classfactory);
p_classfactory->CreateInstance(NULL,
__uuidof(IGetRes),
(LPVOID*)&ptr);
p_classfactory->CreateInstance(NULL,
__uuidof(IGetResEx),
(LPVOID*)&ptrEx);
ptr->Hello();
ptrEx->HelloEx();
CoUninitialize();
// 定义一个函数指针类型
typedef HRESULT (__stdcall * pfnHello)(REFCLSID,REFIID,void**);
pfnHello fnHello = NULL; // 创建一个函数指针变量
// 将COM组件DLL像一般DLL一样加载,得到DLL句柄
HINSTANCE hdllInst = LoadLibrary("*:\*.dll");
// 判断DLL加载是否成功
if (NULL != hdllInst)
{
// 取得相应的函数地址
fnHello = (pfnHello)GetProcAddress(hdllInst,
"DllGetClassObject");
if (NULL != fnHello)
{
// 得到类工厂接口指针
IClassFactory* pcf = NULL;
HRESULT hr = (fnHello)(CLSID_GetRes,
IID_IClassFactory,
(void**)&pcf);
if (SUCCEEDED(hr) &&
(pcf != NULL))
{
// 得到具体方法接口指针
IGetRes* pGetRes = NULL;
hr = pcf->CreateInstance(NULL,
IID_IFoo,
(void**)&pGetRes);
if (SUCCEEDED(hr) &&
(pGetRes != NULL))
{
pGetRes->Hello();
pGetRes->Release();
}
pcf->Release();
}
}
FreeLibrary(hdllInst);
}
typedef HRESULT (__stdcall * pfnHello)(REFCLSID,REFIID,void**);
pfnHello fnHello = NULL; // 创建一个函数指针变量
// 将COM组件DLL像一般DLL一样加载,得到DLL句柄
HINSTANCE hdllInst = LoadLibrary("*:\*.dll");
// 判断DLL加载是否成功
if (NULL != hdllInst)
{
// 取得相应的函数地址
fnHello = (pfnHello)GetProcAddress(hdllInst,
"DllGetClassObject");
if (NULL != fnHello)
{
// 得到类工厂接口指针
IClassFactory* pcf = NULL;
HRESULT hr = (fnHello)(CLSID_GetRes,
IID_IClassFactory,
(void**)&pcf);
if (SUCCEEDED(hr) &&
(pcf != NULL))
{
// 得到具体方法接口指针
IGetRes* pGetRes = NULL;
hr = pcf->CreateInstance(NULL,
IID_IFoo,
(void**)&pGetRes);
if (SUCCEEDED(hr) &&
(pGetRes != NULL))
{
pGetRes->Hello();
pGetRes->Release();
}
pcf->Release();
}
}
FreeLibrary(hdllInst);
}
CoInitialize(NULL);
CGetRes getRest;
if (getRest.CreateDispatch("myCom.GetRes") != 0)
{
getRest.Hello();
getRest.ReleaseDispatch();
}
CoUninitialize();
------------------------------------ 注意------------------------------------CGetRes getRest;
if (getRest.CreateDispatch("myCom.GetRes") != 0)
{
getRest.Hello();
getRest.ReleaseDispatch();
}
CoUninitialize();
代码
CoInitialize(NULL);
CLSID clsid;
CLSIDFromProgID(OLESTR("myCom.GetRes"),
&clsid);
CComPtr<IGetRes> pGetRes; // 智能指针
pGetRes.CoCreateInstance(clsid);
pGetRes->Hello();
// pGetRes.Release(); // 将其注释掉
CoUninitialize();
CLSID clsid;
CLSIDFromProgID(OLESTR("myCom.GetRes"),
&clsid);
CComPtr<IGetRes> pGetRes; // 智能指针
pGetRes.CoCreateInstance(clsid);
pGetRes->Hello();
// pGetRes.Release(); // 将其注释掉
CoUninitialize();
以上就是COM的5中方法,当然具体怎么使用还是要根据具体情况而定。
来源:https://www.cnblogs.com/rogee/archive/2010/10/03/1841680.html