Page 1 of 1
FindText in IDE editor
Posted: Thu Apr 23, 2009 2:05 pm
by lavachri
Hi,
i'm trying to find text in the Scintilla editor of the IDE.
I maked an external tool, but then i call it Pure crash by an "Error : Invalid memory access".
I don't understand what is wrong in my conde ...
Code: Select all
EnableExplicit
Global scintilla
Procedure.l ScFindText( Text$, BeginPos = 0, EndPos = #INFINITE, Flag = 0 )
Protected Search.SCTextToFind
Protected R = -1
Search\ChrG\CpMin = BeginPos
Search\ChrG\CpMax = EndPos
Search\lpstrText = @Text$
If SendMessage_( Scintilla, #SCI_FINDTEXT, Flag, Search )
R = Search\ChrGText\CpMin
EndIf
ProcedureReturn R
EndProcedure
;
Scintilla = Val(GetEnvironmentVariable("PB_Tool_Scintilla"))
If Scintilla
If ScFindText( "Procedure" ) <> -1
SendMessage_( scintilla, #SCI_GOTOLINE, 0, 0)
EndIf
EndIf
End
Thanks for your help
Posted: Thu Apr 23, 2009 2:55 pm
by Arctic Fox
Shouldn't it be either Scintilla or scintilla (uppercase or lowercase S) in the variable name?
Posted: Thu Apr 23, 2009 3:53 pm
by Fluid Byte
Arctic Fox wrote:Shouldn't it be either Scintilla or scintilla (uppercase or lowercase S) in the variable name?
Variable names are case insensitive. So is EnableExplicit.
Posted: Thu Apr 23, 2009 5:12 pm
by hallodri
Allocate your Text$ buffer with VirtualAllocEx_
example:
http://www.purebasic.fr/german/viewtopi ... 992#232992
Posted: Fri Apr 24, 2009 9:25 am
by lavachri
Thanks,
but je may be too bad ...
Pure continue to crash !
Code: Select all
EnableExplicit
Global hSCI
Global P
Procedure GetProcessFromWindow(hwnd)
Protected pid.l = 0
Protected result.l = 0
If GetWindowThreadProcessId_(hwnd, @pid)
result = OpenProcess_(#PROCESS_ALL_ACCESS, #False, pid)
EndIf
ProcedureReturn result
EndProcedure
Procedure.l ScFindText( Text$, BeginPos = 0, EndPos = #INFINITE, Flag = 0 )
Protected hProc.l, bW.l, len.l, R = -1
Protected *mem.l
Protected Search.SCTextToFind
hProc = GetProcessFromWindow( hSCI )
If hProc
len = Len( Text$ )
*mem = VirtualAllocEx_( hProc, 0, len, #MEM_RESERVE | #MEM_COMMIT, #PAGE_EXECUTE_READWRITE)
If *mem
WriteProcessMemory_( hProc,*mem, @Text$, len, @bW)
Search\ChrG\CpMin = BeginPos
Search\ChrG\CpMax = EndPos
Search\lpstrText = *mem
If SendMessage_( hSCI, #SCI_FINDTEXT, Flag, Search )
R = Search\ChrGText\CpMin
EndIf
VirtualFreeEx_(hProc,*mem, len, #MEM_RELEASE)
EndIf
CloseHandle_(hProc)
EndIf
ProcedureReturn R
EndProcedure
; Main
hSCI = Val(GetEnvironmentVariable("PB_Tool_Scintilla"))
If hSCI
P = ScFindText( "Procedure" )
MessageRequester("Find", str(P) )
EndIf
End
Re: FindText in IDE editor
Posted: Fri Apr 16, 2010 9:52 pm
by eddy
It works now.
#SCI_FINDTEXT accepts
a structure pointer as second argument so you have to
allocate memory for this pointer.
I noticed that
Search\chrgText\CpMin and
Search\chrgText\CpMax are not returned correctly perhaps because PB uses a 2 years old version of scintilla
Code: Select all
EnableExplicit
Global SCI
Procedure GetProcessFromWindow(hwnd)
Protected PID, result
If GetWindowThreadProcessId_(hwnd, @PID)
result=OpenProcess_(#PROCESS_ALL_ACCESS, #False, PID)
EndIf
ProcedureReturn result
EndProcedure
Procedure ScFindText(Text$, Beginpos=0, Endpos=1000, Flag=0)
Protected proc
Protected *Search.SCTextToFind,Search.SCTextToFind, pos=-1
Protected *Text, len
proc=GetProcessFromWindow(SCI)
If proc
len=Len(Text$)+1
*Text=VirtualAllocEx_(proc, 0, len, #MEM_RESERVE | #MEM_COMMIT, #PAGE_EXECUTE_READWRITE)
*Search=VirtualAllocEx_(proc, 0, SizeOf(SCTextToFind), #MEM_RESERVE | #MEM_COMMIT, #PAGE_EXECUTE_READWRITE)
If *Text
WriteProcessMemory_(proc, *Text, @Text$, len, #Null)
Search\chrg\cpMin=Beginpos
Search\chrg\cpMax=Endpos
Search\lpstrText=*Text
Search\chrgText\cpMin=Beginpos
Search\chrgText\cpMax=Endpos
If *Search
WriteProcessMemory_(proc, *Search, @Search, SizeOf(SCTextToFind), #Null)
pos=SendMessage_(SCI, #SCI_FINDTEXT, Flag, *Search)
If pos>-1
MessageRequester("",str(Search\chrg\CpMin)+" "+str(Search\chrgText\CpMin)+" "+str(Search\chrg\cpMax)+" "+str(Search\chrgText\cpMax))
EndIf
VirtualFreeEx_(proc, *Search, SizeOf(SCTextToFind), #MEM_RELEASE)
EndIf
VirtualFreeEx_(proc, *Text, len, #MEM_RELEASE)
EndIf
CloseHandle_(proc)
EndIf
ProcedureReturn pos
EndProcedure
; Main
SCI=Val(GetEnvironmentVariable("PB_Tool_Scintilla"))
If SCI
Define pos=ScFindText("e")
MessageRequester("Word Found", "pos="+str(pos))
EndIf
End