autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
kevinpak
Posts: 4
Joined: 30 Apr 2024, 17:57
Contact:

autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?

Post by kevinpak » 30 Apr 2024, 18:07

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?

teadrinker
Posts: 4358
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?

Post by teadrinker » 01 May 2024, 03:29

kevinpak wrote: convert string to byte
It's not clear what you mean by that. A string is a sequence of bytes. What exactly should be the result of conversion?

kevinpak
Posts: 4
Joined: 30 Apr 2024, 17:57
Contact:

Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?

Post by kevinpak » 01 May 2024, 04:38

Maybe I didn't describe it clearly
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)
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.

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.]

teadrinker
Posts: 4358
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?

Post by teadrinker » 01 May 2024, 08:39

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)
    }
}

kevinpak
Posts: 4
Joined: 30 Apr 2024, 17:57
Contact:

Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?

Post by kevinpak » 01 May 2024, 19:06

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.

kevinpak
Posts: 4
Joined: 30 Apr 2024, 17:57
Contact:

Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?

Post by kevinpak » 02 May 2024, 07:50

teadrinker wrote:
01 May 2024, 08:39
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)
    }
}

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 result displayed by debug is buffer.size=8.
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.

User avatar
thqby
Posts: 417
Joined: 16 Apr 2021, 11:18
Contact:

Re: autohotkey V2 does not have a byte type, Is there any way to convert string to byte in AHK?

Post by thqby » 02 May 2024, 08:39

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.

teadrinker
Posts: 4358
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?

Post by teadrinker » 02 May 2024, 08:51

Replace this line

Code: Select all

StrPut(str, buf := Buffer(StrPut(str, 'CP0')), 'CP0')
with

Code: Select all

StrPut(str, buf := Buffer(StrPut(str, 'CP0') - 1), 'CP0')

Post Reply

Return to “Ask for Help (v2)”