我在 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);
}
“为什么抛出异常而不是返回错误?”
我猜这是因为在 .NET echosystem 中使用异常进行错误处理很常见。
“如何解决?”
COMException
类包含具有HResult
值的
HRESULT
属性。
因此,您应该像往常一样在 .NET 中捕获异常,然后使用此属性来确定错误的性质,如 COM 文档中所述。