Exception from HRESULT when using COM server and ISupportErrorInfo

  • Thread starter Thread starter Kimmo Laine
  • Start date Start date
K

Kimmo Laine

Hi,

we have a (unmanaged) ATL COM server, which implements two interface:
ITestInterface1 and ITestInterface2. Both of these interfaces also support
the ISupportErrorInfo-interface.

Interface implementation is divided into two class: CTestInterface2, which
implements the ITestInterfac2, and CTestInterface1, which implements
ITestInterface1 AND ITestInterface2.

ITestInterface1 has only 1 method which is implemented like this:

// IDS_TESTSTRING_1 is defined in stringtable and it says "Error in
ShowLong"
// Methodname: : ShowLong
HRESULT hRes = Error( IDS_TESTSTRING_1 );
return hRes;

ITestInterface2 has also only 1 method and it is implemented like this:

// IDS_TESTSTRING_2 is defined in stringtable and it says "Error in
ShowChar"
// Methodname : ShowChar
HRESULT hRes = Error( IDS_TESTSTRING_2 );;
return hRes;


Now, when i code, in VB 6, something like this

Dim my1 As ATLSERVERLib.TestInterface1
Dim my2 As ATLSERVERLib.TestInterface2
' . . .
Set my1 = New ATLSERVERLib.TestInterface1
Set my2 = my1
my2.ShowChar

i get a nice exception which says "Error in ShowChar".

But when i do the same in C#

private ATLSERVERLib.TestInterface1 my1;
private ATLSERVERLib.TestInterface2 my2;
// . . .
my1 = new ATLSERVERLib.TestInterface1();
my2 = (ATLSERVERLib.TestInterface2)ti1;
my2.ShowChar();

i get a exception which says: "Exception from HRESULT: 0x80041172."

Any ideas? If you like, i can post the sourcecode for all of these projects.


thx

Kimmo Laine
 
Hi Kimmo,

What exatcly does the Error() function do in the ATL COM? I remember from my
ATL experience that raising errors properly from a COM component is rather
tricky.

Here's the code I used in one of my ATL COMs:

CComError::CComError(HRESULT hr, const IID& iidInterface, UINT
unStringTableID):
_com_error(hr)
{
HRESULT hrInternal;
CComBSTR bstrErrorMsg;

bstrErrorMsg.LoadString(unStringTableID);

CComPtr<ICreateErrorInfo> pICEI;
if (hrInternal = SUCCEEDED(CreateErrorInfo(&pICEI)))
{
CComPtr<IErrorInfo> pErrorInfo;
pICEI->SetGUID(iidInterface);
pICEI->SetDescription(_bstr_t(bstrErrorMsg));
if (SUCCEEDED(hrInternal = pICEI->QueryInterface(IID_IErrorInfo,
(void**)&pErrorInfo)))
{
this->_com_error::~_com_error();
this->_com_error::_com_error(hr, pErrorInfo, true);
}
else
{
throw _com_error(hrInternal);
}
}
else
{
throw _com_error(hrInternal);
}
}

P.S. Let's move our discussion to the
microsoft.public.dotnet.framework.interop newsgroup not to irritate the
subscribers with scary ATL C++ code and COM Interop details.
 
Back
Top