Search code examples
c#cominterophresult

How do I get HRESULT returns from libraries imported to c# with tlbimp.exe?


I am using the GMFBridge directshow filter from c# and the import library seems to discard the HRESULTs. i.e

[id(5), helpstring("Create render filters in empty render graph")]
HRESULT CreateRenderGraph(
  [in] IUnknown* pSourceGraphSinkFilter,
  [in] IUnknown* pRenderGraph,
  [out, retval] IUnknown** pRenderGraphSourceFilter);

[id(6), helpstring("Connect two graphs")]
HRESULT BridgeGraphs(
  [in] IUnknown* pSourceGraphSinkFilter,
  [in] IUnknown* pRenderGraphSourceFilter);

is translated (by midl.exe and tlbimp.exe) into:

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(5)]
object CreateRenderGraph([In, MarshalAs(UnmanagedType.IUnknown)] object pSourceGraphSinkFilter, [In, MarshalAs(UnmanagedType.IUnknown)] object pRenderGraph);

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(6)]
void BridgeGraphs([In, MarshalAs(UnmanagedType.IUnknown)] object pSourceGraphSinkFilter, [In, MarshalAs(UnmanagedType.IUnknown)] object pRenderGraphSourceFilter);

Other COM libraries I have imported translate returned values as out parameters and preserve the HRESULT return value of the original COM method. Why does this library behave differently? Is it possible to force the other behaviour?

Thanks, Andy


Solution

  • The .NET convention is to turn [out, retval] into a C# return value, and turn failure HRESULTs into COMException objects, with the HRESULT in the ErrorCode property. I think you're stuck if you want to see non-error HRESULTs.

    I'd be interested to see the IDL declarations of the methods that produced raw HRESULTs after translation - the example in your question is what I'd expect to see normally.