🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.tools
Article #389

Re: Can VB write over itself?

#389
From: Howard Pell
Date: Mon, 31 Aug 1992 13:21
70 lines
3514 bytes
joero@microsoft.com (Joe Robison) writes:

>In article <U=T.714494455@cansqnt> hlpell@sequent.com wrote:
>> I want to have my software product prompt the user for his/her name and the
>> registration number and then store this in a safe place, as do most
>> software packages.  Can Visual Basic overwrite its own .EXE file to store
>> this info?  That is, can I set aside an area of the .EXE that will be used
>> exclusively for this purpose?  Then if the setup program sees that this
>> area has data in it, it will not prompt the user to re-enter his/her name
>> and registration number.
>> 
>> Any and all help appreciated.

>Well, doing this gives me the heebee-geebies (is that how you spell that?)
>but here you go:

>Function ReplaceTextinEXE (EXE As String, Target As String, replace As String)
>Dim f As Integer, i As Long, X As String
>    If Len(Target) <> Len(replace) Then Exit Function
>    f = FreeFile
>    If UCase$(Right$(EXE, 4)) <> ".EXE" Then EXE = EXE + ".EXE"
>    Open "foo.exe" For Binary Access Read Write Shared As f
>    For i = 1 To LOF(f) - Len(Target)
>        X = String$(Len(Target), " ")
>        Get #f, i, X
>        If X = Target Then
>            Put #f, i, replace
>            ReplaceTextinEXE = -1
>            Close #f
>            Exit Function
>        End If
>    Next
>End Function


>Essentially what you want to do is store a target string somewhere in
>your application (an invisible label on a form, in the tag property of
>any control, even as literal in your code).  Then you call this function 
>and pass it the EXE name, the target string, and the replacement string.  
>Obviously the target and replacement have to be the same length.  Also, 
>since you don't have any control over the order that strings get put in 
>the finished EXE, you want to make sure this string is unique so you 
>replace the right one (and so this function will fail if you ever run 
>it on the same EXE again). Notice too that this changes the file on
>disk, not the copy in memory (if any).  So if you make the EXE modify
>itself, you won't see the change until the next time you run it from
>disk (moreover, you won't be able to run multiple instances of the app
>--until you re-run it from disk--because the version in memory is different 
>from the version on disk). For this reason you probably want to have a
>separate "setup" program that writes into the EXE (this is what most
>commercial applications do).

>Like I said, actually writing into an EXE is a fairly risky thing to do.
>Be careful.

>Hope this helps.

>--
>Joe Robison
>joero@microsoft.com
>NAMS (Not A Microsoft Spokeshuman)

Thanks for the assistance and warnings.  I will give this a try.  Someone
also suggested I try declaring a global constant and assigning it a unique
string.  Then search the .EXE for this string and replace it.  Much the
same as your suggestion.

I'll let you know how it turns out.

Howie
Technical Details
Message-ID: <U=T.715267275@cansqnt>
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!utgpu!attcan!ncrcan!becker!censor!geac!torsqnt!hlpell
References: <U=T.714494455@cansqnt> <1992Aug30.200217.9872@microsoft.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!utgpu!attcan!ncrcan!becker!censor!geac!torsqnt!hlpell
From: hlpell@sequent.com (Howard Pell)
Newsgroups: comp.os.ms-windows.programmer.tools
Subject: Re: Can VB write over itself?
Message-ID: <U=T.715267275@cansqnt>
Date: 31 Aug 92 13:21:15 GMT
References: <U=T.714494455@cansqnt>  <1992Aug30.200217.9872@microsoft.com>
Sender: usenet@torsqnt.tor.sequent.com (Usenet News Owner)
Organization: Sequent Computer Systems (Canada) Ltd.
Lines: 70
Nntp-Posting-Host: cansqnt.tor.sequent.com