delphi枚举类型长度默认为2个字节(单字),而在C中枚举为4个字节(双字),如果需要跨这两个平台编程,传输结构时会由于数据长度不一造成灾难。
经过查找资料,原来delphi可以通过{$Z+} {$Z-} {$Z1} {$Z4} 等宏设置枚举类型的长度,小至1个字节,大至4个字节。
官方说明如下:
The $Z directive controls the minimum storage size of Delphi enumerated types.
An enumerated type is stored as an unsigned byte if the enumeration has no more than 256 values, and if the type was declared in the {$Z1} state (the default). If an enumerated type has more than 256 values, or if the type was declared in the {$Z2} state, it is stored as an unsigned word. Finally, if an enumerated type is declared in the {$Z4} state, it is stored as an unsigned double word.
The {$Z2} and {$Z4} states are useful for interfacing with C and C++ libraries, which usually represent enumerated types as words or double words.
Note: Note: For backwards compatibility with early versions of Delphi and CodeGear Pascal, the directives {$Z-} and {$Z+} are also supported. They correspond to {$Z1} and {$Z4}, respectively.
例子如下:
Example code : Various enum type sizes |
type {$Z1} TCars1 = (Rover, Jaguar, Honda); // Will fit into 1 byte TFruit1 = (Banana=255, Apple, Pear); // Will exceed one byte {$Z4} TCars2 = (Ford, Nissan, Vauxhall); // Now uses 4 bytes TFruit2 = (Orange=255, Plum, Grape); // Now uses 4 bytes begin ShowMessage('TCars1 size = '+IntToStr(SizeOf(TCars1))); ShowMessage('TFruit1 size = '+IntToStr(SizeOf(TFruit1))); ShowMessage('TCars2 size = '+IntToStr(SizeOf(TCars2))); ShowMessage('TFruit2 size = '+IntToStr(SizeOf(TFruit2))); end; |
TCars1 size = 1 TFruit1 size = 2 TCars2 size = 4 TFruit2 size = 4 |
相关推荐
演示如何操作Delphi 的枚举类型。 1. 包含源代码; 2. 包含执行程序 3. 演示如下函数如何使用:GetEnumName、GetEnumProp、GetEnumValue、SetEnumProp 4. 非常简单,一看就会。
delphi枚举系统硬件id,SetupDiGetClassDevs,SetupDiEnumDeviceInfo,SetupDiGetDeviceRegistryProperty
DELPHI窗体枚举源码,可以枚举全部窗体,包含隐藏窗体。
Delphi 线程枚举的简单例子,可能和CPU线程有关系,线程越多的情况下,枚举到的线程也越多。线程是如何获取的,这个枚举的例子会告诉你方法。
delphi枚举字符串转换
Delphi实现枚举所有网卡信息,成功识别当前使用的那个网卡 正在使用的网卡MAC:04-A1-51-B6-54-25 Adapter 1 ComboIndex: 0 Adapter name: {A76C4421-246E-44B1-A342-27EACCD0487C} Description: NETGEAR WNDA4100 ...
Delphi 枚举窗口的方法 演示 最简单的Delphi枚举窗体的方法演示,运行程序后先刷新一下,程序即可枚举出当前所有打开的窗口,枚举出的每一个窗体都会列出窗体标题、窗体类名、窗体宽高、进程路径等信息。
枚举线程的Delphi实例代码,Delphi 枚举线程的例子应该很多了,这一个比较简单的那种,可以为学习Delphi的新手朋友提供一些参考思路,运行界面效果如下图所示。源代码编译于Delphi7.0环境。
Delphi枚举指定进程的内存堆,也就是枚举指定程序的内存,枚举出内存堆栈ID、内存基地址、占用内存大小以及标志等,程序还需改进,有些内存堆栈是枚举不到的。
Delphi枚举指定程序的内存堆..rar
枚举程序关联句柄,与windows资源监视器功能一样。
delphixe设置全局快捷键 delphixe设置全局快捷键 delphixe设置全局快捷键
Delphi枚举Windows系统的所有特权..rar
Delphi获取内存信息+枚举进程..rar
delphi设置全局快捷键
Delphi设置显示器属性..rar
Delphi 设置专家是一个功能强大的Delphi管理工具。使用它,可以轻松的管理Delphi的组件、专家、工具,快速切换组件等等,它将是你最佳的Delphi伴侣工具。 系统重装后无需对Delphi进行漫长繁琐的安装。 方便的管理...
delphi 设置屏幕分辨率,delphi 设置屏幕分辨率
Delphi 7数据类型 详细说明了,新手资料大吓跳过