What is the best way to embed binary file information into the program while in development mode, so that it gets compiled into the EXE ???
For example: I write a lot of programs that use IO.DLL, and because I'm strange, I prefer to keep the DLL in the install directory of the program and leave it unregistered in the system, which does work. If I wanted to embed that into the EXE so that when the program starts, it checks if the DLL has been corrupted or lost, then will spawn a new one (Open for binary as #1, Put #1.....). This may be a poor example, but I can remember a number of times that I would have liked to embed files in the EXE so that they could be created at will.
Another example would be having large binary arrays for lookup tables without trying to define it with Array(12,65,23,98,34,114,.....). The table could be dumped to a file or possibly read from the containing control.
If you have ever worked with Assembly or Firmware programming, you know that it is relatively easy to store misc. raw data into the program and read it from memory as a reference. I guess this is sort of what I was looking for.
I don’t know what would happen if you cut/paste the data to a hidden text box of a form while in the IDE - I haven’t tried it yet, but I would assume that there would be a problem reading back the binary information from the textbox and get it byte for byte perfect.
Has anyone tried this before ?? Is there a control that could work as a PictureBox for binary data ?? (Actually come to think of it, I guess you could create a bitmap with the pixels representing the binary data of a file, and use a PictureBox)
Any ideas on a clean way to do this ??? What ever the solution, I would like to be able to access the data from code.
Resource files are designed for just this sort of thing. The only problem with doing it like that is there is a fairly small limit to the size of the files you can store. But within reason, i supposes you could have a file split up into several entries in a resource file to overcome this.
I tried saving a small EXE (16K) into a resource file as a custom resource, then tried to "Put" it to a binary file. When I tried to execute the EXE, windows generated the error "C:\Spawn.exe is not a valid Win32 application."
I used:
Code:
Open "C:\Spawn.exe" For Binary As #1
Put #1, , LoadResData(500, "Custom")
Close 1
If I tried to open the EXE for binary and used the "Get" command to read the data to a string, then wrote it to a new file with the "Put" command, then the Spawn'ed program would execute just fine.
Code:
Private Sub Command1_Click()
Dim Test As String
Open App.Path & "\Project1.exe" For Binary As #1
Test = Space(LOF(1))
Get #1, , Test
Close 1
Open "C:\Spawn.exe" For Binary As #1
Put #1, , Test
Close 1
End Sub
(I used an exe, because I figured that this would be a better test to see if the contents were restored perfectly. I haven't tried it with the DLL)
oh, techman2553. you can not declare the buffer as string. It must be Byte array:
Code:
Sub main()
Dim Buff() As Byte
If MsgBox("Continue to extract the notepad.exe into C drive", vbQuestion + vbYesNo, "SelfExtract") = vbYes Then
Buff = LoadResData(101, "CUSTOM")
Open "C:\notepad.exe" For Binary As #1
Put #1, 1, Buff
Close #1
End If
End
End Sub
OK, I decided to try and encode files within a bitmap, and I got it to work....
The enclosed program will encode any type of file up to 16MB into a bitmap, then the bitmap can be added to a hidden PictureBox control in a project, which can be compiled to an EXE, and finally the EXE can regenerate the file through code. This creates a perfect byte by byte record of the entire file, and so can be used to store EXE, INI, HLP, WAV, MOV, DLL, etc.
Although this technique is really screwy, it will work if you absolutely had to add a file that is larger then 64K to an EXE.
The format goes like this:
Take the file size in bytes, add 3 bytes to make room for encoding the file length, divide that number by 3 to figure that each pixel will contain 3 bytes, then take the square root of that and add two to make sure there is enough room. The resulting value is now the length and width of the bitmap.
For example:
29997 byte file + 3byte length = 30,000 bytes
30,000 bytes / 3 = 10,000 pixels
SQR(10,000 pixels) = 100 pixel x 100 pixel bitmap
+2 to make it 102 x 102 pixels (accounts for rounding of SQR, etc.)
Now write the first pixel color as the size of the file
Pixel 0 color = 29997
Next, combine the hex values of the first 3 bytes of the file to create the color for pixel 1
If:
byte 0 of the file = &H23
byte 1 of the file = &H7C
byte 2 of the file = &H44
Then:
Pixel 1 color = &H447C23
Pixel 2 will contain bytes 3-5, pixel 3 will contain bytes 6-8, etc.
Note that when a file is converted to a bitmap, the bitmap will become at least 133% the size of the original file. This is because each pixel is actually made of 4 bytes not 3, but the fourth byte will only except 80 hex as a flag for system colors. If you write any other number to the fourth byte of the pixel color, then the color value will default to zero.
There's a few reasons why someone would want to store data within an EXE rather than a data file, least of all security. Assuming the data was encrypted wherever it was stored in the file, it would be next to impossible to recognise and retrieve from the file if stored well :-)
Another obvious reason is so that there's just one file rather than a bunch of them, and it'd run standalone
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
VB Standalone... That's an oxymoron!!! I would be pissed to have a program that took up large amounts of memory just to store data that it MIGHT one day need to use. There are better ways to use memory and that is not one of them.
You know the old saying...horses for courses...you might not want to store the data within the EXE, but he does...I personally wouldn't either, but in this case... :-)
As long as programmers give him possible alternative solutions while giving him a possible way to do what he wants, we're helping
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.