开发者

Making a COM dll to be invoked from c#, HRESULT error handling?

I'm making a COM interface that c# will be using, however i am wondering how i am to check for errors for exception handling on the c# end. As at the moment i am just returning a HRESTULT or bool for most methods and then doing a Marshal.ThrowExceptionForHR but several things can go wrong in some of these methods 开发者_如何转开发and returning a E_FAIL just doesn't cut it.

What can i do in order to return more information? Can i make a HRESULT of my own?


I've never actually done this, but in theory you should be able to do it by supporting IErrorInfo. Marshal.GetExceptionForHR() has an overload that takes an IErrorInfo pointer.

By the way, you should not have to manually call Marshal.ThrowExceptionForHR() - if your method returns HRESULT and and [out] parameter for the return value you can declare it in .NET as returning the return value and set PreserveSig=false and the CLR will automatically throw an exception if the HRESULT indicates a failure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