开发者

How to raise Exceptions in a COM Server?

开发者 https://www.devze.com 2023-02-24 08:43 出处:网络
How do I raise exceptions in a COM server to be consumed be a COM client? My serve开发者_StackOverflow社区r and client are written in Delphi if that makes any difference? Basically I have a background

How do I raise exceptions in a COM server to be consumed be a COM client? My serve开发者_StackOverflow社区r and client are written in Delphi if that makes any difference? Basically I have a background thread checking various things in my server. When it is not happy, it should stop whatever the server is doing, and raise an exception. This exception needs to be caught by the client. Not sure how to handle it though.


The simple answer is that you don't. Exceptions are not part of the COM interop specification and so should not cross module boundaries. You indicate errors with status codes.

Catch the exception just before your method exits (in each COM interface method). Turn the result into an error code. If you use an HRESULT then you can consume it as safecall and get the Delphi compiler to re-raise an exception on the other side of the fence.


The always knowledgeable Rob Kennedy points out in a comment that safecall can be used "both sides of the fence" which makes life even easier than outlined above.

I'm not an expert on this at all (hence the need to be educated by Rob's comment). However, the documentation indicates that when you are implementing safecall routines you should override TObject.SafeCallException to convert exceptions into HRESULT error codes. Some of the standard COM related classes provide overriden versions but you may need to add some of your own logic to the mix. The compiler writes implicit Try/Except blocks in safecall routines that handle exceptions by calling this virtual function.

0

精彩评论

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