如何正确使用“出错时转到”不同的 HRESULT 值

How to correctly use On Error Goto for different HRESULT values

本文关键字:出错时转到 HRESULT 何正确 出错      更新时间:2023-10-16

我正在尝试读取文件。调用是从 VB 到 CPP dll。

这是我的示例代码片段

VB 调用

  Private Sub ReadFile(...)
    On Error GoTo Problem
    Dim errorString As String
    Sample.ReadFile basepath, filename, register, errorString
    GoTo Completed
Problem:
    WriteToLogFile basepath + filename + errorString //error string contains the formatted hresult message from cpp dll

我的CPP功能:

HRESULT ReadFile(...)
{
hr= actualread(...)
if(FAILED(hr)
{
return E_FAIL // 
}

如果我将E_FAIL更改为ERROR_FILE_NOT_FOUND则VB不会记录错误消息。

E_FAIL消息描述为"未指定错误"。这对用户没有多大帮助。

它记录E_POINTER,E_HANDLE等任何以E_开头而不是以ERROR_开头的内容

COM 指定HRESULT中的哪些值被视为错误,哪些值不被视为错误。引用 COM 错误代码的结构:

HRESULT 或 SCODE 中的高阶位指示返回值是表示成功还是失败。如果设置为 0,SEVERITY_SUCCESS,则该值表示成功。如果设置为 1,SEVERITY_ERROR,则表示失败。

E_常量设计为以这种方式使用,并具有适当的值以确保它们被视为错误。ERROR_常量不是,它们用于报告错误的不同约定。

正如WhozCraig在评论中指出的那样,您可以使用一个HRESULT_FROM_WIN32函数将Win32错误代码转换为HRESULT

解码错误

-2147220978样式数字是 32 位有符号整数,使用计算器转换为十六进制。

Windows 错误(

小数字)和 COM HResults(通常,但有例外,以 8 开头,如 0x80040154 中)在 WinError.h 中定义,但 8007nnnn 除外,您可以在其中查找它包含的窗口错误号。

作为一般规则,Windows 错误小于 65,535 (0xFFFF)。从0x80000001开始的错误是组件对象模型 (COM) HResults。从0xC0000001开始的错误是NTStatus的结果。从 0xD0000001 开始的错误也是 HResult 中返回的 NTStatus 值。

  • WinError - 从 Windows API 调用返回

  • HResult - 从 COM 调用返回

  • NTStatus - 从内核返回,某些 API 调用也会在以下情况下返回遇到内核错误。

NTStatus错误(通常但不总是以0xC0000022中的C开头)在NTStatus.h中定义。

.h文件是最好的来源,因为它包括错误的符号名称,可以提供诸如错误来源之类的线索。格式消息不会只为符号名称提供说明。

您可以通过下载平台 SDK(千兆字节)来获取这些文件

http://www.microsoft.com/en-us/download/details.aspx%3Fid%3D8279&sa=U&ei=w2IrULDDLsHFmAWbmIHoBg&ved=0CBwQFjAA&usg=AFQjCNHZn9-4f2NnuN9o3UWUsOF3wL7HBQ

如果您只想要这两个文件,我将它们放在我的 skydrive 上,这样我就可以在任何地方引用它们。

https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121

请注意,互联网错误(12,000 - 12,999)是Windows错误,但在wininet.h中指定,上面也有

其他 .h 文件中定义了错误。但99%都在上述三个中。

HResults和NTStatus代码的结构

HResults中的最高有效位和NTStatus中的两个最高有效位设置在错误上。因此,Hresults在错误时从8开始,NTStatus在错误时启动C。接下来的 14 或 15 位是保留的,有些指定了设施 - 错误所在的区域。这是读取十六进制时的第三个和第四个数字。EG 0xnn07nnnn - HResult 设施代码 7 是正常的 Windows 错误(从 COM 程序返回 - 因此它作为 HResult 返回)。设施代码在Winerror.h中定义用于HResults,NTStatus.h定义用于NTStatus代码。他们是不同的。

解码 0x8003nnnn 错误

设施代码为 3 的 HResult 表示 HResult 包含 OLE 结构化存储错误(0x0 到 0xff)。这些与 Dos 错误代码相同。这些似乎不在Windows的头文件中,代码列表在本文末尾。

解码 0x8004nnnn 错误

设施代码为 4 的 HResult 表示 HResult 包含 OLE 错误(0x0 到 0x1ff),而范围的其余部分(0x200 起)是特定于组件的错误,因此一个组件的 20e 与另一个组件的 20e 具有不同的含义。

这就是为什么错误来源对于0x80040200以上的错误特别重要的原因。

解码 0x8007nnnn 错误

设施代码为 7 的 HResult 表示 HResult 包含 Windows 的错误代码。您必须查找Windows的错误代码而不是HResult。

解码0x80070002。0x 表示它是一个十六进制数,8 表示错误,前 7 表示它是窗口错误,其余数字 2 是实际的 Windows 错误。

要查找错误,我们需要十进制格式。启动计算器(开始 - 所有程序 - 附件 - 计算器),然后选择视图菜单 - 科学,然后选择视图菜单 - 十六进制。输入 2。然后查看菜单 - 十进制。它会说 2。

启动命令提示符(开始 - 所有程序 - 附件 - 命令提示符)并键入

net helpmsg 2

它会说

The system cannot find the file specified.

或在 Winerror.h 中查找它

//
// MessageId: ERROR_FILE_NOT_FOUND
//
// MessageText:
//
// The system cannot find the file specified.
//
#define ERROR_FILE_NOT_FOUND             2L

解码 0x8019nnnn 错误

具有设施0x19的HResults是HTTP错误。低于 16,384 (0x4000) 的代码与 HTTP 错误相同,例如 HTTP 状态 404:请求的 URL 在服务器上不存在 0x80190194 (0x194 = 404)。代码 16,384 及更高版本特定于 BITS。

解码 0xDnnnnnnnnn 错误

HResults 开头0xD是一个 HResult,其中包含一个 NTStatus 值。只需将导线 D 转换为 C 并视为 NTStatus (Hresult = NTStatus OR 10000000)。

Dos 错误代码(对于 0x8003nnnn 错误)代码消息

01 Invalid function number 
02 File not found 
03 Path not found 
04 Too many open files (no handles left) 
05 Access denied 
06 Invalid handle 
07 Memory control blocks destroyed 
08 Insufficient memory 
09 Invalid memory block address 
0A Invalid environment 
0B Invalid format 
0C Invalid access mode (open mode is invalid) 
0D Invalid data 
0E Reserved 
0F Invalid drive specified 
10 Attempt to remove current directory 
11 Not same device 
12 No more files 
13 Attempt to write on a write-protected diskette 
14 Unknown unit 
15 Drive not ready 
16 Unknown command 
17 CRC error 
18 Bad request structure length 
19 Seek error 
1A Unknown media type 
1B Sector not found 
1C Printer out of paper 
1D Write fault 
1E Read fault 
1F General failure 
20 Sharing violation 
21 Lock violation 
22 Invalid disk change 
23 FCB unavailable 
24 Sharing buffer overflow 
25 Reserved 
26 Unable to complete file operation (DOS 4.x) 
27-31 Reserved 
32 Network request not supported 
33 Remote computer not listening 
34 Duplicate name on network 
35 Network name not found 
36 Network busy 
37 Network device no longer exists 
38 NetBIOS command limit exceeded 
39 Network adapter error 
3A Incorrect network response 
3B Unexpected network error 
3C Incompatible remote adapter 
3D Print queue full 
3E No space for print file 
3F Print file deleted 
40 Network name deleted 
41 Access denied 
42 Network device type incorrect 
43 Network name not found 
44 Network name limit exceeded 
45 NetBIOS session limit exceeded 
46 Temporarily paused 
47 Network request not accepted 
48 Print or disk redirection is paused 
49-4F Reserved 
50 File already exists 
51 Reserved 
52 Cannot make directory entry 
53 Fail on INT 24 
54 Too many redirections 
55 Duplicate redirection 
56 Invalid password 
57 Invalid parameter 
58 Network device fault 
59 Function not supported by network (DOS 4.x) 
5A Required system component not installed (DOS 4.x) 

设施代码

新台斯塔斯

设施 人力资源设施
Common status values 
0x0 Null 0x0 
Debugger 0x1 Rpc 0x1 
Rpc_runtime 0x2 Dispatch 0x2 
Rpc_stubs 0x3 Storage 0x3 
Io_error_code 0x4 Itf 0x4 
Various drivers 0x5-0xf Win32 0x7 
Ntwin32 0x7 Windows 0x8 
Ntsspi 0x9 Sspi 0x9 
Terminal_server 0xa Security 0x9 
Faciltiy_mui_error_code 0xb Control 0xa 
Usb_error_code 0x10 Cert 0xb 
Hid_error_code 0x11 Internet 0xc 
Firewire_error_code 0x12 Mediaserver 0xd 
Cluster_error_code 0x13 Msmq 0xe 
Acpi_error_code 0x14 Setupapi 0xf 
Sxs_error_code 0x15 Scard 0x10 
Transaction 0x19 Complus 0x11 
Commonlog 0x1a Aaf 0x12 
Video 0x1b Urt 0x13 
Filter_manager 0x1c Acs 0x14 
Monitor 0x1d Dplay 0x15 
Graphics_kernel 0x1e Umi 0x16 
Driver_framework 0x20 Sxs 0x17 
Fve_error_code 0x21 Windows_ce 0x18 
Fwp_error_code 0x22 Http 0x19 
Ndis_error_code 0x23 Usermode_commonlog 0x1a 
Hypervisor 0x35 Usermode_filter_manager 0x1f 
Ipsec 0x36 Backgroundcopy 0x20 
Maximum_value 0x37 Configuration 0x21 
  State_management 0x22 
  Metadirectory 0x23 
  Windowsupdate 0x24 
  Directoryservice 0x25 
  Graphics 0x26 
  Shell 0x27 
  Tpm_services 0x28 
  Tpm_software 0x29 
  Pla 0x30 
  Fve 0x31 
  Fwp 0x32 
  Winrm 0x33 
  Ndis 0x34 
  Usermode_hypervisor 0x35 
  Cmi 0x36 
  Windows_defender 0x50