(preliminary note: I'm not yet fully up to speed with the whole 'interop' thing...)
When using a COM library from within .NET, all HRESULT
methods are wrapped into something that throws when the return code is not SUCCEEDED.
//ATL magic exluded
class C {
HRESULT foo(){ return E_FAIL; }
};
// usage code:
if( SUCCEEDED( c.foo() ) ) {
// success code
} else {
// failure code
}
The .NET counterpart of this code reads:
try {
c.foo();
// success code
} catch ( Exception e ) {
// failure code
}
Is there a way to access the COM return code directly in .NET, so that no exception handling is needed?
[PreserveSig]
is the attribute that is used to say if you want HRESULTS converted to exceptions or not. It should also be noted that you generally want to use exceptions. HRESULTS only exist because not all languages handle exceptions the same way, so they needed a non-exception-based mechanism to report exceptions. Once you're inside your own language the HRESULT should (ideally) be converted to your language's native exception mechanism. – Bisque