autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
My AHK needs to obtain a certain string type field from program A, process it and send it to program B. The type required by B's interface is byte, but AHK does not convert characters into byte like python string type enconde, causing program B to The required data type cannot be accepted. Is there any way to convert string to byte in AHK?
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
Maybe I didn't describe it clearly
For example in python
Because AHK does not have a byte type, I don’t know how to get the same value as byte_value in ahk.
The following code can be used to run program B in python, but if it is transformed into AHK, I don’t know how to do it.
[Mod edit: Added Python codebox tags. Please use appropriate code tags yourself when posting code.]
For example in python
Code: Select all
str_value="!123456"(This value is obtained by program A)
bytes_value=str_value.encode('ascii','ignore')
bytes_value=b'!123456' (The converted value needs to be passed to program B)
The following code can be used to run program B in python, but if it is transformed into AHK, I don’t know how to do it.
Code: Select all
argv_address = kernel32.VirtualAllocEx(ths_process_hwnd, 0, 8, VIRTUAL_MEN, FAGE_READWRITE)
kernel32.WriteProcessMemory(ths_process_hwnd, argv_address, bytes_value, 7, None)
win32api.SendMessage(ths_hwnd, 1398, 0, argv_address)
[Mod edit: Added Python codebox tags. Please use appropriate code tags yourself when posting code.]
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
Try this:
Code: Select all
#Requires AutoHotkey v2
DetectHiddenWindows true
Persistent
str := '!123456'
pidOrProcessName := 'MyProgram.exe' ; specify PID or exe name of the target program
if !PID := ProcessExist(pidOrProcessName) {
throw Error('Process not found')
}
StrPut(str, buf := Buffer(StrPut(str, 'CP0')), 'CP0')
RB := RemoteBuffer(PID, buf.size)
RB.Write(buf)
SendMessage 1398,, RB.ptr,, 'ahk_pid ' . PID
class RemoteBuffer
{
__New(PID, size) {
static flags := (PROCESS_VM_OPERATION := 0x8) | (PROCESS_VM_WRITE := 0x20) | (PROCESS_VM_READ := 0x10)
, params := ['UInt', MEM_COMMIT := 0x1000, 'UInt', PAGE_READWRITE := 0x4, 'Ptr']
if !this.hProc := DllCall('OpenProcess', 'UInt', flags, 'Int', 0, 'UInt', PID, 'Ptr') {
throw OSError()
}
if !this.ptr := DllCall('VirtualAllocEx', 'Ptr', this.hProc, 'Ptr', 0, 'Ptr', size, params*) {
DllCall('CloseHandle', 'Ptr', this.hProc)
throw OSError()
}
}
Read( localBuf, offset := 0) => this._ReadWrite('Read' , localBuf, this.ptr + offset)
Write(localBuf, offset := 0) => this._ReadWrite('Write', localBuf, this.ptr + offset)
_ReadWrite(mode, buf, addr) {
if !DllCall(mode . 'ProcessMemory', 'Ptr', this.hProc, 'Ptr', addr, 'Ptr', buf, 'Ptr', buf.size, 'PtrP', &bytes := 0)
throw OSError()
return bytes
}
__Delete() {
DllCall('VirtualFreeEx', 'Ptr', this.hProc, 'Ptr', this.ptr, 'UInt', 0, 'UInt', MEM_RELEASE := 0x8000)
DllCall('CloseHandle', 'Ptr', this.hProc)
}
}
Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
I ran the code you provided and I stepped through the debug trace and found that the allocated memory and number of bytes looked the same as in python, but it still doesn't work.
Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
teadrinker wrote: ↑01 May 2024, 08:39Try this:Code: Select all
#Requires AutoHotkey v2 DetectHiddenWindows true Persistent str := '!123456' pidOrProcessName := 'MyProgram.exe' ; specify PID or exe name of the target program if !PID := ProcessExist(pidOrProcessName) { throw Error('Process not found') } StrPut(str, buf := Buffer(StrPut(str, 'CP0')), 'CP0') RB := RemoteBuffer(PID, buf.size) RB.Write(buf) SendMessage 1398,, RB.ptr,, 'ahk_pid ' . PID class RemoteBuffer { __New(PID, size) { static flags := (PROCESS_VM_OPERATION := 0x8) | (PROCESS_VM_WRITE := 0x20) | (PROCESS_VM_READ := 0x10) , params := ['UInt', MEM_COMMIT := 0x1000, 'UInt', PAGE_READWRITE := 0x4, 'Ptr'] if !this.hProc := DllCall('OpenProcess', 'UInt', flags, 'Int', 0, 'UInt', PID, 'Ptr') { throw OSError() } if !this.ptr := DllCall('VirtualAllocEx', 'Ptr', this.hProc, 'Ptr', 0, 'Ptr', size, params*) { DllCall('CloseHandle', 'Ptr', this.hProc) throw OSError() } } Read( localBuf, offset := 0) => this._ReadWrite('Read' , localBuf, this.ptr + offset) Write(localBuf, offset := 0) => this._ReadWrite('Write', localBuf, this.ptr + offset) _ReadWrite(mode, buf, addr) { if !DllCall(mode . 'ProcessMemory', 'Ptr', this.hProc, 'Ptr', addr, 'Ptr', buf, 'Ptr', buf.size, 'PtrP', &bytes := 0) throw OSError() return bytes } __Delete() { DllCall('VirtualFreeEx', 'Ptr', this.hProc, 'Ptr', this.ptr, 'UInt', 0, 'UInt', MEM_RELEASE := 0x8000) DllCall('CloseHandle', 'Ptr', this.hProc) } }
Code: Select all
str_value="!123456"
byte_value=str_value.encode('ascii','ignore')
byte_length=len(str_value.encode('ascii','ignore'))
print(byte_length) #byte_length=7
Code: Select all
str := '!123456'
pidOrProcessName := 'MyProgram.exe' ; specify PID or exe name of the target program
if !PID := ProcessExist(pidOrProcessName) {
throw Error('Process not found')
}
StrPut(str, buf := Buffer(StrPut(str, 'CP0')), 'CP0')
The byte length in pthon is 7. I don’t know much about the usage of ahk’s buffer and strput. I don’t know how ahk can be one byte longer than python,I checked the help instructions of AHK about buffer and strput, etc., but I didn't understand them.I don’t know if program B cannot work because there is one more byte in ahk’s buffer.
Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
https://www.autohotkey.com/docs/v2/lib/StrPut.htm#Return_Value wrote:In 2-parameter mode, this function returns the required buffer size in bytes, including space for the null-terminator.
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?
Replace this line
with
Code: Select all
StrPut(str, buf := Buffer(StrPut(str, 'CP0')), 'CP0')
Code: Select all
StrPut(str, buf := Buffer(StrPut(str, 'CP0') - 1), 'CP0')