Need to load an EXE file so I can scan it looking for a char string

Hi all,

I need to load an EXE file (about 1.5 meg) so that I can scan it looking for the character string "VERSION ".  I am having a little trouble figuring out how to open and read the EXE file.   Can I suck the whole file into memory at one time?  Any help will be appreciated.  Is there an easier way to accompolish this?

As always, thanks for any help.
Who is Participating?
Here's a quick and dirty method that you can play with.

Dim strRaw As String
Dim strFind As String
Dim lngPos As Long

strFind = "VERSION"

Open "C:\My Documents\MyExe.exe" For Binary As #1
    'Read the file into a variable
    strRaw = Input(LOF(1), 1)
Close #1

'Find the first occurance of the string
lngPos = InStr(1, strRaw, strFind)

If lngPos > 0 Then
    'Add the length of the string to the start position
    lngPos = lngPos + Len(strFind)
    'Exract 3 characters to the right of the string
    MsgBox Mid(strRaw, lngPos, 3)
End If
Here is the simple way. In windows right click on START and select SEARCH. You might have to look for advance search, but you can search a file for a given word. I beleve that it will search an EXE as well as an TXT file. If not I have code to search an EXE file.
Declare in a module

Public Declare Function GetFileVersionInfo Lib "Version.dll" Alias "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal dwhandle As Long, ByVal dwlen As Long, lpData As Any) As Long

Private Declare Function GetFileVersionInfoSize Lib "Version.dll" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long

Private Declare Function VerQueryValue Lib "Version.dll" Alias "VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Any, puLen As Long) As Long

Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, ByVal Source As Long, ByVal Length As Long)

Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long

Public Type FILEINFO
    CompanyName As String
    FileDescription As String
    FileVersion As String
    InternalName As String
    LegalCopyright As String
    OriginalFileName As String
    ProductName As String
    ProductVersion As String
End Type

Public Enum VerisonReturnValue
    eOK = 1
    eNoVersion = 2
End Enum

Public Function GetFileVersionInformation(ByRef pstrFieName As String, ByRef tFileInfo As FILEINFO) As VerisonReturnValue
    Dim lBufferLen As Long, lDummy As Long
    Dim sBuffer() As Byte
    Dim lVerPointer As Long
    Dim lRet As Long
    Dim Lang_Charset_String As String
    Dim HexNumber As Long
    Dim i As Integer
    Dim strTemp As String
    'Clear the Buffer tFileInfo
    tFileInfo.CompanyName = ""
    tFileInfo.FileDescription = ""
    tFileInfo.FileVersion = ""
    tFileInfo.InternalName = ""
    tFileInfo.LegalCopyright = ""
    tFileInfo.OriginalFileName = ""
    tFileInfo.ProductName = ""
    tFileInfo.ProductVersion = ""
    lBufferLen = GetFileVersionInfoSize(pstrFieName, lDummy)

    If lBufferLen < 1 Then
        GetFileVersionInformation = eNoVersion
        Exit Function
    End If
    ReDim sBuffer(lBufferLen)
    lRet = GetFileVersionInfo(pstrFieName, 0&, lBufferLen, sBuffer(0))

    If lRet = 0 Then
        GetFileVersionInformation = eNoVersion
        Exit Function
    End If
    lRet = VerQueryValue(sBuffer(0), "\VarFileInfo\Translation", lVerPointer, lBufferLen)

    If lRet = 0 Then
        GetFileVersionInformation = eNoVersion
        Exit Function
    End If
    Dim bytebuffer(255) As Byte
    MoveMemory bytebuffer(0), lVerPointer, lBufferLen
    HexNumber = bytebuffer(2) + bytebuffer(3) * &H100 + bytebuffer(0) * &H10000 + bytebuffer(1) * &H1000000
    Lang_Charset_String = Hex(HexNumber)
    'Pull it all apart:
    '04------= SUBLANG_ENGLISH_USA
    '--09----= LANG_ENGLISH
    ' ----04E4 = 1252 = Codepage for Windows
    '     :Multilingual

    Do While Len(Lang_Charset_String) < 8
        Lang_Charset_String = "0" & Lang_Charset_String
    Dim strVersionInfo(7) As String
    strVersionInfo(0) = "CompanyName"
    strVersionInfo(1) = "FileDescription"
    strVersionInfo(2) = "FileVersion"
    strVersionInfo(3) = "InternalName"
    strVersionInfo(4) = "LegalCopyright"
    strVersionInfo(5) = "OriginalFileName"
    strVersionInfo(6) = "ProductName"
    strVersionInfo(7) = "ProductVersion"
    Dim buffer As String

    For i = 0 To 7
        buffer = String(255, 0)
        strTemp = "\StringFileInfo\" & Lang_Charset_String _
        & "\" & strVersionInfo(i)
        lRet = VerQueryValue(sBuffer(0), strTemp, _
        lVerPointer, lBufferLen)

        If lRet = 0 Then
            GetFileVersionInformation = eNoVersion
            Exit Function
        End If
        lstrcpy buffer, lVerPointer
        buffer = Mid$(buffer, 1, InStr(buffer, vbNullChar) - 1)

        Select Case i
            Case 0
            tFileInfo.CompanyName = buffer
            Case 1
            tFileInfo.FileDescription = buffer
            Case 2
            tFileInfo.FileVersion = buffer
            Case 3
            tFileInfo.InternalName = buffer
            Case 4
            tFileInfo.LegalCopyright = buffer
            Case 5
            tFileInfo.OriginalFileName = buffer
            Case 6
            tFileInfo.ProductName = buffer
            Case 7
            tFileInfo.ProductVersion = buffer
        End Select
