ATL COM 组件 调用

//===========================================================//

/*

//方法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();

*/

  //方法: 通过智能指针调用。

  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,否则,会对组件释放两次(抛出异常)。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值