__ExampleA()
Func __ExampleA()
Local $BinaryString = StringToBinary("Hello @World")
Local $Bin_Len = BinaryLen($BinaryString)
ConsoleWrite('BinaryLen: ' & $Bin_Len & @TAB & 'BinaryString: ' & $BinaryString & @CRLF)
Local $tSTRUCT1 = DllStructCreate('uint; Byte[' & $Bin_Len & ']')
DllStructSetData($tSTRUCT1, 1, $Bin_Len, 1)
For $i = 1 To $Bin_Len ;you have to write bytes...because it is not a string! or you can make a memcpy
DllStructSetData($tSTRUCT1, 2, Dec(StringMid($BinaryString, $i * 2 + 1, 2)), $i)
;~ ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : stringmid($BinaryString,$i*2+1,2) = ' & StringMid($BinaryString, $i * 2 + 1, 2) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Next
ConsoleWrite('[Data uint ]: ' & DllStructGetData($tSTRUCT1, 1) & @CRLF)
ConsoleWrite('[Data String ]: ' & DllStructGetData($tSTRUCT1, 2) & @CRLF)
Local $pVariant = DllStructGetPtr($tSTRUCT1)
ConsoleWrite('[$pVariant ]: ' & $pVariant & @CRLF & @CRLF & @CRLF)
;end of writing data into memory
;start reading data from memory with known pointer
$struct_stringlen = DllStructCreate("uint", $pVariant) ;get first 4 bytes = uint =stringlen
$stringlen = DllStructGetData($struct_stringlen, 1)
;lets get the binarystring
$struct_binarystring = DllStructCreate("byte[" & $stringlen & "]", $pVariant + 4) ;place the struct at the pointer +4 because uint is 4 bytes
$binary = DllStructGetData($struct_binarystring, 1)
$string = BinaryToString($binary)
ConsoleWrite(" Stringlen = " & $stringlen & @CRLF & " Binary = " & $binary & @CRLF & " String = " & $string & @crlf & @crlf)
EndFunc ;==>__ExampleA
If you have an ANSI/ASCII String, it is much easier because the transformation to binary data is omitted. You can write the (ANSI/ASCII) string directly into the memory. This works with char and wchar (16 bit unicode).
__ExampleB()
Func __ExampleB()
Local $String = "Hello @World"
Local $Len = StringLen($String)
ConsoleWrite('StringLen: ' & $Len & @TAB & 'String: ' & $String & @CRLF)
Local $tSTRUCT1 = DllStructCreate('uint; char[' & $Len & ']') ;or wchar if unicode!!!
DllStructSetData($tSTRUCT1, 1, $Len)
DllStructSetData($tSTRUCT1, 2, $String)
ConsoleWrite('[Data uint ]: ' & DllStructGetData($tSTRUCT1, 1) & @CRLF)
ConsoleWrite('[Data String ]: ' & DllStructGetData($tSTRUCT1, 2) & @CRLF)
Local $pVariant = DllStructGetPtr($tSTRUCT1)
ConsoleWrite('[$pVariant ]: ' & $pVariant & @CRLF & @CRLF & @CRLF)
;end of writing data into memory
;start reading data from memory with known pointer
$struct_stringlen = DllStructCreate("uint", $pVariant) ;get first 4 bytes = uint =stringlen
$stringlen = DllStructGetData($struct_stringlen, 1)
;lets get the string char or wchar if unicode
$struct_string = DllStructCreate("char[" & $stringlen & "]", $pVariant + 4) ;place the struct at the pointer +4 because uint is 4 bytes
$String = DllStructGetData($struct_string, 1)
ConsoleWrite(" Stringlen = " & $stringlen & @CRLF & " String = " & $String & @CRLF & @CRLF)
EndFunc ;==>__ExampleB