I can't understand the meaning of this sentence
You mean using hotkeys?
Code: Select all
F12::
{
MsgBox Sci_GetTextRange(hSci, 3, 15)
}
I can't understand the meaning of this sentence
Code: Select all
F12::
{
MsgBox Sci_GetTextRange(hSci, 3, 15)
}
The changelog says that these changes came starting with v5.2.3, so we'll be guided by that.
So, there are two options, first for versions <= 5.2.2, second for versions >= 5.2.3. Some apps still use old versions, like SciTE4AutoHotkey. Notepad++ uses new versions.Duplicate APIs to support 64-bit document positions on Win32: SCI_GETTEXTRANGEFULL, SCI_FINDTEXTFULL, and SCI_FORMATRANGEFULL.
Code: Select all
#Requires AutoHotkey v2
start := 0
end := 100
hSci := ControlGetHwnd('Scintilla1', 'ahk_class SciTEWindow')
MsgBox Sci_GetTextRange(hSci, start, end)
; for Scintilla versions <= 5.2.2 (SciTE4AutoHotkey)
Sci_GetTextRange(hSci, start, end) {
static SCI_GETTEXTRANGE := 2162, SCI_GETTEXTLENGTH := 2183, SCI_GETCODEPAGE := 2137
fullTextLength := SendMessage(SCI_GETTEXTLENGTH,,, hSci)
if start >= fullTextLength {
throw Error('The starting position exceeds the total length of the text')
}
if end < -1 || (end != -1 && end <= start) {
throw Error('Invalid end position')
}
if end > fullTextLength {
end := fullTextLength
}
textSize := end = -1 ? fullTextLength - start : end - start
PID := WinGetPID(hSci)
ptrSize := GetProcessBitness(PID) // 8
Sci_TextRange := Buffer(size := 8 + ptrSize, 0)
RB := RemoteBuffer(PID, size + textSize + 1)
NumPut('UInt', start, 'Int', end, ptrSize = 4 ? 'UInt' : 'Int64', RB.ptr + size, Sci_TextRange)
RB.Write(Sci_TextRange)
textSize := SendMessage(SCI_GETTEXTRANGE,, RB.ptr, hSci) + 1
textBuf := Buffer(textSize, 0)
RB.Read(textBuf, size)
return StrGet(textBuf, "CP" . SendMessage(SCI_GETCODEPAGE,,, hSci))
}
GetProcessBitness(PID) {
static flag := PROCESS_QUERY_INFORMATION := 0x400
if !A_Is64bitOS
return 32
hProc := DllCall('OpenProcess', 'UInt', flag, 'UInt', 0, 'UInt', PID, 'Ptr')
DllCall('IsWow64Process', 'Ptr', hProc, 'IntP', &is32 := 0)
DllCall('CloseHandle', 'Ptr', hProc)
return 32 << !is32
}
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
}
}
Code: Select all
#Requires AutoHotkey v2
start := 0
end := 100
hSci := ControlGetHwnd('Scintilla1', 'ahk_class Notepad++')
MsgBox Sci_GetTextRangeFull(hSci, start, end)
; for Scintilla versions >= 5.2.3 (Notepad++)
Sci_GetTextRangeFull(hSci, start, end) {
static SCI_GETTEXTRANGEFULL := 2039, SCI_GETTEXTLENGTH := 2183, SCI_GETCODEPAGE := 2137
fullTextLength := SendMessage(SCI_GETTEXTLENGTH,,, hSci)
if start >= fullTextLength {
throw Error('The starting position exceeds the total length of the text')
}
if end < -1 || (end != -1 && end <= start) {
throw Error('Invalid end position')
}
if end > fullTextLength {
end := fullTextLength
}
textSize := end = -1 ? fullTextLength - start : end - start
PID := WinGetPID(hSci)
ptrSize := GetProcessBitness(PID) // 8
Sci_TextRange := Buffer(size := ptrSize * 3, 0)
RB := RemoteBuffer(PID, size + textSize + 1)
type := ptrSize = 4 ? 'UInt' : 'Int64'
NumPut(type, start, type, end, type, RB.ptr + size, Sci_TextRange)
RB.Write(Sci_TextRange)
textSize := SendMessage(SCI_GETTEXTRANGEFULL,, RB.ptr, hSci) + 1
textBuf := Buffer(textSize, 0)
RB.Read(textBuf, size)
return StrGet(textBuf, 'CP' . SendMessage(SCI_GETCODEPAGE,,, hSci))
}
GetProcessBitness(PID) {
static flag := PROCESS_QUERY_INFORMATION := 0x400
if !A_Is64bitOS
return 32
hProc := DllCall('OpenProcess', 'UInt', flag, 'UInt', 0, 'UInt', PID, 'Ptr')
DllCall('IsWow64Process', 'Ptr', hProc, 'IntP', &is32 := 0)
DllCall('CloseHandle', 'Ptr', hProc)
return 32 << !is32
}
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
}
}
Code: Select all
#SingleInstance
hSci := ControlGetHwnd('Scintilla1', 'ahk_class SciTEWindow')
MsgBox Sci_GetSelection()
;~ ____________________________________________ 1234567890abcdefgh
Sci_GetSelection() {
static SCI_GETCODEPAGE := 2137, SCI_GETSELTEXT := 2161
;~ ;Get length. SCI_GETSELTEXT
textSize:=SendMessage(SCI_GETSELTEXT, 0, 0,,hSci)
PID := WinGetPID(hSci)
ptrSize := GetProcessBitness(PID) // 8
TextRange := Buffer(size := 8 + ptrSize, 0)
RB := RemoteBuffer(PID, size + textSize + 1)
NumPut(ptrSize = 4 ? 'UInt' : 'Int64', RB.ptr + size, TextRange)
RB.Write(TextRange)
textSize := SendMessage(SCI_GETSELTEXT,, RB.ptr, hSci) + 1
textBuf := Buffer(textSize, 0)
RB.Read(textBuf, size)
return StrGet(textBuf, "CP" . SendMessage(SCI_GETCODEPAGE,,, hSci))
}
GetProcessBitness(PID) {
static flag := PROCESS_QUERY_INFORMATION := 0x400
if !A_Is64bitOS
return 32
hProc := DllCall('OpenProcess', 'UInt', flag, 'UInt', 0, 'UInt', PID, 'Ptr')
DllCall('IsWow64Process', 'Ptr', hProc, 'IntP', &is32 := 0)
DllCall('CloseHandle', 'Ptr', hProc)
return 32 << !is32
}
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
}
}
Code: Select all
#SingleInstance
hSci := ControlGetHwnd('Scintilla1', 'ahk_class SciTEWindow')
MsgBox Sci_GetSelection(hSci)
Sci_GetSelection(hSci) {
static SCI_GETCODEPAGE := 2137, SCI_GETSELTEXT := 2161
PID := WinGetPID(hSci)
textSize := SendMessage(SCI_GETSELTEXT,,, hSci) + 1
RB := RemoteBuffer(PID, textSize)
SendMessage(SCI_GETSELTEXT,, RB.ptr, hSci)
textBuf := Buffer(textSize, 0)
RB.Read(textBuf)
return StrGet(textBuf, "CP" . SendMessage(SCI_GETCODEPAGE,,, hSci))
}
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
}
}
Code: Select all
#SingleInstance
hSci := ControlGetHwnd('Scintilla1', 'ahk_class SciTEWindow')
Sci_INSERTTEXT("AAA",5)
;~ _________________________________
Sci_INSERTTEXT(txt,pos) {
static Sci_INSERTTEXT:=2003,SCI_GETCODEPAGE:=2137,SCI_GETCODEPAGE := 2137
textSize := StrPut(txt,"CP" . SendMessage(SCI_GETCODEPAGE,,, hSci)) ; textsize
PID := WinGetPID(hSci)
ptrSize := GetProcessBitness(PID) // 8
Sci_TextRange := Buffer(size := 8 + ptrSize, 0)
RB := RemoteBuffer(PID, size + textSize + 1)
NumPut('UInt', pos, ptrSize = 4 ? 'UInt' : 'Int64', RB.ptr + size, Sci_TextRange) ; pos
RB.Write(Sci_TextRange)
SendMessage(Sci_INSERTTEXT,pos, RB.ptr, hSci) + 1
}
;~ ================================
GetProcessBitness(PID) {
static flag := PROCESS_QUERY_INFORMATION := 0x400
if !A_Is64bitOS
return 32
hProc := DllCall('OpenProcess', 'UInt', flag, 'UInt', 0, 'UInt', PID, 'Ptr')
DllCall('IsWow64Process', 'Ptr', hProc, 'IntP', &is32 := 0)
DllCall('CloseHandle', 'Ptr', hProc)
return 32 << !is32
}
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
}
}
Code: Select all
#Include <RemoteBuffer>
hSci := ControlGetHwnd("Scintilla2", "ahk_class SciTEWindow")
cp:="CP" . SendMessage(2137,,, hSci) ;~ SCI_GETCODEPAGE:=2137
Sci_InsertText("aaa",2)
Sci_InsertText(Text, pos := -1) {
textSize := StrPut(Text, cp)
TextCnv := Buffer(textSize)
PID := WinGetPID(hSci)
RB := RemoteBuffer(PID, textSize)
StrPut(Text, TextCnv, cp)
RB.Write(TextCnv, textSize + 1)
/**SCI_INSERTTEXT(position pos, const char *text) := 2003
*This inserts the zero terminated text string at position pos or at the current position if pos is -1.
*If the current position is after the insertion point then it is moved along with its surrounding text but no scrolling is performed.
*/
SendMessage(2003, pos, RB.ptr, , hSci)
}
Code: Select all
hSci := ControlGetHwnd("Scintilla1", "ahk_class SciTEWindow")
cp := "CP" . SendMessage(2137,,, hSci) ;~ SCI_GETCODEPAGE:=2137
Sci_InsertText("aaa",2)
Sci_InsertText(Text, pos := -1) {
;calculate needed buffer size to fit the text (including a NUL zero terminator at the end)
textSize := StrPut(Text, cp)
;create local Buffer that fits text and NUL
;zero-initialising ensures that the last character is NUL (0)
TextCnv := Buffer(textSize, 0)
PID := WinGetPID(hSci)
;create remote Buffer that fits text and NUL
RB := RemoteBuffer(PID, textSize)
StrPut(Text, TextCnv, cp)
;write local buffer to remote buffer without offset
RB.Write(TextCnv)
SendMessage(2003, pos, RB.ptr, hSci)
}