Visual Basic > Files Directories Drives
Determine if file is a Windows or DOS executable
Determine if file is a Windows or DOS executable To determine if an executable has been written for Windows or DOS you need to check byte offset 24 in the file. If it contains 40h, it's a Windows executable. Option Explicit 'Purpose : Determine if an executable has been written for Windows or DOS. 'Inputs : sFilePath The path and name of the executable. 'Outputs : Returns 1 if the file is a windows executable. ' Returns 0 if the file is a DOS executable. ' Returns -1 if the file doesn't exist. Function FileWindows(ByVal sFilePath As String) As Long Dim lhwndFile As Integer Dim sTest As String * 1 'Buffer If Len(sFilePath) > 0 And Len(Dir$(sFilePath)) > 0 Then lhwndFile = FreeFile Open sFilePath For Binary As #lhwndFile Get lhwndFile, 25, sTest Close #lhwndFile FileWindows = Abs(Asc(sTest) = &H40&) Else 'File doesn't exist FileWindows = -1 End If End Function 'Demonstration routine Sub Test() Select Case FileWindows("C:\FootballAid.exe") Case 1 MsgBox "Windows file" Case 0 MsgBox "DOS file" Case -1 MsgBox "file doesn't exist" End Select End Sub
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows