函数参考


DllStructCreate

创建一个 C/C++ 样式的数据结构供 DllCall 使用.

DllStructCreate ( Struct [,Pointer] )

参数

Struct 用字符串来表示的要创建的数据结构.(见 注释).
指针 [可选参数] 如果指定的数据结构不能分配内存,则使用此指针参数.

返回值

成功: 返回一个供 DllStruct 调用的变量.
失败: 0 .
@Error: 0 = 没有错误.
1 = 传递给 DllStructCreate 的变量不是一个字符串.
2 = 传递的字符串中有一个未知的数据类型.
3 = 为数据结构分配需要的内存失败, 或者指针 = 0.
4 = 为传递的字串分配内存错误.


类型 详细信息
BYTE 8bit(1字节) 带符号字符(signed char)
BOOLEAN 8bit(1字节) 无符号字符(unsigned char)
CHAR 8bit(1字节) ASCII 字符(ASCII char)
WCHAR 16bit(2字节) UNICODE 宽字符(Wide char)
short 16bit(2字节) 带符号整数(signed integer)
USHORT 16bit(2字节) 无符号整数(unsigned integer)
WORD 16bit(2字节) 无符号整数(unsigned integer)
int 32bit(4字节) 带符号整数(signed integer)
long 32bit(4字节) 带符号整数(signed integer)
BOOL 32bit(4字节) 带符号整数(signed integer)
UINT 32bit(4字节) 无符号整数(unsigned integer)
ULONG 32bit(4字节) 无符号整数(unsigned integer)
DWORD 32bit(4字节) 无符号整数(unsigned integer)
INT64 64bit(8字节) 带符号整数(signed integer)
UINT64 64bit(8字节) 无符号整数(unsigned integer)
ptr 32/64位 无符号整数(unsigned integer) (依据使用的 x86 或 x64 AutoIt 的版本)
HWND 32bit(4字节) 整数(integer)
HANDLE 32bit(4字节) 整数(integer)
float 32bit(4字节) 浮点指针(floating point)
double 64bit(8字节) 浮点指针(floating point)
INT_PTR, LONG_PTR, LRESULT, LPARAM 32/64位 带符号整数(signed integer) (依据使用的 x86 或 x64 AutoIt 的版本)
UINT_PTR, ULONG_PTR, DWORD_PTR, WPARAM 32/64位 无符号整数(unsigned integer) (依据使用的 x86 或 x64 AutoIt 的版本)
STRUCT The following datatypes will be align according to C declaration rules. See below.
ENDSTRUCT end of the collection datatypes. Padding can occurs see below.
ALIGN n bytes boundary where datatype must be aligned.

注意/说明

每个数据类型必须使用分号 ';' 分开.

如果要指定数据大小请在数据类型后面添加 '[大小]' : DllStructCreate("int;char[128]")

元素名称可以添加类似C样式的声明: DllStructCreate("int n;char buffer[128]").
这些数据名称可用于在其他 DllStruct... 函数里. 数据名称必须为英文字母或下划线.

If a collection of datatypes is defined as in a "struct{}" in C declaration, the "STRUCT; ...; ENDSTRUCT;" must be used.
This needs to be done to respect alignment inside the entire structure creation. No need if all datatypes are in the defined structure as an implicit structure alignment is done.

DllStructCreate("int;STRUCT;ptr;int;ENDSTRUCT;int") ; structure is 32 bytes under a Windows 64-Bit and 16 under Windows 32-Bit
DllStructCreate("int;ptr;int;int") ; structure is 24 bytes under a Windows 64-Bit and 16 under Windows 32-Bit

To use a different alignment prefix the structure with the align keyword. The default value for n is 8. Valid values are 1, 2, 4, 8, and 16. The alignment of a member will be on a boundary that is either a multiple of n or a multiple of the size of the member, whichever is smaller. This is equivalent to the #pragma pack option with the Microsoft Visual C++ compiler.

DllStructCreate("short;int") ; 数据结构有 8 字节, "int" 在偏移量 4 的位置
DllStructCreate("align 2;short;int") ; 数据结构有 6 字节, "int" 在偏移量 2 的位置

DllStructCreate("byte;double") ; 数据结构有 16 字节, "double" 在偏移量 8 的位置
DllStructCreate("align 4;byte;double") ; 数据结构有 12 字节, "double" 在偏移量 4 的位置

If a change of alignment is needed "align" can be use before the first element which need to be changed.
"align" or "align 8" leads to default alignment.

要释放分配的内存,只需将返回的变量设置为 0.

The following aggregate alignment rules apply:

The alignment of an array is the same as the alignment of one of the elements of the array.

The alignment of the beginning of a structure is the maximum alignment of any individual member.
Each member within the structure is be placed at its proper alignment as defined in the previous table, which require implicit internal padding, depending on the previous member.

Structure size is an integral multiple of its alignment, which requires padding after the last member.

相关

DllCall, DllStructGetData, DllStructSetData, DllStructGetPtr, DllStructGetSize, IsDllStruct

示例/演示


;=========================================================
;   创建数据结构
;   struct {
;       int             var1;
;       unsigned char   var2;
;       unsigned int    var3;
;       char            var4[128];
;   }
;=========================================================
Local $str = "int var1;byte var2;uint var3;char var4[128]"
Local $a = DllStructCreate($str)
If @error Then
    MsgBox(4096,"","DllStructCreate 发生错误" & @error);
    Exit
EndIf

;=========================================================
;   设置数据结构中的数据
;   struct.var1 = -1;
;   struct.var2 = 255;
;   struct.var3 = INT_MAX; -1 将自动确定类型(unsigned int)
;   strcpy(struct.var4,"Hello");
;   struct.var4[0]  = 'h';
;=========================================================
DllStructSetData($a, "var1", -1)
DllStructSetData($a, "var2", 255)
DllStructSetData($a, "var3", -1)
DllStructSetData($a, "var4", "Hello")
DllStructSetData($a, "var4", Asc("h"), 1)

;=========================================================
;   显示数据结构的信息
;=========================================================
MsgBox(4096,"DllStruct","数据结构大小: " & DllStructGetSize($a) & @CRLF & _
        "数据结构指针: " & DllStructGetPtr($a) & @CRLF & _
        "Data:" & @CRLF & _
        DllStructGetData($a, 1) & @CRLF & _
        DllStructGetData($a, 2) & @CRLF & _
        DllStructGetData($a, 3) & @CRLF & _
        DllStructGetData($a, 4))

;=========================================================
;   释放为数据结构分配的内存
;=========================================================
$a = 0