I have just been playing around trying different things and thought I would get people's thoughts on it (not that anyone will be interested).
I have found that you can append a lot of plain text on just about any file without harming the usefulness of the files. (bmp, jpg, gif, dll and even exe).
I found that nothing prevents you from appending text to the open app's exe file and reading from it. So basically you could store the settings in the exe file by just appending it. (I imagine you would have to be very careful not to erase any part of the file).
Didn't work for me. I made a crude BMP in MS Paint and added "blahblahblah" to the end, it corrupted the file. I didn't think it would work either, because of the binary-text representation and interchange going on. Got any small examples of what you did? (Give us .jpg or .bmp, no .exes)
I opened that file in notepad, then immediately saved it without modifying anything. That corrupted the image. I suppose the next question would be, what did you use to add the text?
Yes if you save it in a text file it will corrupt. It is still a binary file.
This,
Open "MyFile.bmp" for append as #1
? #1, "This is my string."
Close #1
This appends the text.
Now open the gif in notepad and you will see it at the bottom.
Close notepad without saving and open the gif in IE.
The gif looks fine.
I found that you can't save to the running app (since the app is write protected when it is running), but you can read from it.
So if I had a key or something that I had appended to my EXE I can have code in that app read the end of the app to get the key (or whatever).
The big thing is you must always add the information after the end of the file, if you put it anywhere else it will mess it up (JPG and maybe Gif you can, since you can change a bit data - but it will cause the picture to change).
Mike
Last edited by MikeJoel; Mar 15th, 2008 at 05:55 PM.
Here is a quick app to see it at work (it was done quickly so please no comments on the coding )
The form should have two command buttons and 1 text box set to multiline.
Also place a BMP file (test.bmp) in the folder with the app.
Code:
Dim MyString As String * 45
Private Sub Command1_Click()
Dim StartArea As Long
StartArea = GetStartPos(App.Path & "\test.bmp")
Open App.Path & "\test.bmp" For Append As #1
If (StartArea > 0) Then
Seek #1, StartArea
Else
MyString = "[START]"
Print #1, MyString
End If
MyString = "This is line " & Int(Rnd * 2000)
Print #1, MyString
MyString = "This is line " & Int(Rnd * 2000)
Print #1, MyString
MyString = "This is line " & Int(Rnd * 2000)
Print #1, MyString
MyString = "This is line " & Int(Rnd * 2000)
Print #1, MyString
Close #1
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Dim StartArea As Long
Dim LineIne As String
Dim FindStart As String
StartArea = GetStartPos(App.Path & "\test.bmp")
Open App.Path & "\test.bmp" For Input As #1
If (StartArea > 0) Then
Seek #1, StartArea
End If
Do Until EOF(1)
Input #1, MyString
Text1.Text = Text1.Text & Trim(MyString) & vbCrLf
Loop
Close #1
End Sub
Public Function GetStartPos(MyFile As String) As Long
Dim LineIne As String
Dim FindStart As String
Dim StartPos As Long
Dim AFreeFile As Integer
AFreeFile = FreeFile
StartPos = 0
Open App.Path & "\test.bmp" For Input As #AFreeFile
Do Until EOF(AFreeFile)
If (InStr(1, FindStart, "[START]") <= 0) Then
Input #AFreeFile, LineIne
FindStart = FindStart & LineIne
Else
StartPos = Seek(AFreeFile)
Exit Do
End If
Loop
Close #AFreeFile
GetStartPos = StartPos
End Function
It checks if the file has a "[START]" string in it. If not it will be the first item written in. From then on it uses that as a reference for reading and writing.
Using another method would probably be faster but, like I said, I just did this up as an example.