FindText in IDE editor

Just starting out? Need help? Post your questions and find answers here.
lavachri
User
User
Posts: 19
Joined: Tue Oct 14, 2008 3:01 pm
Location: French

FindText in IDE editor

Post 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
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Post by Arctic Fox »

Shouldn't it be either Scintilla or scintilla (uppercase or lowercase S) in the variable name?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post by hallodri »

Allocate your Text$ buffer with VirtualAllocEx_
example:

http://www.purebasic.fr/german/viewtopi ... 992#232992
lavachri
User
User
Posts: 19
Joined: Tue Oct 14, 2008 3:01 pm
Location: French

Post 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
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: FindText in IDE editor

Post 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
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply