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
Solved

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

Posted on 2002-06-11
9
173 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 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