Next i
GetFileVersionInformation = eOK
End Function
Where you want the version

Dim strFile As String
Dim udtFileInfo As FILEINFO

If GetFileVersionInformation("Your File", udtFileInfo) = eNoVersion Then
        MsgBox "No version available For this file", vbInformation
        Exit Sub
    End If
    Label1 = "Company Name: " & udtFileInfo.CompanyName & vbCrLf
    Label1 = Label1 & "File Description:" & udtFileInfo.FileDescription & vbCrLf
    Label1 = Label1 & "File Version:" & udtFileInfo.FileVersion & vbCrLf
    Label1 = Label1 & "Internal Name: " & udtFileInfo.InternalName & vbCrLf
    Label1 = Label1 & "Legal Copyright: " & udtFileInfo.LegalCopyright & vbCrLf
    Label1 = Label1 & "Original FileName:" & udtFileInfo.OriginalFileName & vbCrLf
    Label1 = Label1 & "Product Name:" & udtFileInfo.ProductName & vbCrLf
    Label1 = Label1 & "Product Version: " & udtFileInfo.ProductVersion & vbCrLf
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

GeneMAuthor Commented:
vbDoc, I want to do this search from a program.  Thus, I would need your program code.

hes, I really don't know if the Version information is the project property version as we know it in VB.  I don't even know if the EXE is a VB program.  All I know is that I can look at the EXE and find the word VERSION followed by a number.  The number is the information I need.  It is not of the form nn.nn.nn or anything like that, so I think it is a constant in the code itself.

I had really hoped someone would tell me OPEN "FILE.EXE" for something as #1; then (get or read or input); then use the Instr function to find my keyword "VERSION".


Ok, I found a great VB Application in Search on 'search binary file' in the VB section. The prject name is 'Binary Viewer'. I got it up and running and searched through an EXE file and it works. I did have to remove the 'Command Line Arguments' Menu>Project>Properties>Make
Any More? Let me know, Regards John
This code actually does a search/replace in a binary file.  It can be modified pretty easily.  It would help if you know what format the version number is in the exe. (string?, byte?, integer?, long? other?)  If you look at it in a hex editor, it might give you some idea.

Public Sub sReplaceInFile(strToReplace As String, strReplaceWith As String, strFilename As String)
   Dim strChunk As String
   Dim hFile As Integer
   Dim i As Long
   Const ChunkSize = 4096
   Dim strInput As String
   Dim lngNumChunks As Long
   Dim intNumBytesLeftOver As Integer
   Dim lngPos As Long, lngFP As Long
   lngNumChunks = FileLen(strFilename) \ ChunkSize
   intNumBytesLeftOver = FileLen(strFilename) Mod ChunkSize
   strInput = Space(Len(strReplaceWith))
   strChunk = Space(ChunkSize)
   hFile = FreeFile
   Open strFilename For Binary As #hFile
   For i = 1 To lngNumChunks + 1
       If i = lngNumChunks + 1 Then
           strChunk = Space(intNumBytesLeftOver)
       End If
       Get #hFile, (i - 1) * ChunkSize + 1, strChunk
       lngPos = InStr(1, strChunk, Left$(strToReplace, 1))
       Do While lngPos > 0
           lngFP = lngPos + (i - 1) * ChunkSize
           Get #hFile, lngFP, strInput
           If StrComp(strInput, strToReplace) = 0 Then  ''equal
               Put #hFile, lngFP, strReplaceWith
           End If
           lngPos = InStr(lngPos + 1, strChunk, Left$(strToReplace, 1))
   Next i

End Sub
Doc, just a note...  It is much easier to paste the link to the code snippet from PSC (or anywhere) and in the comment.  

This is the one for the Binary Viewer:

PS: I received your email about the app and will be getting back to you.
Oh, thanks for the info. Just an OLD DOG awaiting for new tricks.
GeneMAuthor Commented:
Thanks for all who contributed.

I wanted something simple, and that is what jgv provided.  I have therefore accepted his solution.

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.