如果在托管平台调用非托管的COM组件,而在组件之中又引发了错误,那么COM组件会以什么样的形式把错误返回给托管代码呢?
答案就是HRESULT。
相信许多朋友对该HRESULT码都不陌生,电脑中经常出现一堆莫名其妙的报错,就会给你弹出一堆错误编码,而那个编码,就是HRESULT。
所以HRESULT到底是个啥玩意?接着看下去,你就知道了。
根据微软提供的文档我们可以得知,HRESULT码是一个32位的数值:
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
| 3 |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
S | R | C | N | X | Facility | Code |
S (1 bit): Severity,代表严重性。如果为1,则指示失败结果。如果为0,表示成功结果。
R (1 bit):Reserved, 保留,如果N位为0,则此位必为0,如果N位为1, 将合并S位和R位用以表示四种状态标识,详见下方NTSTATUS模式定义
C (1 bit): Customer,此位表示着该HRESULT信息是由微软定义,还是由用户自定义。如果为0,则为微软定义,如果为1,则为用户自定义,这代表着HRESULT码具有扩展性。如果你在编程过程中遇到了一个第三方组件返回了一个自定义HRESULT码,那么你最好有其错误码所对应的信息表,不然的话,鬼才知道它代表啥意思。
N (1 bit): NTSTATUS,如果此位为1,则表示该消息为返回值模式,详见下方NTSTATUS模式定义。
X (1 bit): 保留,应该设置为0,(应该是为了提供扩展)。
Facility (11 bits): 错误来源的指示符。微软偶尔会增加一些新功能,如下,这是目前微软提供的对照表:
值 | 含义 |
---|---|
FACILITY_NULL 0 | The default facility code. |
FACILITY_RPC 1 | The source of the error code is an RPC subsystem. |
FACILITY_DISPATCH 2 | The source of the error code is a COM Dispatch. |
FACILITY_STORAGE 3 | The source of the error code is OLE Storage. |
FACILITY_ITF 4 | The source of the error code is COM/OLE Interface management. |
FACILITY_WIN32 7 | This region is reserved to map undecorated error codes into HRESULTs. |
FACILITY_WINDOWS 8 | The source of the error code is the Windows subsystem. |
FACILITY_SECURITY 9 | The source of the error code is the Security API layer. |
FACILITY_SSPI 9 | The source of the error code is the Security API layer. |
FACILITY_CONTROL 10 | The source of the error code is the control mechanism. |
FACILITY_CERT 11 | The source of the error code is a certificate client or server? |
FACILITY_INTERNET 12 | The source of the error code is Wininet related. |
FACILITY_MEDIASERVER 13 | The source of the error code is the Windows Media Server. |
FACILITY_MSMQ 14 | The source of the error code is the Microsoft Message Queue. |
FACILITY_SETUPAPI 15 | The source of the error code is the Setup API. |
FACILITY_SCARD 16 | The source of the error code is the Smart-card subsystem. |
FACILITY_COMPLUS 17 | The source of the error code is COM+. |
FACILITY_AAF 18 | The source of the error code is the Microsoft agent. |
FACILITY_URT 19 | The source of the error code is .NET CLR. |
FACILITY_ACS 20 | The source of the error code is the audit collection service. |
FACILITY_DPLAY 21 | The source of the error code is Direct Play. |
FACILITY_UMI 22 | The source of the error code is the ubiquitous memoryintrospection service. |
FACILITY_SXS 23 | The source of the error code is Side-by-side servicing. |
FACILITY_WINDOWS_CE 24 | The error code is specific to Windows CE. |
FACILITY_HTTP 25 | The source of the error code is HTTP support. |
FACILITY_USERMODE_COMMONLOG 26 | The source of the error code is common Logging support. |
FACILITY_USERMODE_FILTER_MANAGER 31 | The source of the error code is the user mode filter manager. |
FACILITY_BACKGROUNDCOPY 32 | The source of the error code is background copy control |
FACILITY_CONFIGURATION 33 | The source of the error code is configuration services. |
FACILITY_STATE_MANAGEMENT 34 | The source of the error code is state management services. |
FACILITY_METADIRECTORY 35 | The source of the error code is the Microsoft Identity Server. |
FACILITY_WINDOWSUPDATE 36 | The source of the error code is a Windows update. |
FACILITY_DIRECTORYSERVICE 37 | The source of the error code is Active Directory. |
FACILITY_GRAPHICS 38 | The source of the error code is the graphics drivers. |
FACILITY_SHELL 39 | The source of the error code is the user Shell. |
FACILITY_TPM_SERVICES 40 | The source of the error code is the Trusted Platform Module services. |
FACILITY_TPM_SOFTWARE 41 | The source of the error code is the Trusted Platform Module applications. |
FACILITY_PLA 48 | The source of the error code is Performance Logs and Alerts |
FACILITY_FVE 49 | The source of the error code is Full volume encryption. |
FACILITY_FWP 50 | he source of the error code is the Firewall Platform. |
FACILITY_WINRM 51 | The source of the error code is the Windows Resource Manager. |
FACILITY_NDIS 52 | The source of the error code is the Network Driver Interface. |
FACILITY_USERMODE_HYPERVISOR 53 | The source of the error code is the Usermode Hypervisor components. |
FACILITY_CMI 54 | The source of the error code is the Configuration Management Infrastructure. |
FACILITY_USERMODE_VIRTUALIZATION 55 | The source of the error code is the user mode virtualization subsystem. |
FACILITY_USERMODE_VOLMGR 56 | The source of the error code is the user mode volume manager |
FACILITY_BCD 57 | The source of the error code is the Boot Configuration Database. |
FACILITY_USERMODE_VHD 58 | The source of the error code is user mode virtual hard disk support. |
FACILITY_SDIAG 60 | The source of the error code is System Diagnostics. |
FACILITY_WEBSERVICES 61 | The source of the error code is the Web Services. |
FACILITY_WINDOWS_DEFENDER 80 | The source of the error code is a Windows Defender component. |
FACILITY_OPC 81 | The source of the error code is the open connectivity service. |
Code (2 bytes): 这个就是主要标示错误编号的部分了,如果是微软定义的HRESULT,即C位为0,你可以通过微软的相关文档找到相应的错误对照信息,如果是第三方组件自定义的HRESULT,此段数据为厂商编写,那么你就要参考组件厂商所提供的错误对照信息了。
NTSTATUS模式
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
| 3 |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sev | C | N | Facility | Code |
Sev (2 bits): Severity,严重性,含义对照如下
值 | 含义 |
---|---|
STATUS_SEVERITY_SUCCESS 0x0 | 成功 |
STATUS_SEVERITY_INFORMATIONAL 0x1 | 返回信息 |
STATUS_SEVERITY_WARNING 0x2 | 警告 |
STATUS_SEVERITY_ERROR 0x3 | 错误 |
C (1 bit): 自定义标识,同上。
N (1 bit): 非NTSTATUS模式设置为0,反之为1。
Facility (12 bits): 如果C位为1,指示要自定义为代码字段使用的编号空间,反之如下:
Name | Value |
---|---|
FACILITY_DEBUGGER | 0x001 |
FACILITY_RPC_RUNTIME | 0x002 |
FACILITY_RPC_STUBS | 0x003 |
FACILITY_IO_ERROR_CODE | 0x004 |
FACILITY_NTWIN32 | 0x007 |
FACILITY_NTSSPI | 0x009 |
FACILITY_TERMINAL_SERVER | 0x00A |
FACILTIY_MUI_ERROR_CODE | 0x00B |
FACILITY_USB_ERROR_CODE | 0x010 |
FACILITY_HID_ERROR_CODE | 0x011 |
FACILITY_FIREWIRE_ERROR_CODE | 0x012 |
FACILITY_CLUSTER_ERROR_CODE | 0x013 |
FACILITY_ACPI_ERROR_CODE | 0x014 |
FACILITY_SXS_ERROR_CODE | 0x015 |
FACILITY_TRANSACTION | 0x019 |
FACILITY_COMMONLOG | 0x01A |
FACILITY_VIDEO | 0x01B |
FACILITY_FILTER_MANAGER | 0x01C |
FACILITY_MONITOR | 0x01D |
FACILITY_GRAPHICS_KERNEL | 0x01E |
FACILITY_DRIVER_FRAMEWORK | 0x020 |
FACILITY_FVE_ERROR_CODE | 0x021 |
FACILITY_FWP_ERROR_CODE | 0x022 |
FACILITY_NDIS_ERROR_CODE | 0x023 |
FACILITY_HYPERVISOR | 0x035 |
FACILITY_IPSEC | 0x036 |
FACILITY_MAXIMUM_VALUE | 0x037 |
Code (2 bytes): 同上。
最后,再附上一个超级好用的微软HRESULT查询网站:https://www.hresult.info/