home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / mswindo / programm / tools / 865 < prev    next >
Encoding:
Internet Message Format  |  1992-09-04  |  3.4 KB

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