Solved

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

Posted on 2002-06-11
9
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…
Suggested Courses
Course of the Month4 days, 13 hours left to enroll

636 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