Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open an exe for Binary Access?

Status
Not open for further replies.

therick

Programmer
Mar 20, 2001
34
0
0
US
I need to create a config tool that consists of a simple form w/ a text box for updating an ip string hard coded and compiled into an exe. This IP changes a lot and I don't want to recompile everytime the IP needs to be changed. I can do this myself w/ a hex editor but would rather the client not fool w/ my code in a hex editor. Thus, I need to make use of the :

Open "filepath" For Binary Access Read As #1

in conjunction with a Put or Get Statement

Essentially, I want to open the exe for writing. Pass that to a variable that I can do a string search on. Find some dummy strings (in binary format) that will mark where the IP is and then update the binary IP w/ a new one.

Please help...I'm not too good at regular old Basic.
 
A (probably) safer route would be to hae the details in a seperate "file", and read the file at run time. Generally, none would use an ".INI" file or registry entries for the 'dynamic' information. At this point, I don't think much new development is done even using IP addresses, as network browsing for connections is fairly common place.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
You'll be much better off finding a way to externalize the value. You said recompile, so you have control of the source.

Shift the Value to an external object. INI File, registry, or whatever. If that's the only configurable item the envirionment and command line are other places you can change it from.

Use the internal value as your last ditch default, Registry value overrides that, INI overrides both of those, Environment provides the next layer. And, the command line overrides everything.

If you are bent on modifying the exe.
Get entire file into a string and use Mid$ to edit it.
Instr$ will find the a string for you.


Wil Mead
wmead@optonline.net

 
How do I get it to a string that I can edit? I want to provide the use w/ a simple GUI just to change the IP in the compiled code.
I guess what I'm still not understanding is how to get the exe file that has just been opened for Binary Access to a string variable. With the FileSystemObject, I would just access the returned string from the TextStream and search through it, edit it and then save the file after the changes have been made but the FSO doesn't allow binary reading.

I can only send the one .exe so I couldn't include an ini I dont' think.

Thanks
 
tested on Win95 only

Code:
  sFile = "C:\Project1.exe"
  nFile = FreeFile
  
  sCont = Space$(FileLen(sFile))
  
  Open sFile For Binary Lock Read Write As nFile
  Get nFile, 1, sCont
  Close nFile


you may not find the text you are looking for unless you insert chr$(0) between all chars. of your text

i.e.,
i compiled a test project and found that the hard-coded text i had ("535.54.530.535") was stored as
"5" & chr$(0) & "3" & chr$(0) & "5" & chr$(0) & "." & chr$(0) & "5" & chr$(0) & "4" & chr$(0) & "." & chr$(0) & "5" & chr$(0) & "3" & chr$(0) & "0" & chr$(0) & "." & chr$(0) & "5" & chr$(0) & "3" & chr$(0) & "5" in the .EXE file
 
i've found that editing an EXE file this way only works if the new string is exactly the same length as the old string you are replacing. otherwise you can make the EXE unexecutable.
haven't tested if you could pad your new string with Chr$(0)'s if the new string is shorter.
am pretty sure it won't work if your new string is longer that your old string.
 
Thanks Justin...how did you perform your search and replace? I am trying the following where Text1 and 2 are textboxes where I put what I want to find and what I want to replace it with....not replacing correctly though.

Here's what I have been trying:

Private Sub Command1_Click()
Dim i$, i2$, msg As String, X As Integer, filedata As String, msg2 As String, Y As Integer
i$ = "D:\LanlordReports\lakdiva\Release\lakdiva4.exe" ' Program to Make
i2$ = "D:\LanlordReports\lakdiva\Release\lakdiva.exe" ' Program to Edit
' Make sure this file you want to edit e
' xists
If Len(Dir$(i2$)) = 0 Then
MsgBox "" + Chr(34) + UCase(i2$) + Chr(34) + " Does Not Exist", 32, "anti"
End If

Exit Sub
' Open the file to make and the file you
' r making this file with

'Public Sub Stuff()

Open i$ For Output As #1
Open i2$ For Binary As #2


