Solved

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

Posted on 2002-06-11
9
170 Views
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.
0
Comment
Question by:GeneM
9 Comments
 
LVL 2

Expert Comment

by:vbDoc
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.
0
 
LVL 20

Expert Comment

by:hes
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
    Loop
    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
0
 
LVL 3

Author Comment

by:GeneM
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".

GeneM

0
 
LVL 2

Expert Comment

by:vbDoc
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 38

Expert Comment

by:PaulHews
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))
       Loop
   Next i
   
   

End Sub
0
 
LVL 4

Expert Comment

by:gencross
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:

http://www.planetsourcecode.com./vb/scripts/ShowCode.asp?txtCodeId=10079&lngWId=1

PS: I received your email about the app and will be getting back to you.
0
 
LVL 2

Expert Comment

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

Accepted Solution

by:
jgv earned 100 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
0
 
LVL 3

Author Comment

by:GeneM
ID: 7071532
Thanks for all who contributed.

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

GeneM
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now