home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / mswindo / programm / tools / 801 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.8 KB  |  70 lines

  1. Newsgroups: comp.os.ms-windows.programmer.tools
  2. Path: sparky!uunet!microsoft!hexnut!joero
  3. From: joero@microsoft.com (Joe Robison)
  4. Subject: Re: Can VB write over itself?
  5. Message-ID: <1992Aug30.200217.9872@microsoft.com>
  6. Date: 30 Aug 92 20:02:17 GMT
  7. Organization: Microsoft Corp.
  8. References: <U=T.714494455@cansqnt> 
  9. Lines: 59
  10.  
  11. In article <U=T.714494455@cansqnt> hlpell@sequent.com wrote:
  12. > I want to have my software product prompt the user for his/her name and the
  13. > registration number and then store this in a safe place, as do most
  14. > software packages.  Can Visual Basic overwrite its own .EXE file to store
  15. > this info?  That is, can I set aside an area of the .EXE that will be used
  16. > exclusively for this purpose?  Then if the setup program sees that this
  17. > area has data in it, it will not prompt the user to re-enter his/her name
  18. > and registration number.
  19. > Any and all help appreciated.
  20.  
  21. Well, doing this gives me the heebee-geebies (is that how you spell that?)
  22. but here you go:
  23.  
  24. Function ReplaceTextinEXE (EXE As String, Target As String, replace As String)
  25. Dim f As Integer, i As Long, X As String
  26.     If Len(Target) <> Len(replace) Then Exit Function
  27.     f = FreeFile
  28.     If UCase$(Right$(EXE, 4)) <> ".EXE" Then EXE = EXE + ".EXE"
  29.     Open "foo.exe" For Binary Access Read Write Shared As f
  30.     For i = 1 To LOF(f) - Len(Target)
  31.         X = String$(Len(Target), " ")
  32.         Get #f, i, X
  33.         If X = Target Then
  34.             Put #f, i, replace
  35.             ReplaceTextinEXE = -1
  36.             Close #f
  37.             Exit Function
  38.         End If
  39.     Next
  40. End Function
  41.  
  42.  
  43. Essentially what you want to do is store a target string somewhere in
  44. your application (an invisible label on a form, in the tag property of
  45. any control, even as literal in your code).  Then you call this function 
  46. and pass it the EXE name, the target string, and the replacement string.  
  47. Obviously the target and replacement have to be the same length.  Also, 
  48. since you don't have any control over the order that strings get put in 
  49. the finished EXE, you want to make sure this string is unique so you 
  50. replace the right one (and so this function will fail if you ever run 
  51. it on the same EXE again). Notice too that this changes the file on
  52. disk, not the copy in memory (if any).  So if you make the EXE modify
  53. itself, you won't see the change until the next time you run it from
  54. disk (moreover, you won't be able to run multiple instances of the app
  55. --until you re-run it from disk--because the version in memory is different 
  56. from the version on disk). For this reason you probably want to have a
  57. separate "setup" program that writes into the EXE (this is what most
  58. commercial applications do).
  59.  
  60. Like I said, actually writing into an EXE is a fairly risky thing to do.
  61. Be careful.
  62.  
  63. Hope this helps.
  64.  
  65. --
  66. Joe Robison
  67. joero@microsoft.com
  68. NAMS (Not A Microsoft Spokeshuman)
  69.