计算字符串需要的缓冲空间
; 计算字符串需要的缓冲空间. bytes_per_char := A_IsUnicode ? 2 : 1 max_chars := 500 max_bytes := max_chars * bytes_per_char Loop 2 { ; 分配用于 DllCall 的空间. VarSetCapacity(buf, max_bytes) if A_Index = 1 ; 通过 DllCall 间接修改变量. DllCall("wsprintf", "ptr", &buf, "str", "0x%08x", "uint", 4919) else ; 使用 "str" 来自动更新长度: DllCall("wsprintf", "str", buf, "str", "0x%08x", "uint", 4919) ; 连接字符串以演示为什么需要更新长度: wrong_str := buf . "<end>" wrong_len := StrLen(buf) ; 更新变量的长度. VarSetCapacity(buf, -1) right_str := buf . "<end>" right_len := StrLen(buf) MsgBox, ( Before updating String: %wrong_str% Length: %wrong_len% After updating String: %right_str% Length: %right_len% ) }