C#/VBA COM互操作中的错误处理 [英] Error Handling in C# / VBA COM Interop

查看:64
本文介绍了C#/VBA COM互操作中的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel VBA调用的C#DLL,我已经通过COM可调用包装程序/COM Interop公开了它.我希望能够将C#代码中发生的所有异常传递到VBA客户端上.有建议的方法来做到这一点吗?谢谢.

I have a C# DLL being called from Excel VBA which I have exposed via a COM Callable Wrapper / COM Interop. I want to be able to pass any exceptions which occur in the C# code up onto the VBA client. Are there any recommended approaches to doing this ? Thanks.

推荐答案

可以创建自己的异常,这些异常将HRESULT错误代码传达回VBA或其他基于COM的调用者.这是一个使用E_FAIL的示例:

It is possible to create your own exceptions that communicate HRESULT error codes back to VBA or other COM based callers. Here's an example that uses E_FAIL:

public class ComMessageException : Exception
{
    public ComMessageException(string message)
        :base(message)
    {
        HResult = unchecked((int)0x80004005);
    }
}

这是MSDN文章描述该过程.

这应该为您提供与VB6中相同的VBA错误支持,它将向用户显示您选择的错误消息以及您选择的HRESULT.

This should give you the same VBA error support you had in VB6, it will display the error message of your choice to the user along with the HRESULT that you choose.

这篇关于C#/VBA COM互操作中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