Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2002-06-11
Medium Priority
Last Modified: 2010-05-02
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.
Question by:GeneM

Expert Comment

ID: 7070484
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.
LVL 20

Expert Comment

ID: 7070686
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

Author Comment

ID: 7070809
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".


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 7071050
Ok, I found a great VB Application in www.planetsourcecode.com. 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
LVL 38

Expert Comment

ID: 7071128
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

Expert Comment

ID: 7071131
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.

Expert Comment

ID: 7071158
Oh, thanks for the info. Just an OLD DOG awaiting for new tricks.
LVL 12

Accepted Solution

jgv earned 400 total points
ID: 7071462
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

Author Comment

ID: 7071532
Thanks for all who contributed.

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


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question