Find a string in an e.g. exe or dll file

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

This script searches through an e.g. exe or dll file for the input string.

Usage

Push "500" ;character count per search
Push "Hello"
Push "$INSTDIR\myfile.dll"
 Call SearchByteFile
Pop $0
Pop $1

$0 = Found? yes|no|error
$1 = [times found]

The Function

Function SearchByteFile
Exch $0 ;file
Exch
Exch $1 ;search for
Exch
Exch 2
Exch $2 ;group-up count
Exch 2
Push $3
Push $4
Push $5
Push $6
Push $7
Push $8
Push $9
 
 ClearErrors
  FileOpen $9 $0 r
   IfErrors error
 
 StrCpy $8 0
 
loop_read_top:
 StrCpy $3 0
 StrCpy $5 ""
 
loop_read:
 ClearErrors
  FileReadByte $9 $4
   IfErrors exit
  IntFmt $4 "%c" $4
   StrCpy $5 $5$4
  IntOp $3 $3 + 1
   StrCmp $3 $2 +2
Goto loop_read
 
 StrLen $6 $1
 StrCpy $3 0
 
loop_search:
  IntOp $3 $3 - 1
  StrCpy $4 $5 $6 $3
    StrCmp $4 "" loop_read_top
StrCmp $4 $1 0 loop_search
 
StrCpy $0 yes
 StrCpy $5 $5 $3
 StrCpy $3 0
  IntOp $8 $8 + 1
Goto loop_search
 
error:
 StrCpy $0 error
 
exit:
 StrCmp $0 yes +3
 StrCmp $0 error +2
  StrCpy $0 no
 
StrCpy $1 $8
FileClose $9
 
Pop $9
Pop $8
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Exch $1 ;found count
Exch
Exch $0 ;yes/no/error
FunctionEnd

-Stu