Win32 Error Code COM Error Code NTSTATUS的区别、转换

这三种码其实都是Windows系统错误码,只是对应不同API和使用场景。它们既有区别,又相互有联系。

一、区别和联系

  • 都是32位值
  • Win32 Error Code和NTSTATUS位域组成相同,但Win32 Error Code的取值范围只能在0x00000000---0x0000FFFF
  • Win32 Error Code和COM Error Code,在高2位定义不同,设备来源值可能一样,但代表的设备不一样,设备来源值位数也不一样,但它们又可以互相转换。它们实际上的定义都是LONG型的,都是通过相关的API函数返回值返回来的,小于0时都代表着失败了。
  • 具有相同格式的ntstatus和win32错误代码可能会使用相同的设施代码。然而,情况并非如此——相反,win32错误代码(根据winerrror.h)使用的是hresults的功能值!因此,交换ntstatus和win32错误代码在语法上是正常的,但由于设施代码不匹配而改变了它们的语义。

二、相互转换

1 Facility may need to be adapted
2 Holds for ‘real’ Win32 error codes. For compatibility error codes, use HRESULT_FROM_WIN32
3 As long as you have a ‘real’ HRESULT (i.e. not one from HRESULT_FROM_WIN32) and want to get a ‘real’ Win32 error code (i.e. not a compaitibility one) — otherwise it can get tricky
4 Note that HRESULT_FROM_NT does not take the NT Status to Win32 Error Code conversion table into account, thus the result may not be what one would expect. Using LsaNtStatusToWinError takes this table into account, but yields ‘compatibility’ Win32 error code.

参考:

https://jpassing.com/2007/08/20/error-codes-win32-vs-hresult-vs-ntstatus/?like=1&_wpnonce=9495153bae

 

posted on 2019-07-10 19:28  活着的虫子  阅读(1178)  评论(0编辑  收藏  举报

导航