home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.ms-windows.programmer.tools
- Path: sparky!uunet!microsoft!hexnut!joero
- From: joero@microsoft.com (Joe Robison)
- Subject: Re: Can VB write over itself?
- Message-ID: <1992Aug30.200217.9872@microsoft.com>
- Date: 30 Aug 92 20:02:17 GMT
- Organization: Microsoft Corp.
- References: <U=T.714494455@cansqnt>
- Lines: 59
-
- 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)
-