为什么在 C++ 中我们使用 DWORD 而不是 unsigned int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2995251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:44:01  来源:igfitidea点击:

Why in C++ do we use DWORD rather than unsigned int?

c++winapitypesdword

提问by dreadwail

I'm not afraid to admit that I'm somewhat of a C++ newbie, so this might seem like a silly question but....

我不怕承认我有点像 C++ 新手,所以这似乎是一个愚蠢的问题,但是......

I see DWORD used all over the place in code examples. When I look up what a DWORD truly means, its apparently just an unsigned int (0 to 4,294,967,295). So my question then is, why do we have DWORD? What does it give us that the integral type 'unsigned int' does not? Does it have something to do with portability and machine differences?

我看到在代码示例中到处都使用了 DWORD。当我查看 DWORD 的真正含义时,它显然只是一个无符号整数(0 到 4,294,967,295)。所以我的问题是,为什么我们有 DWORD?整数类型“unsigned int”没有给我们什么?是否与便携性和机器差异有关?

回答by GManNickG

DWORDis not a C++ type, it's defined in <windows.h>.

DWORD不是 C++ 类型,它是在<windows.h>.

The reason is that DWORDhas a specific range and format Windows functions rely on, so if you require that specific range use that type. (Or as they say "When in Rome, do as the Romans do.") For you, that happens to correspond to unsigned int, but that might not always be the case. To be safe, use DWORDwhen a DWORDis expected, regardless of what it may actually be.

原因是DWORDWindows 函数依赖于特定范围和格式,因此如果您需要该特定范围,请使用该类型。(或者正如他们所说的“在罗马时,像罗马人那样做。”)对您来说,这恰好对应于unsigned int,但情况可能并非总是如此。为安全起见,请DWORDDWORD预期为a 时使用,而不管它实际是什么。

For example, if they ever changed the range or format of unsigned intthey could use a different type to underly DWORDto keep the same requirements, and all code using DWORDwould be none-the-wiser. (Likewise, they could decide DWORDneeds to be unsigned long long, change it, and all code using DWORDwould be none-the-wiser.)

例如,如果他们改变了范围或格式,unsigned int他们可以使用不同的类型作为基础DWORD来保持相同的要求,并且所有使用的代码DWORD都是不明智的。(同样,他们可以决定DWORD需要unsigned long long,更改它,并且所有使用的代码DWORD都不明智。)



Also note unsigned intdoes notnecessary have the range 0 to 4,294,967,295. See here.

另外值得注意unsigned int没有必要有范围为0〜4294967295。见这里

回答by Windows programmer

When MS-DOS and Windows 3.1 operated in 16-bit mode, an Intel 8086 word was 16 bits, a Microsoft WORD was 16 bits, a Microsoft DWORD was 32 bits, and a typical compiler's unsigned int was 16 bits.

当 MS-DOS 和 Windows 3.1 在 16 位模式下运行时,Intel 8086 字为 16 位,Microsoft WORD 为 16 位,Microsoft DWORD 为 32 位,典型编译器的 unsigned int 为 16 位。

When Windows NT operated in 32-bit mode, an Intel 80386 word was 32 bits, a Microsoft WORD was 16 bits, a Microsoft DWORD was 32 bits, and a typical compiler's unsigned int was 32 bits. The names WORD and DWORD were no longer self-descriptive but they preserved the functionality of Microsoft programs.

Windows NT 在 32 位模式下运行时,Intel 80386 字为 32 位,Microsoft WORD 为 16 位,Microsoft DWORD 为 32 位,典型编译器的 unsigned int 为 32 位。名称 WORD 和 DWORD 不再具有自我描述性,但它们保留了 Microsoft 程序的功能。

When Windows operates in 64-bit mode, an Intel word is 64 bits, a Microsoft WORD is 16 bits, a Microsoft DWORD is 32 bits, and a typical compiler's unsigned int is 32 bits. The names WORD and DWORD are no longer self-descriptive, AND an unsigned int no longer conforms to the principle of least surprises, but they preserve the functionality of lots of programs.

Windows 在 64 位模式下运行时,Intel 字为 64 位,Microsoft WORD 为 16 位,Microsoft DWORD 为 32 位,典型编译器的 unsigned int 为 32 位。名称 WORD 和 DWORD 不再具有自我描述性,并且 unsigned int 不再符合最小意外原则,但它们保留了许多程序的功能。

I don't think WORD or DWORD will ever change.

我认为 WORD 或 DWORD 永远不会改变。

回答by Alex F

SDK developers prefer to define their own types using typedef. This allows changing underlying types only in one place, without changing all client code. It is important to follow this convention. DWORD is unlikely to be changed, but types like DWORD_PTR are different on different platforms, like Win32 and x64. So, if some function has DWORD parameter, use DWORD and not unsigned int, and your code will be compiled in all future windows headers versions.

SDK 开发人员更喜欢使用 typedef 定义他们自己的类型。这允许仅在一处更改底层类型,而无需更改所有客户端代码。遵守这个约定很重要。DWORD 不太可能改变,但像 DWORD_PTR 这样的类型在不同平台上是不同的,比如 Win32 和 x64。因此,如果某些函数具有 DWORD 参数,请使用 DWORD 而不是 unsigned int,并且您的代码将在所有未来的 Windows 头文件版本中编译。

回答by YeenFei

For myself, I would assume unsigned int is platform specific. Integer could be 8 bits, 16 bits, 32 bits or even 64 bits.

对于我自己,我认为 unsigned int 是特定于平台的。整数可以是 8 位、16 位、32 位甚至 64 位。

DWORD in the other hand, specifies its own size, which is Double Word. Word are 16 bits so DWORD will be known as 32 bit across all platform

另一方面,DWORD 指定自己的大小,即双字。Word 是 16 位,因此 DWORD 在所有平台上都称为 32 位