服务器端com组件的异常信息怎么被客户端捕获?
我想自己定义异常类,不用_com_error的,该怎么做呢?
或者派生一个?怎么定义呢?

解决方案 »

  1.   

    uses the IErrorInfo interface to report errors back to the client,  your object must also support the ISupportErrorInfo interface.
      

  2.   

    恩?我用atl做的类实现了ISUPPORTERRORINFO接口,调用Error("wrong!")方法抛出异常。
    然后在客户端捕获,这样客户端应该不用实现ISupportErrorInfo吧?
    但是我的客户端的IErrorInfo接口的指针总是得不到,为0,为什么呢????
    服务器端:
    if(!g_adoConn.ExecuteSQL(bufSQL))
    {return Error("Wrong", IID_IUserManager);
    } return S_OK;客户端代码:
       HRESULT hr;   try
       {
       // See if the object supports rich error info
       ISupportErrorInfo* pSEI = 0;
       hr = pUnk->QueryInterface( IID_ISupportErrorInfo, (void**) &pSEI );
       if (SUCCEEDED( hr ))
       {
          hr = pSEI->InterfaceSupportsErrorInfo( riid );
          if ( SUCCEEDED( hr ))
          {
             // Get the error info
             IErrorInfo* pEI;
             //下面这句总是错的!pEI = 0x000000
             if ( SUCCEEDED( GetErrorInfo( 0, &pEI )))
             {
                USES_CONVERSION;            BSTR bstrDescription = 0;
                BSTR bstrSource = 0;            pEI->GetDescription( &bstrDescription );
                pEI->GetSource( &bstrSource );            ::MessageBox(NULL, (char*)bstrDescription, "dlg", MB_OK);
                //cout << OLE2T( bstrSource ) << endl;            ::SysFreeString( bstrDescription );
                ::SysFreeString( bstrSource );            pEI->Release();
             }
          }
          pSEI->Release();
       }
       }
       catch(_com_error& e)
       {
       ::MessageBox(NULL, (char* )e.Description(), "&acute;í&Icirc;ó&pound;&iexcl;", MB_OK);
       }
       catch(...)
       {
       }