在 C# 中使用 HResult、互操作和相关内容
我正在使用 C# 开发一个项目来创建表单应用程序。我想使用 IFileDialog 和其他功能,它们是本机 Windows API 的一部分(或者应该调用 ti )。
一开始我对互操作或 Windows API 一无所知。我正在开始学习,但有时很难找到某些方面的非常基本的信息。因此我有一个(可能是微不足道/愚蠢的)问题:
HResults 经常被使用。据我了解,HResults只不过是一个32位实体,其中不同的位提供有关某些操作结果的信息。在我在网上找到的一些代码中,我经常看到类似 int hres = -2147467259;
的内容。作为一个十足的菜鸟,我去检查一下这意味着什么。 -2147467259
是 0xFFFFFFFF80004005
,在 MSDN 的在线文档中我可以看到 0x80004005
表示 E_FAIL
又名未指定错误。我的问题是,FFFFFFFF
部分怎么了?他们不能直接使用int hres = 0x80004005
吗?也许这是非常明显的,而且我是个菜鸟,但仍然:)
I am working on a project in C# to create a forms application. I would like to use IFileDialog and other functionality that is part of the native Windows API (or however ti should be called).
Starting out I knew absolutely nothing about interop or the Windows API. I am starting to learn but at times it can be hard to find very basic info on certain aspects. Therefore I have a (probably trivial/stupid) questions:
HResults are used often. As I understand, HResults are nothing more than a 32 bit entity where the different bits supply info on the result of certain operations. In some code I found online I often see things like int hres = -2147467259;
. Being a total noob I went to check what this means. -2147467259
is 0xFFFFFFFF80004005
and in the online documentation at MSDN I can see that 0x80004005
signifies E_FAIL
aka unspecified error. My question is, what is up with the FFFFFFFF
part? Couldn't they just have used int hres = 0x80004005
? Maybe this is very obvious and I'm a total noob, but still :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 32 位平台上,
int
的长度为 32 位,即 8 个十六进制数字的 4 个字节。因此,E_FAIL
将是0x80004005
,(这就是您粘贴的代码所显示的内容。如果您将此值转储到 64 位计算机上,那么它将占用两倍的存储空间由于数字是符号扩展的,并且前导 8(二进制100
)意味着符号位是1
,因此它一直是1111
。二进制是十六进制的F
,它带来了您看到的所有F
。On 32 bit platforms an
int
is 32 bits long which is 4 bytes of 8 hexadecimal digits. SoE_FAIL
would be0x80004005
, (which is what the code you pasted shows. If you dump this value on a 64 bit machine then it'll take up twice as much storage and since numbers are sign extended and the leading 8 (binary100
) means the sign bit is1
then it's ones all the way.1111
in binary isF
in hex which brings all theF
s you see.FFFFFFFF 部分是因为你的 HResult 是负数。
这就是计算机使用二进制补码存储负数的方式。
看看这个计算(我的重点):
-2147467259 = 0xFFFFFFFF80004005
-2147467259 + 2^32(翻转)= 2147500037 = 0x80004005
The FFFFFFFF part is because your HResult is negative.
That's how computers store negative numbers using Two's complement.
Check out this calculation (my emphasis):
-2147467259 = 0xFFFFFFFF80004005
-2147467259 + 2^32 (rollover) = 2147500037 = 0x80004005