I have this method in my ATL project which exposes a COM object (snippet taken from my *.idl
file):
[id(1)] HRESULT Create([in, string] CHAR* location, [out] CerberusErrorDetails* details);
After registering my object and adding a reference to it from my managed C# code, it generates the following proxy stub C# code for this method:
[DispId(1)]
void Create(string location, out CerberusErrorDetails details);
There are a few problems with this stub for me which I have been unable to sort out. Firstly, it does not return an int
value (with respect to the returned HRESULT
) in managed code to tell me what error has actually occurred. Secondly, the method will throw an exception instead of returning the error code. Is there a way to have the function return an int
such that I can then parse the details
object if it does not return 0
, or are there any alternatives to get the behavior I want? Any help is greatly appreciated. If you need more details, feel free to ask and I'll update the question. Thank you!
Yes, it's possible to bypass the default exception translation behavior of the COM interop layer. However, you'll have to redefine your native COM interface and decorate all methods with the PreserveSig
attribute:
[ComImport]
[Guid("xxx-yyy-zzz")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ICOMInterfaceImp
{
[PreserveSig]
int Create(string location, out CerberusErrorDetails details);
}
You can then use this interface in your client:
ICOMInterfaceImp obj = (ICOMInterfaceImp)new CoClassImpl();
As an alternative, you may also use the updated type library importer (tlbimp.exe) from here and pass the /PreserveSig
flag.