Code: Select all
memcpy(ptrAddress, ptrFrom, size) {
return DllCall("msvcrt\memcpy_s", "Ptr", ptrAddress, "Int", size, "Ptr", ptrFrom, "Int", size, "Int")
}
BaseToDec(n, Base) {
static U := A_IsUnicode ? "wcstoui64_l" : "strtoui64"
return, DllCall("msvcrt\_" U, "Str",n, "Uint",0, "Int",Base, "CDECL Int64")
}
DecToBase(n, Base) {
static U := A_IsUnicode ? "w" : "a"
VarSetCapacity(S,65,0)
DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
return, S
}
HexStringToBufferObject(ByRef buffer, str, repeat := 1) {
originalStr := str
Loop, % repeat - 1 {
str .= " " . originalStr
}
hexString := str
bytes := StrSplit(str, " ")
VarSetCapacity(buffer, bytes.MaxIndex() + 2, 0)
for k, hex in bytes {
val := BaseToDec(hex, 16)
NumPut(val, buffer, k - 1, "Char")
}
StringUpper, hexString, hexString
return { "str" : hexString, "buffer" : &buffer, "size" : bytes.MaxIndex() }
}
GetBufferObjectFrom(ByRef buffer, ptrAddress, bufferSize) {
VarSetCapacity(buffer, bufferSize, 0)
memcpy(&buffer, ptrAddress, bufferSize)
hexString := ""
Loop % bufferSize {
val := DecToBase(NumGet(buffer, A_Index - 1, "UChar"), 16)
if (val < 10) {
hexString .= "0"
}
hexString .= val
if (A_Index != bufferSize) {
hexString .= " "
}
}
StringUpper, hexString, hexString
return { "str" : hexString, "buffer" : &buffer, "size" : bufferSize }
}
var1 := HexStringToBufferObject(b, "01 02 03")
var2 := GetBufferObjectFrom(b2, var1.buffer, var1.size)
var3 := GetBufferObjectFrom(b3, var2.buffer, var2.size)
MsgBox % var1.str . " == " . var2.str . " == " . var3.str
But it does not work if I remove b, b2, b3 and ByRef buffer variables from the function definitions. Can I get it to work without those? They seem redundant.
I tried also following, and it does not copy the contents, as if the address can't be referenced:
Code: Select all
memcpy(ptrAddress, ptrFrom, size) {
return DllCall("msvcrt\memcpy_s", "Ptr", ptrAddress, "Int", size, "Ptr", ptrFrom, "Int", size, "Int")
}
BaseToDec(n, Base) {
static U := A_IsUnicode ? "wcstoui64_l" : "strtoui64"
return, DllCall("msvcrt\_" U, "Str",n, "Uint",0, "Int",Base, "CDECL Int64")
}
DecToBase(n, Base) {
static U := A_IsUnicode ? "w" : "a"
VarSetCapacity(S,65,0)
DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
return, S
}
HexStringToBufferObject(str, repeat := 1) {
originalStr := str
Loop, % repeat - 1 {
str .= " " . originalStr
}
hexString := str
bytes := StrSplit(str, " ")
VarSetCapacity(buffer, bytes.MaxIndex() + 2, 0)
for k, hex in bytes {
val := BaseToDec(hex, 16)
NumPut(val, buffer, k - 1, "Char")
}
StringUpper, hexString, hexString
return { "str" : hexString, "buffer" : buffer, "size" : bytes.MaxIndex() }
}
GetBufferObjectFrom(ptrAddress, bufferSize) {
VarSetCapacity(buffer, bufferSize, 0)
memcpy(&buffer, ptrAddress, bufferSize)
hexString := ""
Loop % bufferSize {
val := DecToBase(NumGet(buffer, A_Index - 1, "UChar"), 16)
if (val < 10) {
hexString .= "0"
}
hexString .= val
if (A_Index != bufferSize) {
hexString .= " "
}
}
StringUpper, hexString, hexString
return { "str" : hexString, "buffer" : buffer, "size" : bufferSize }
}
var1 := HexStringToBufferObject("01 02 03")
var2 := GetBufferObjectFrom(&var1.buffer, var1.size)
var3 := GetBufferObjectFrom(&var2.buffer, var2.size)
MsgBox % var1.str . " == " . var2.str . " == " . var3.str