如何从 COM 方法获取 HRESULT 而不是异常

问题描述 投票:0回答:1

我在 C# 中使用 IAMCameraControl.GetRange 方法。该描述没有提及任何异常。仅关于返回

HRESULT
错误值。但在我的代码中出现了
COMException
,其中指定了
HRESULT
。 为什么抛出异常而不是返回错误?怎么解决?

[ComVisible(true)]
[ComImport]
[Guid("C6E13370-30AC-11d0-A18C-00A0C9118956")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAMCameraControl
{
    int GetRange([In] CameraControlProperty property, [In][Out] ref int pMin, [In][Out] ref int pMax, [In][Out] ref int pSteppingDelta, [In][Out] ref int pDefault, [In][Out] ref int pCapsFlag);
    int Set([In] CameraControlProperty property, [In] int lValue, [In] int flags);
    int Get([In] CameraControlProperty property, [In][Out] ref int lValue, [In][Out] ref int flags);
}
c# exception com interop hresult
1个回答
0
投票

“为什么抛出异常而不是返回错误?”

我猜这是因为在 .NET echosystem 中使用异常进行错误处理很常见。

“如何解决?”

COMException
包含具有HResult
值的
HRESULT
属性。

因此,您应该像往常一样在 .NET 中捕获异常,然后使用此属性来确定错误的性质,如 COM 文档中所述。

© www.soinside.com 2019 - 2024. All rights reserved.