Microsoft如何说WinAPI中单词的大小为16位? [英] How can Microsoft say the size of a word in WinAPI is 16 bits?
问题描述
我刚刚开始学习WinAPI.在MSDN中,对WORD数据类型提供了以下说明.
I've just started learning the WinAPI. In the MSDN, the following explanation is provided for the WORD data type.
WORD
一个16位无符号整数.范围是0到65535(十进制).
此类型在WinDef.h中声明如下:
typedef unsigned short WORD;
WORD
A 16-bit unsigned integer. The range is 0 through 65535 decimal.
This type is declared in WinDef.h as follows:
typedef unsigned short WORD;
足够简单,并且它与我一直在学习中使用的其他资源相匹配,但是怎么可以确定地说它是16位呢? Wikipedia上的 C数据类型页指定
Simple enough, and it matches with the other resources I've been using for learning, but how can it can definitively said that it is 16 bits? The C data types page on Wikipedia specifies
short/short int/有符号的short/有符号的short int
短符号整数 类型.
能够至少包含[−32767,+32767]范围;因此, 它的大小至少为 16位.
short / short int / signed short / signed short int
Short signed integer type.
Capable of containing at least the [−32767, +32767] range; thus, it is at least 16 bits in size.
因此,根据C标准,short
的大小可能为32位.但是谁决定要使用什么位大小呢?我找到了实用说明在这里.具体来说,该行:
So the size of a short
could very well be 32 bits according to the C Standard. But who decides what bit sizes are going to be used anyway? I found a practical explanation here. Specifically, the line:
...这取决于两个处理器(更具体地说,是ISA,指令 设置架构,例如x86和x86-64)和编译器,包括 编程模型.
...it depends on both processors (more specifically, ISA, instruction set architecture, e.g., x86 and x86-64) and compilers including programming model.
那么这就是ISA,我想这是有道理的.这是我迷路的地方.看一下Wikipedia上的 Windows页面,我在侧栏中看到了这一点:>
So it's the ISA then, which makes sense I suppose. This is where I get lost. Taking a look at the Windows page on Wikipedia, I see this in the side bar:
平台 ARM,IA-32,Itanium,x86-64,DEC Alpha,MIPS,PowerPC
Platforms ARM, IA-32, Itanium, x86-64, DEC Alpha, MIPS, PowerPC
我真的不知道这些是什么,但是我认为这些是处理器,每个处理器都有一个ISA.也许Windows支持这些平台,因为它们都保证对unsigned short
使用16位吗?这听起来不太正确,但是我对这方面的知识还不够了解,无法进一步研究.
I don't really know what these are but I think these are processors, each which would have an ISA. Maybe Windows supports these platforms because all of them are guaranteed to use 16 bits for an unsigned short
? This doesn't sound quite right, but I don't really know enough about this stuff to research any further.
回到我的问题:当C标准本身不保证short
始终为16位时,Windows API如何能够typedef unsigned short WORD;
然后说WORD
是16位无符号整数?
Back to my question: How is it that the Windows API can typedef unsigned short WORD;
and then say WORD
is a 16-bit unsigned integer when the C Standard itself does not guarantee that a short
is always 16 bits?
推荐答案
简单地说,WORD
始终为16位.
Simply put, a WORD
is always 16 bits.
由于WORD
始终为16位,但unsigned short
并非为16位,因此WORD
并不总是unsigned short
.
As a WORD
is always 16 bits, but an unsigned short
is not, a WORD
is not always an unsigned short
.
对于Windows SDK支持的每个平台,Windows头文件均包含#ifdef
样式宏,这些宏可以检测编译器及其平台,并将Windows SDK定义的类型(WORD
,DWORD
等)关联到适当大小的平台类型.
For every platform that the Windows SDK supports, the windows header file contains #ifdef
style macros that can detect the compiler and its platform, and associate the Windows SDK defined types (WORD
, DWORD
, etc) to the appropriately sized platform types.
这就是为什么Windows SDK实际上使用内部定义的类型(例如WORD
)而不是使用语言类型:以便它们可以确保其定义始终正确的原因.
This is WHY the Windows SDK actually uses internally defined types, such as WORD
, rather than using language types: so that they can ensure that their definitions are always correct.
Microsoft工具链附带的Windows SDK可能很懒,因为Microsoft c ++工具链始终使用16位无符号短裤.
The Windows SDK that ships with Microsoft toolchains, is possibly lazy, as Microsoft c++ toolchains always use 16bit unsigned shorts.
我不希望将Visual Studio C ++附带的windows.h放入GCC,clang等中,因为许多细节,包括使用Platform SDK分发的.iib文件导入dll的机制,都无法正常工作. Microsoft特定的实现.
I would not expect the windows.h that ships with Visual Studio C++ to work correctly if dropped into GCC, clang etc. as so many details, including the mechanism of importing dll's using .iib files that the Platform SDK distribute, is a Microsoft specific implementation.
另一种解释是:
Microsoft说WORD
是16位.如果某人"想要调用Windows API,则他们必须传递一个16位值,其中API将字段定义为WORD.
微软还可能说,为了使用其Windows SDK中存在的Windows头文件来构建有效的Windows程序,用户必须选择具有16位short
的编译器.
Microsoft says a WORD
is 16 bits. If "someone" wants to call a windows API, they must pass a 16 bit value where the API defines the field as a WORD.
Microsoft also possibly says, in order to build a valid windows program, using the windows header files present in their Windows SDK, the user MUST choose a compiler that has a 16bit short
.
c ++规范没有说编译器必须将short
s实现为16位-微软表示您选择构建Windows可执行文件的编译器必须.
The c++ spec does not say that compilers must implement short
s as 16 bits - Microsoft says the compiler you choose to build windows executables must.
这篇关于Microsoft如何说WinAPI中单词的大小为16位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!