Question: is there a way to use the direct access method from AutoHotkey to control Scintilla in notepad++ and other external processes?
My test code below gives DllCall error 0xC0000005 "access violation". I think because Scintilla's direct function is in notepad++ memory and not AutoHotkey memory. Can we work around that somehow? Some extra DllCall steps with OpenProcess?
Code: Select all
#IfWinActive, ahk_exe notepad++.exe
F1::
;scintilla control handle
ControlGet, CtrlHWND, Hwnd,, Scintilla1, A
;scintilla direct access
;https://www.scintilla.org/ScintillaDoc.html#DirectAccess
;"On Windows, the message-passing scheme used to communicate between the container and Scintilla is mediated "by the "
;"operating system SendMessage function and can lead to bad performance when calling intensively. "
;"To avoid this overhead, Scintilla provides messages that allow you to call the Scintilla message function directly."
;https://www.scintilla.org/Steps.html
;"retrieve the pointer to the function and a pointer which must be the first parameter"
;also https://www.autohotkey.com/boards/viewtopic.php?p=335222#p335222
df := DllCall("SendMessage", "Ptr", CtrlHWND, "Int", SCI_GETDIRECTFUNCTION := 2184, "Int", 0, "Int", 0, "UInt")
dp := DllCall("SendMessage", "Ptr", CtrlHWND, "Int", SCI_GETDIRECTPOINTER := 2185, "Int", 0, "Int", 0, "Ptr")
;direct access call to read caret position
;https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/scintilla/include/Scintilla.h#L57
CurrentPos := DllCall(df, "Ptr", dp, "Int", SCI_GETCURRENTPOS := 2008, "Int", wParam := 0, "Int", lParam := 0)
MsgBox % "CtrlHWND=" CtrlHWND
. "`ndf=" df
. "`ndp=" dp
. "`nCurrentPos=" CurrentPos
. "`nErrorLevel=" ErrorLevel ; error 0xC0000005 = "access violation"
;For comparison, reading scintilla caret position in notepad++ through SendMessage works
MsgBox % CurrentPos := DllCall("SendMessage", "Ptr", CtrlHWND, "Int", SCI_GETCURRENTPOS := 2008)
ExitApp
#IfWinActive