Link to home
Start Free TrialLog in
Avatar of vbkann
vbkann

asked on

Exe composition

Inside a VB exe, how can i add data manually that will not affect the exe when it is run. ( i mean once it is compiled and can be run, if i open the exe in wordpad or something, where abouts in the exe can i add data that will not corrupt the file)

I tried to add it at the end but this did not work.

Please help.
Avatar of Erick37
Erick37
Flag of United States of America image

Writing data at the end of a file is permitted, since it outside the executable portion of the code.  Make sure you append the data using a binary editor or through code.  Notepad will not work.

You can use the DOS command COPY to append 2 files, or using VB code:

Private Sub Command1_Click()
    Dim sFileName As String
    Dim sBuffer As String
    Dim ff As Integer
   
    ff = FreeFile
    'Name of file to append data to
    sFileName = "c:\project1.exe"
    'String to append
    sBuffer = "Appended data"
    On Error GoTo ERRTRAP
    Open sFileName For Append Access Write As #ff
    Write #ff, sBuffer
    Close #ff
    Exit Sub
ERRTRAP:
    MsgBox Err.Description & vbCrLf & sFileName, , "ERROR " & Err.Number
    Exit Sub
End Sub
You can use resource editor and modify resources in your EXE - version info, icon and if you used .res file, any resource (strings, bitmaps, ...) used in that file.
Avatar of vbkann
vbkann

ASKER

sorry, i forgot to add this into the question:

"How can i then retrieve that data from the file. eg if i entered "Test1=hi,Test2=me" to the file how can i then get the values "hi" and "me" back. I presumed you should open the file as a string then use string manipulation to get the values - but i cant load the file to a string.

How can i do this please ( the values will sometimes be big)

thanks

p.s your answer so far is perfect - could you please tell me how to do this to finish the question off, thanks
If you modify the image, you must update the checksum in the PE header.

..B ekiM
mikeblas, I think you are talking to me, since I mentioned images (bitmaps).

If by checksum you mean file size, resource editor aligns size to 4K multiples when saving .EXE.
I just tried modifying splash screen and EXE works OK.

I also tried modifying a bitmap in VB6.EXE (our vb IDE). Interestingly, it works. Previous vb versions were 'protected' and I was getting checksum error: 'File has been modified'.

My guess is - PE header (whatever this is) is also modified in my resource editor (Borland Resource Workshop 4.5)
mikeblas is talking about the executable file which is referred to as an image. The checksum is a field in the PE header that contains a value computed from the contents of the file, if you change one byte it is a different checksum, and it serves to verify integrity.

If you add anything to the end of the file, you need to compute the updated checksum with the same algorith that Windows does, and then update it in the header or else the EXE won't load in NT or Win2k.

By the way, why do you need to modify the EXE? can;t you use the registry or an INI file?
>... won't load in NT or Win2k
Thanks for the info, luisr
Avatar of vbkann

ASKER

na, im making a compression program like a self extracting exe, except different.
> and then update it in the header or else the E
 > EXE won't load in NT or Win2k.

Right.  And by "image", I mean "executable image".

The PE Header is the header for all executable files in Win32, and is well-documented in MSDN. It describes the file format for EXE's and DLL's in Windows. (Seems like the first thing you should read, if you were talking about modifying an executable file!)

..B ekiM
> im making a compression program like a
 > self extracting exe, except different.

Then it seems to me like you shouldn't be modifying an existing EXE--you should be creating a new one from the ground up!

..B ekiM
Avatar of vbkann

ASKER

no...cause what i want to do is write all the code in vb...and have the code look at the data in its own exe ( which i add) and that data will be the files that i have compressed....but anyway..how can i get values from a file?

How can i search a file for a string..and retrieve the data that follows it..(look at my 2nd comment)
Can someone help me to do this...thanks
What you could do probably is to add a header preceding each file with certain signature, and then search for it, and when you find it, you know you've reached the beginning of a file.
Avatar of vbkann

ASKER

yes, thats exactly what im going to do, but how?

i need a piece of vb code that will show me how to open a file and get the data from it...Can you do this please?
Avatar of vbkann

ASKER

ill reject your answer for the moment unless you can totally answer the question eric37...i will devide the points if someone else can answer
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vbkann

ASKER

ok this is good but i want to be able to store data in the file and then retrieve it and use it in the extraction process.

eg where to extract the temp file to ...
For this i would add "ExtractPath=C:\temp" to the end of the exe. Now how can i retrieve this value using vbcode. In other words, how can i open a file and search for data then retrieve the data that follows it.

Do you know what i mean? like a header or whatever..

Please provide code
Here you go...

After you've add the information buffer to the end of the EXE, here's an example of how to get it back...

Remember to change the constant AttachedBufferSize to be the size of the buffer you are adding to the end of the EXE...  I would make it a fixed size...  In my example, I am using a size of 80.  This means that I stored something like:

   "ExtractPath=C:\temp" and padded the end of the string with spaces so the buffer I stored is 80 characters... Get the idea??


Cheers!


THE CODE:


    Private Sub Command1_Click()
        'THIS CONSTANT MUST BE CHANGE TO THE SIZE
        'OF THE ATTACHED BUFFER IN BYTES
        Const AttachedBufferSize = 80
         
        Dim MySelfFnum As Long
        Dim MySelfExe As String
        Dim MySelfSize As Long
        Dim lByte As Byte
        Dim AttachedBuffer As String
         
        'BUILD THE PATH TO MYSELF
        MySelfExe = App.Path
        If Not Right$(MySelfExe, 1) = "\" Then MySelfExe = MySelfExe + "\"
        MySelfExe = MySelfExe + App.EXEName + ".EXE"
         
        'GET A FREE FILENUMBER TO OPEN MYSELF AND OPEN MYSELF
        MySelfFnum = FreeFile
        Open MySelfExe For Binary Access Read As MySelfFnum
         
         
        'FIND THE START OF THE ATTACHED BUFFER
        MySelfSize = LOF(MySelfFnum)
        Seek MySelfFnum, MySelfSize - AttachedBufferSize + 1
         
        'EXTRACT THE ATTACHED BUFFER
        AttachedBuffer = ""
        Do
            Get #MySelfFnum, , lByte
            If EOF(MySelfFnum) = True Then Exit Do
            AttachedBuffer = AttachedBuffer + Chr$(lByte)
        Loop
         
        'CLOSE THE EXE
        Close MySelfFnum
       
        'SHOW THE ATTACHED BUFFER IN A MSGBOX
        MsgBox AttachedBuffer
    End Sub
vbkann,

Just checking back... You still there?


Cheers!
Avatar of vbkann

ASKER

yeh sorry, thanks for reminding me
Thanks for the points! Glad I could help!


Cheers!