I've been working on various functions for getting/setting the text of controls. And Scintilla is one of them, so here's a sneak peek of my functions:
I might replace the JEE_DC functions with DllCall when I'm finished.
Functions such as these that deal with remote buffers in external processes, should be used with caution as they could potentially crash the process.
Code: Select all
q:: ;Scintilla control - get/set text (works with Notepad2 and Notepad++)
ControlGet, hSci, Hwnd,, Scintilla1, A
MsgBox, % JEE_SciGetText(hSci)
JEE_SciSetText(hSci, "hello world")
return
;==================================================
;JEE_ScintillaGetText
;options:
;a - read as ANSI
JEE_SciGetText(hCtl, vOpt:="")
{
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
SendMessage, 2006, 0, 0,, % "ahk_id " hCtl ;SCI_GETLENGTH := 2006 ;returns size of UTF-8 buffer in bytes (not chars)
vSize := ErrorLevel+1
VarSetCapacity(vText, vSize, 0)
if vIsLocal
SendMessage, 2182, % vSize, % &vText,, % "ahk_id " hCtl ;SCI_GETTEXT := 2182
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
return
SendMessage, 2182, % vSize, % pBuf,, % "ahk_id " hCtl ;SCI_GETTEXT := 2182
JEE_DCReadProcessMemory(hProc, pBuf, &vText, vSize, 0)
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
return StrGet(&vText, InStr(vOpt, "a")?"CP0":"UTF-8")
}
;==================================================
;JEE_ScintillaSetText
JEE_SciSetText(hCtl, vText)
{
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
vSize := StrPut(vText, "UTF-8")
vOutput := ""
VarSetCapacity(vOutput, vSize, 0)
StrPut(vText, &vOutput, "UTF-8")
if vIsLocal
SendMessage, 2181,, % &vOutput,, % "ahk_id " hCtl ;SCI_SETTEXT := 2181
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
return
JEE_DCWriteProcessMemory(hProc, pBuf, &vOutput, vSize, 0)
SendMessage, 2181,, % pBuf,, % "ahk_id " hCtl ;SCI_SETTEXT := 2181
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
}
;==================================================
JEE_DCOpenProcess(vAccess, hInherit, vPID)
{
return DllCall("kernel32\OpenProcess", UInt,vAccess, Int,hInherit, UInt,vPID, Ptr)
}
JEE_DCVirtualAllocEx(hProc, vAddress, vSize, vAllocType, vProtect)
{
return DllCall("kernel32\VirtualAllocEx", Ptr,hProc, Ptr,vAddress, UPtr,vSize, UInt,vAllocType, UInt,vProtect, Ptr)
}
JEE_DCWriteProcessMemory(hProc, vBAddress, pBuf, vSize, vWritten)
{
return DllCall("kernel32\WriteProcessMemory", Ptr,hProc, Ptr,vBAddress, Ptr,pBuf, UPtr,vSize, Ptr,vWritten)
}
JEE_DCReadProcessMemory(hProc, vBAddress, pBuf, vSize, vRead)
{
return DllCall("kernel32\ReadProcessMemory", Ptr,hProc, Ptr,vBAddress, Ptr,pBuf, UPtr,vSize, Ptr,vRead)
}
JEE_DCVirtualFreeEx(hProc, vAddress, vSize, vFreeType)
{
return DllCall("kernel32\VirtualFreeEx", Ptr,hProc, Ptr,vAddress, UPtr,vSize, UInt,vFreeType)
}
JEE_DCCloseHandle(hObject) ;e.g. hProc
{
return DllCall("kernel32\CloseHandle", Ptr,hObject)
}
;==================================================
ControlSetText and Control EditPaste are not guaranteed to work correctly on all controls.
When things happen where only one character is inserted into a control can often be related to Unicode/ANSI issues. I.e. you have an ANSI control, you try to put Unicode text into it, the Unicode text 'abc' in byte form is: a null b null c null. And so the control sees the string a, as ANSI strings terminate at the first null byte.