Do While Not EOF(2)
' Grab the next 8000 characters out of t
' he file
filedata = Input$(8000, #2)
msg = filedata


'For Y = 0 To List1.ListCount - 1
' Check and replace the old strings with
' the new strings


Do
' X = InStr(LCase$(msg), LCase$(List1.List(Y)))
X = InStr(LCase$(msg), LCase$(Text1.Text))


If X <> 0 Then
msg2 = msg2 + Mid(msg, 1, X - 1) + Text2.Text
msg = Mid(msg, X + Len(Text1.Text))
End If
Loop Until Not (X <> 0)
msg2 = msg2 + msg
msg = msg2
msg2 = &quot;&quot;
' Next
' Print the new characters into the file
' your making
msg2 = msg2 + msg
Print #1, msg2;
msg2 = &quot;&quot;
If Len(msg) > 8000 Then msg = &quot;&quot;
Loop
Close #2
Close #1
' Say that it's done
MsgBox Chr(34) + text11 + Chr(34) + &quot; Is Done Compiling (your program has been edited)&quot;, 32, &quot;anti&quot;

End Sub
 
if you have VB6 like i do then you can use the Replace function.
ex. stext=replace(stext,sfind,sreplace,1,-1,vbTextCompare)

Again i must stress that the replace string should be exactly the same length as your search string.

in case you forgot, you might (like i did) have to insert Chr$(0) between all letters of your search string and replace string

(&quot;justin&quot; s/b &quot;j&quot; & Chr$(0) & &quot;u&quot; & Chr$(0) & &quot;s&quot; & Chr$(0) & &quot;t&quot; & Chr$(0) & &quot;i&quot; & Chr$(0) & &quot;n&quot;)

do not have time to check your code (maybe later)
 
I keep getting type mismatch errors with this code..... entering into text 1: &quot;1&quot; &chr$(0) &&quot;9&quot; &chr$(0)
text 2: &quot;2&quot; &chr$(0) &&quot;z&quot; &chr$(0)

just want to start by replacing the 1 and 9 in &quot;192.168.1.15&quot; w/ 2 and z to see if it works but get type mismatch. Please help if you have any ideas.


Private Sub Command1_Click()
Dim msg
Dim scont

sFile = &quot;D:\LanlordReports\lakdiva\Release\lakdiva.exe&quot;
nFile = &quot;D:\LanlordReports\lakdiva\Release\lakdiva.exe&quot;

scont = Space$(FileLen(sFile))

Open sFile For Binary Lock Read Write As nFile
Get nFile, 1, scont

msg = Replace(scont, Text1.Text, Text2.Text, 1, -1, vbTextCompare)

Put nFile, , msg


Close nFile
End Sub

 
Code:
Option Explicit' add this at the very start of your form's code!

Private Sub Command1_Click()
  Dim sCont As String
  
  Dim nFile As Integer ' s/b numeric!
  Dim sFile As String
  Dim sFind As String ' for inserting Chr$(0)'s
  Dim sRepl As String ' for inserting Chr$(0)'s
  Dim sTemp As String
  Dim i As Long
  
  sFind = Text1.Text ' w/o Chr$(0)'s
  sRepl = Text2.Text ' w/o Chr$(0)'s
  
  ' insert Chr$(0) between chars. of sFind
  sTemp = &quot;&quot;
  For i = 1 To Len(sFind)
    sTemp = sTemp & Chr$(0) & Mid$(sFind, i, 1)
  Next i
  sFind = Mid$(sTemp, 2)
  
  ' insert Chr$(0) between chars. of sRepl
  sTemp = &quot;&quot;
  For i = 1 To Len(sRepl)
    sTemp = sTemp & Chr$(0) & Mid$(sRepl, i, 1)
  Next i
  sRepl = Mid$(sTemp, 2)
  
  sFile = &quot;D:\LanlordReports\lakdiva\Release\lakdiva.exe&quot;
  nFile = FreeFile ' s/b numeric!
  
  sCont = Space$(FileLen(sFile))
  
  Open sFile For Binary Lock Read Write As nFile
  Get nFile, 1, sCont
  
  sCont = Replace(sCont, sFind, sRepl, 1, -1, vbTextCompare)
  
  Put nFile, 1, sCont
  
  Close nFile
End Sub


I suggest you back-up the EXE file first
Again, the replace string s/b the same length as your search string
 
Justin....if you live in Austin I'll buy you a beer...works great...thanks.
 
Justin,

How would you recommend handling IP addresses with this? For instance, there will be times when I need to replace something like 192.168.1.13 w/ say 64.221.51.89 which is one character shorter. You've helped me tons on this so I don't want to bug you too much, but if you have any ideas I would love to hear them.

Thanks,

Rick
 
Maybe you could hardcode in 000.000.000.000, the max length of any IP. And when u want to replace the ip with something like: 198.1.2.14, you should replace it with
198.001.002.014.

Then from inside your program, you take those zeros out and make it 198.1.2.14 again.
 
That's sort of along the lines of what I was thinking but couldn't figure out what to do w/ 198.001.002.014...I think you're idea of handling that part in the program is probably the best solution...thanks Venom.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top