2

基于这个 SO 答案:Catching COMException specific Error Code,我想知道,如果我只需要查看异常的特定部分,则可以跨操作系统和多个版本的 OL 正确处理 COMExceptions。例如,

private const uint HRESULT_OPERATIONABORTED = 0x80004004;

// ...

    try {
        // something that could throw COMExceptions
    } catch (System.Runtime.InteropServices.COMException e) {

        switch ((uint)e.ErrorCode) {

            case HRESULT_OPERATIONABORTED:
                break;

            default:
                break;
        }
    }

这是否足够跨平台,还是只需要考虑错误代码的一部分?

编辑 - 澄清一下,我的确切问题是比较(uint)e.ErrorCode是否0x80004004过于具体(也就是说,无论 OS/OL 如何,我是否总是得到0x80004004这个特定错误),或者这是否是做事的正确方法.

4

2 回答 2

0

You have little to fear as far as cross-platform compat goes, COM only runs on Windows. Similarly, the error code is a well defined one. You can look up the standard COM error codes in the WinError.h SDK header file. It is E_ABORT. I'd recommend you actually use that identifier.

You'll find this header in c:\program files\microsoft sdks\windows\v6.0\include. It is v7.0 for VS2010.

于 2011-05-23T23:19:20.810 回答
0

您可能还需要考虑捕获诸如 System.OutOfMemoryException 和其他由 COM 对象返回的 HRESULT 导致的异常。并非所有失败的 HRESULT 都会导致 COMException。

对于我见过的大多数 MAPI 错误,错误代码与标准 C MAPI 头文件中声明的代码没有什么不同,所以我认为 switch 语句会很好。换言之,该方法的兼容性不应低于 C 编写的 MAPI 客户端应用程序。

于 2011-05-23T22:51:44.963 回答