Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过C中的COM互操作引发正向VB样式错误代码#_C#_.net_Error Handling_Vb6_Com Interop - Fatal编程技术网

C# 通过C中的COM互操作引发正向VB样式错误代码#

C# 通过C中的COM互操作引发正向VB样式错误代码#,c#,.net,error-handling,vb6,com-interop,C#,.net,Error Handling,Vb6,Com Interop,我在VB6中创建了一个基本库,它公开了许多应用程序中使用的标准COM接口。 这还暴露了许多错误代码常量,与Err.Raise一起使用以指示某些情况 Public Enum IOErrors IOErrorBase = 45000 IOErrorConnectionFailed IOErrorAuthFailed IOErrorNotConnected IOErrorInvalidPortDirection IOErrorGettingValue IOErrorNoVa

我在VB6中创建了一个基本库,它公开了许多应用程序中使用的标准COM接口。 这还暴露了许多错误代码常量,与
Err.Raise
一起使用以指示某些情况

Public Enum IOErrors
  IOErrorBase = 45000
  IOErrorConnectionFailed
  IOErrorAuthFailed
  IOErrorNotConnected
  IOErrorInvalidPortDirection
  IOErrorGettingValue
  IOErrorNoValueYet
End Enum
10年后,我们将创建实现相同接口集的C#对象,并希望以调用应用程序能够识别的方式抛出异常

我只能找到两个相关的类,
Win32Exception
comeexception

抛出
Win32Exception((int)IOErrors.IOErrorConnectionFailed,“Connect failed”)
会正确返回消息,但错误代码会被忽略,
Err.Number
&H80004005

抛出
COMException(“Connect failed”,IOErrors.IOErrorConnectionFailed)
会导致调用应用程序中未检测到错误,可能是因为错误代码不是
HRESULT
且为正,表示成功

TL;博士 如何从C抛出异常,以便COM interop将其转换为上面识别的(肯定的)错误代码之一?

将“肯定的”VB样式的错误编号转换为
HRESULT
s,具有“失败”严重性和
设施控制
/
0xA
,即
0x800AAFC9

您可以使用以下方法获得合适的
HRESULT

int HResult = (int)(0x800A0000 | (int)errorCode);
然后,可以使用普通的
COMException
,或者通过抛出自己的
COMException
子类,将其回调到调用进程:

/// <summary>
/// Exception that returns an ICIO error wrapped in an exception.
/// </summary>
internal class ICIOErrorException : COMException {
    internal ICIOErrorException(ICIO.IOErrors errorCode, string message)
        : base(message) {
        this.HResult = (int)(0x800A0000 | (int)errorCode);
    }
}
//
///返回封装在异常中的ICO错误的异常。
/// 
内部类ICIOErrorException:COMException{
内部ICIOErrorException(ICIO.IOErrors错误代码,字符串消息)
:base(消息){
this.HResult=(int)(0x800A0000 |(int)错误代码);
}
}
相关问题: