Exe content

With this code, I can read the content of a line in any executable.
Note: This example required one TextBox and one CommonDialog1

Open FileName For Binary As #Free
ReDim EXEContent(1 To LOF(Free))
Get #Free, , EXEContent
Close #Free

Text1.Text = ""
For i = 79 To 118
Text1.Text = Text1.Text + Chr(EXEContent(i))
Next i
End If

The result: This program cannot be run in DOS mode.

Now my question is:
How can i read some other content of the executable but not 79?
I have try so many times with diferent numbers from 1 to 400 but don't work for me.

I want to read some other content that is not " 79=This program cannot be run in DOS mode. "

For example:

And so on.

Any help will be apreciate.

Who is Participating?
fostejoConnect With a Mentor Commented:

The line


is definately valid in VB - I've just copied from this web page and pasted it into a new VB6 project and it runs as expected.  If it turns red for you than you've perhaps not entered/pasted it correctly? - copy it from just above and it should be fine.

The main issue you've got with trying to get "lines" 1,2,3...n from an executable is that the physical position of the text you want will usually vary and as a consequence, if you hard-code the correct numbers for Executable 'A', they'll more than likely be wrong for Executable 'B'    Therefore, you need to either:

1. Have a generic routine that looks through the specified executable and *attempts* to identify what may be blocks of text within it (this is what FileAlyzer does as per my first post - I mention it because this will show you what you should be able to get out of a specific executable)   The trouble with this approach (and the reason I'm not giving a full example here!) is that it can get quite complex to do properly as you then need to start worrying about *how* to identify a block of text - where it starts, where it ends etc.


2. Have a list of the specific text items you want to find within a specified executable and just search for them - for example, this code will tell you whether the piece of text in 'strToFind' exists within Notepad.exe

free = FreeFile
Open "c:\windows\notepad.exe" For Binary As #free
t$ = String$(LOF(free), 0)
Get #free, , t$
Close #free

strToFind = ".data"

lngPos = InStr(UCase(t$), UCase(strToFind))
If lngPos > 0 Then
    Text1.Text = strToFind + " found at byte" + Str(lngPos) + " onwards"
    Text1.Text = strToFind + " could NOT be found!"
End If

Hope that helps..


I suspect you're attempting to dump out all the textual information from the executable - the problem you're encountering is that only a proportion of the executable actually consists of characters that are printable.

Altering your current code, something like the following should help:

Open FileName For Binary As #Free
ReDim EXEContent(1 To LOF(Free))
Get #Free, , EXEContent
Close #Free

Text1.Text = ""
For i = 1 To UBound(EXEContent)  '<- ALTERED
    strC=EXEContent(i) '<-NEW
        Text1.Text = Text1.Text + strC '<- ALTERED
    END IF '<-NEW
Next i

As well as looping through all of the EXEContent array, the additional 'IF INSTR..' statement will only append those characters listed as part of the INSTR onto text1.text (obviously, that list of 'printable characters' can be altered to include other valid characters such as the @ sign for instance)

For simplicity, the instr also only tests against the uppercase value of strC (rather than a-z also) - though text1.text is appended with the correctly cased character.

Also, to help you know what to expect when you dump out the textual info from a file, you might want to have a look at FileAlyzer at http://www.safer-networking.org/en/filealyzer/index.html - this allows you to look at the 'internals' of a file in HEX dump mode, and also to list all the printable strings within it.

hope that helps,
DazmAuthor Commented:

Fostejo: As well as looping through all of the EXEContent array,

Dazm: I don't want to looping through all of the exe content array,
I forgot to mention that all i need it was just a line of content
I just want to read a single line of the exe content in the Textbox.
Thanks for the suggestion about the altering the characters in the exe
But some how didn't work, I get the following error message...

strC = EXEContent(i) '<-NEW

Error: Variable not defined


Error: Expected: list separator
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Hi Dazm,

Let's correct the errors first;

The "Variable not defined" error is because you're using 'Option Explicit' elsewhere in the code - to get it working you'll need to add a "dim strC" somewhere above the erroring line (the 'Option Explicit' forces you to declare a variable before you use it and is good programming practice)

The "Expected: list separator" is due to a mistype in the line, change the erroring line to the following (notice the addition of the double quote!):


When you say you just want "a line of content" then you could stick to the method you've already been using (ie. FOR i=79 TO 118) but this obviously limits you to that specific batch of bytes in the executable.

Is your intention to get the 'first' batch of text found in the executable into the textbox and then a second batch and then a third batch - controlled by the user perhaps?

DazmAuthor Commented:

This project am working on, is for my own use, maybe nothing important,
But my intention is to get an specific line of the content of the exe file by the
Corresponding line number

Line 1: This program cannot be run in DOS mode.
Line 2: .text
Line 3: .data
Line 4: .rsrc

But not all at once
I would like to read line by line in case i want to jump to a specific line number

Now going back to you post...
Look, it sounds to me that it is not about the  'Option Explicit' or the "dim strC"
cus i thought about this too, then i try to correct the error, but nothing happen.


Just by adding this piece of code into the project, it turns red. Then vb don't let me continue

With this => "For i = 399 To 445" i get the second line of the exe content which is => ".text"

I believe there may be a way to get the line 3, 4, 5, 6, etc. and so on.

At the very beginning i was trying to get this by using hex address, but i couldn’t make it work.
DazmAuthor Commented:

Thanks Fostejo : )

This is even better than the one i was trying to make.

Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.