Get text of a line from notepad++ (SCI_GETLINE)

Get help with using AutoHotkey and its commands and hotkeys
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 00:56

I'm trying to get a specific line form Notepad++
But LineText is empty in my example code. What Do I have to do to get the text?

Code: Select all

;2345
Nppm_HWND(){
	return DllCall("FindWindow", "Str", "Notepad++", "Int", 0, "Ptr") 
}
SCI_HWND(){
  ControlGet hSci, Hwnd,, Scintilla1, % "ahk_id" Nppm_HWND()
  Return hSci
}
SCI_LINELENGTH(Line){
  Return DllCall("SendMessage", "Ptr", SCI_HWND(), "UInt", 2350 , "Int", Line - 1, "Int", 0)
}
SCI_GETLINE(Line){
  VarSetCapacity(LineText, (A_IsUnicode?2:1) * SCI_LINELENGTH(Line))
  Int := DllCall("SendMessage", "Ptr", SCI_HWND(), "UInt", 2153 , "Int", Line - 1, "Ptr", *LineText)
  Return LineText "`n" Int
}

Msgbox % SCI_LINELENGTH(1) "`n"
       . SCI_GETLINE(1)
ExitApp
ciao
toralf
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 03:53

I tried that. But that didn’t work either...
The return value int of the dllcall is than 0. With the * it has been 7.
But thank you for the hint
Last edited by toralf on 30 Sep 2018, 03:58, edited 1 time in total.
ciao
toralf
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 03:54

Try str instead of ptr, and no & (and no *)
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 03:59

With str and without & or *: int is 0 and no linetext
ciao
toralf
User avatar
Alguimist
Posts: 371
Joined: 05 Oct 2015, 16:41
Contact:

Re: Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 04:01

You have to allocate memory in the remote process.

Code: Select all

Nppm_HWND() {
    Return DllCall("FindWindow", "Str", "Notepad++", "Int", 0, "Ptr") 
}

SCI_HWND() {
    ControlGet hSci, Hwnd,, Scintilla1, % "ahk_id" Nppm_HWND()
    Return hSci
}

SCI_LINELENGTH(Line) {
    Return DllCall("SendMessage", "Ptr", SCI_HWND(), "UInt", 2350 , "Int", Line - 1, "Int", 0)
}

SCI_GETLINE(Line) {
    If !(hSci := SCI_HWND()) {
        Return
    }

    WinGet PID, PID, ahk_id %hSci%
    If !(hProc := DllCall("OpenProcess", "UInt", 0x438, "Int", False, "UInt", PID, "Ptr")) {
        Return
    }

    Len := SCI_LINELENGTH(Line)
    Address := DllCall("VirtualAllocEx", "Ptr", hProc, "Ptr", 0, "UPtr", Len, "UInt", 0x1000, "UInt", 4, "Ptr")

    Len := DllCall("SendMessage", "Ptr", hSci, "UInt", 2153 , "Int", Line - 1, "Ptr", Address)
    VarSetCapacity(LineText, Len, 0)
    DllCall("ReadProcessMemory", "Ptr", hProc, "Ptr", Address, "Ptr", &LineText, "UPtr", Len, "Ptr", 0)
    LineText := StrGet(&LineText, "UTF-8")

    DllCall("VirtualFreeEx", "Ptr", hProc, "Ptr", Address, "UPtr", 0, "UInt", 0x8000) ; MEM_RELEASE
    DllCall("CloseHandle", "Ptr", hProc)

    Return LineText
}

Msgbox % "Line 1 (" . SCI_LINELENGTH(1) . " bytes):`n"
       . SCI_GETLINE(1)
ExitApp
I was about to tell you that the function SCI_HWND() is only able to retrieve the hWnd of the first Scintilla instance, but them I found that the ClassNN of the Scintilla control in any tab of Notepad++ is always Scintilla1, whereas in AutoGUI it varies according to the instance. I'm trying to figure out why. It must be something simple.

BTW: detailed information about Scintilla controls can be obtained with WinSpy (see the Extra tab). You can also use it to directly SendMessage to Scintilla controls in the Messages tab, where a ComboBox is populated with all messages declared in Scintilla.h.
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 04:02

The scintilla docs name the parameters
(Int Line, Char *text)
ciao
toralf
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Get text of a line from notepad++ (SCI_GETLINE)

30 Sep 2018, 04:03

Thank you Alguimist
ciao
toralf
User avatar
TheArkive
Posts: 330
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: Get text of a line from notepad++ (SCI_GETLINE)

08 Mar 2020, 05:10

@Alguimist, @toralf
Thanks for posting this. Very helpful for one of my projects.

Return to “Ask For Help”

Who is online

Users browsing this forum: Azev, Bing [Bot], boiler, Google [Bot], Karrsen, Paiter and 42 guests