atl 中错误支持的使用

    技术2022-05-11  55

    在atl 创建的组件中 增加错误处理  。

    Error(L"heelo ");

    ......

    在客户程序中

    HRESULT hr=pxxx->Add(10,9,&result); if( FAILED( hr ) ) // 如果发生了错误{ CComQIPtr < ISupportErrorInfo > spSEI = pxxx; // 组件是否提供了 ISupportErrorInfo 接口? if( spSEI ) // 如果支持,那么 {  hr = spSEI->InterfaceSupportsErrorInfo( IID_Ixxx ); // 是否支持 Ixxx 接口的错误处理?  if( SUCCEEDED( hr ) )  { // 支持,太好了。取出错误信息      CComQIPtr < IErrorInfo > spErrInfo;  // 声明 IErrorInfo 接口   hr = ::GetErrorInfo( 0, &spErrInfo ); // 取得接口   if( SUCCEEDED( hr ) )   {    CComBSTR bstrDes;    spErrInfo->GetDescription( &bstrDes ); // 取得错误描述

        USES_CONVERSION;    

        LPTSTR lp = OLE2T( bstrDes.m_str) ;

        cout<<lp<<endl;    //...... // 还可以取得其它的信息   }  } //pMath->Release(); //CoUninitialize(); return ; }} 


    最新回复(0)