//===========================================================//
/*
//方法1
#include "../Simple_ATL.h"
const IID IID_ISecond_ATL = {0x9DCE5206,0x9E76,0x40B1,{0x8E,0x26,0x8F,0x6D,0x1B,0x11,0x32,0x0B}};
const CLSID CLSID_Second_ATL = {0x68EDEE47,0x9E4D,0x4946,{0x92,0xE6,0xBA,0xC1,0xA7,0x9A,0x4E,0x05}};
*/
//方法2
#import "../debug/Simple_ATL.dll" no_namespace
void CTestATL2Dlg::OnBtnTestatl2()
{
HRESULT hr;
hr = CoInitialize(NULL);
/*
//方法1 : 直接创建,然后调用。
ISecond_ATL * ISecondATL = NULL;
if(FAILED(hr)) return;
hr = CoCreateInstance( CLSID_Second_ATL,
NULL,
CLSCTX_INPROC_SERVER,
IID_ISecond_ATL,
(void**)&ISecondATL);
if(SUCCEEDED(hr) && ISecondATL)
{
long lNum1 = 10 ,lNum2 = 20 ,lNum3 = 30,lReValue = 0;
hr = ISecondATL->CalculateSum(lNum1,lNum2,lNum3,&lReValue);
if(SUCCEEDED(hr))
{
CString str;
str.Format("lReValue = %d ",lReValue);
AfxMessageBox(str);
}
}
if (REGDB_E_CLASSNOTREG == hr)
{
AfxMessageBox("A specified class is not registered in the registration database");
}
else if (CLASS_E_NOAGGREGATION == hr)
{
AfxMessageBox("This class cannot be created as part of an aggregate");
}
else if(E_NOINTERFACE == hr)
{
AfxMessageBox("The specified class does not implement the requested interface");
}
else
{
//AfxMessageBox("succeeded calling");
}
ISecondATL->Release();
*/
//方法2 : 通过智能指针调用。
CLSID guid;
ISecond_ATLPtr pSencond;
IIDFromString(L"{68edee47-9e4d-4946-92e6-bac1a79a4e05}", &guid);
hr = pSencond.CreateInstance(guid);
if(SUCCEEDED(hr))
{
long lNum1 = 10 , lNum2 = 20 , lNum3 = 30 ,lReValue = 0;
pSencond->CalculateSum(lNum1,lNum2,lNum3,&lReValue) ;
CString str;
str.Format("Revalue = %d ",lReValue);
AfxMessageBox(str);
}
//此调用是必须的,否则pSencond会出错
if(pSencond)
{
pSencond = NULL;
}
CoUninitialize();
}
区别:
方法1:
直接创建,然后调用,在结束前必须调用Release()以释放组件。
方法2:
通过智能指针调用,在结束前不能调用Release()以释放组件,但是
需要pUnknow = NULL,否则,会对组件释放两次(抛出异常)。