how to use class RemoteBuffer? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cgx5871
Posts: 325
Joined: 26 Jul 2018, 14:02

how to use class RemoteBuffer?

10 Mar 2024, 18:01

Please tell me how to use class RemoteBuffer?
This is a piece of V1 code, I want to convert it to V2
How to use this class RemoteBuffer? Help me

Code: Select all

hSci := ControlGetHwnd('Scintilla2', 'ahk_class SciTEWindow')
sciOutputHwnd:=WinGetPID(hSci)
str:="xxxx"
n := VarSetCapacity(data, StrPut(str, "UTF-8"))
StrPut(str, &data, n, sciOutputCP)
SendMessage 0xC2, % true, % &data,, ahk_id %sciOutputHwnd% ; EM_REPLACESEL

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('Can`'t open remote process PID = ' . PID . '`nA_LastError: ' . A_LastError, 'RemoteBuffer.__New')

        if !this.ptr := DllCall('VirtualAllocEx', 'Ptr', this.hProc, 'Ptr', 0, 'Ptr', size, Params*) {
            DllCall('CloseHandle', 'Ptr', this.hProc)
            throw OSError('Can`'t allocate memory in remote process PID = ' . PID . '`nA_LastError: ' . A_LastError, 'RemoteBuffer.__New')
        }
    }
    __Delete() {
        DllCall('VirtualFreeEx', 'Ptr', this.hProc, 'Ptr', this.ptr, 'UInt', 0, 'UInt', MEM_RELEASE := 0x8000)
        DllCall('CloseHandle', 'Ptr', this.hProc)
    }
    Read(BufObj, offset := 0) {
        if !DllCall('ReadProcessMemory', 'Ptr', this.hProc, 'Ptr', this.ptr + offset, 'Ptr', BufObj, 'Ptr', BufObj.Size, 'PtrP', &bytesRead := 0)
            throw OSError('Can`'t read data from remote buffer`nA_LastError: ' . A_LastError, 'RemoteBuffer.Read')
        return bytesRead
    }
    Write(BufObj, offset := 0) {
        if !res := DllCall('WriteProcessMemory', 'Ptr', this.hProc, 'Ptr', this.ptr + offset, 'Ptr', BufObj, 'Ptr', BufObj.Size, 'PtrP', &bytesWritten := 0)
            throw OSError('Can`'t write data to remote buffer`nA_LastError: ' . A_LastError, 'RemoteBuffer.Write')
        return bytesWritten
    }
}
teadrinker
Posts: 4565
Joined: 29 Mar 2015, 09:41
Contact:

Re: how to use class RemoteBuffer?

10 Mar 2024, 19:33

What exactly do you want to do?
cgx5871
Posts: 325
Joined: 26 Jul 2018, 14:02

Re: how to use class RemoteBuffer?

11 Mar 2024, 12:47

@teadrinker
convert it to V2
teadrinker
Posts: 4565
Joined: 29 Mar 2015, 09:41
Contact:

Re: how to use class RemoteBuffer?

11 Mar 2024, 12:55

No, I wanted to know what this code is supposed to do. Also, the code you posted is not the AHK v1 code.
neogna2
Posts: 619
Joined: 15 Sep 2016, 15:44

Re: how to use class RemoteBuffer?

11 Mar 2024, 14:09

I guess cgx5871 means that in the OP code box (edit: some of) the lines above class RemoteBuffer are v1 code and the goal is to get them working with the v2 class, which I think he copied from the earlier September 2023 thread viewtopic.php?f=82&t=121044

@cgx5871 you should probably use https://www.scintilla.org/ScintillaDoc.html#SCI_REPLACESEL rather than EM_REPLACESEL which is listed as deprecated and to be removed here https://www.scintilla.org/ScintillaDoc.html#DeprecatedMessages
Last edited by neogna2 on 11 Mar 2024, 17:47, edited 1 time in total.
neogna2
Posts: 619
Joined: 15 Sep 2016, 15:44

Re: how to use class RemoteBuffer?  Topic is solved

11 Mar 2024, 17:43

Try this

Code: Select all

MyText := "hello world"
;SciTE4AutoHotkey v3.1.0 uses Scintilla version <= 5.2.2 with control name 'Scintilla1'
hSci := ControlGetHwnd('Scintilla1', 'ahk_class SciTEWindow')
Sci_ReplaceSel(hSci, MyText)

;https://www.scintilla.org/ScintillaDoc.html#SCI_REPLACESEL
;Scintilla v5.2.2 doc https://web.archive.org/web/20220331224443/https://www.scintilla.org/ScintillaDoc.html#SCI_REPLACESEL
Sci_ReplaceSel(hSci, newText) {
    static SCI_REPLACESEL := 2170
    PID := WinGetPID(hSci)
    ;get SciTE4AutoHotkey Scintilla1 control's current codepage
    cp := "CP" . SendMessage(2137,,, hSci)    ;SCI_GETCODEPAGE:=2137
    ;calculate required buffer size (in bytes) to fit new text in codepage cp
    textSize := StrPut(newText, cp)
    ;create local buffer
    textBuf := Buffer(textSize, 0)
    ;write new text to local buffer in codepage cp
    StrPut(newText, textBuf, cp)
    ;create remote buffer
    RB := RemoteBuffer(PID, textSize)
    ;write local buffer to remote buffer
    RB.Write(textBuf)
    ;send SCI_REPLACESEL to make the Scintilla control replace the selected text
    SendMessage(SCI_REPLACESEL,, RB.ptr, hSci)
}

;by teadrinker https://www.autohotkey.com/boards/viewtopic.php?p=537298#p537298 
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('Can`'t open remote process PID = ' . PID . '`nA_LastError: ' . A_LastError, 'RemoteBuffer.__New')

        if !this.ptr := DllCall('VirtualAllocEx', 'Ptr', this.hProc, 'Ptr', 0, 'Ptr', size, Params*) {
            DllCall('CloseHandle', 'Ptr', this.hProc)
            throw OSError('Can`'t allocate memory in remote process PID = ' . PID . '`nA_LastError: ' . A_LastError, 'RemoteBuffer.__New')
        }
    }
    __Delete() {
        DllCall('VirtualFreeEx', 'Ptr', this.hProc, 'Ptr', this.ptr, 'UInt', 0, 'UInt', MEM_RELEASE := 0x8000)
        DllCall('CloseHandle', 'Ptr', this.hProc)
    }
    Read(BufObj, offset := 0) {
        if !DllCall('ReadProcessMemory', 'Ptr', this.hProc, 'Ptr', this.ptr + offset, 'Ptr', BufObj, 'Ptr', BufObj.Size, 'PtrP', &bytesRead := 0)
            throw OSError('Can`'t read data from remote buffer`nA_LastError: ' . A_LastError, 'RemoteBuffer.Read')
        return bytesRead
    }
    Write(BufObj, offset := 0) {
        if !res := DllCall('WriteProcessMemory', 'Ptr', this.hProc, 'Ptr', this.ptr + offset, 'Ptr', BufObj, 'Ptr', BufObj.Size, 'PtrP', &bytesWritten := 0)
            throw OSError('Can`'t write data to remote buffer`nA_LastError: ' . A_LastError, 'RemoteBuffer.Write')
        return bytesWritten
    }
}
cgx5871
Posts: 325
Joined: 26 Jul 2018, 14:02

Re: how to use class RemoteBuffer?

13 Mar 2024, 10:50

@neogna2
thank you very much

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], Descolada, Draken, Sacho and 34 guests