• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1530
  • Last Modified:

GetBinaryType API

Hi,
I know this is such a simple API to use but for some reason it always returns 0!

My code is:

Dim LngBuffer as long

retval = GetBinaryType("C:\file.exe", LngBuffer)

MsgBox retval
MsgBox LngBuffer

----

Both values return 0

How can i make it return the correct number for the binary type of file?

thanx
0
LeXien
Asked:
LeXien
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
Usually, if retval = 0 then the file was not found.  If retval is not 0 and LngBuffer = 0 then the program is a 32-bit program.

Another reason, though, for retval = 0, is if the file is not a recognizable format for an executable under Windows.

You do have the constants for LngBuffer values, correct?
0
 
LeXienAuthor Commented:
em,
I definatly gave a correct path to standard Visual Basic Win32 Executable. I think there may be a problem with the API call under Windows 98. According to the Win32 API Ref. it says NT supports it but 95/98 don't. Even though VB can declare the function perfectly fine, and it can be called perfectly fine, only it returns 0.
Is there any other method i could use?
0
 
Bob LearnedCommented:
When I was reviewing the GetBinaryType, I found that there is another routine that comes with Internet Explorer 4.0.  It is in the Shell32.DLL (SHGetFileInfo).

I feel that this question is worth a little more than 8 points.

The following is a routine to find EXE type:

'Place the following code in a module
'Calling convention:
'   sType = GetEXEType ("C:\VBCode\VBCode.exe")

Private Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" _
                              (ByVal pszPath As Any, _
                              ByVal dwFileAttributes As Long, _
                              psfi As SHFILEINFO, _
                              ByVal cbFileInfo As Long, _
                              ByVal uFlags As Long) As Long

' pszPath:
' Pointer to a buffer that contains the path and filename. Both absolute and
' relative paths are valid. If uFlags includes the SHGFI_PIDL, value pszPath
' must be the address of an ITEMIDLIST structure that contains the list of
' item identifiers that uniquely identifies the file within the shell's name space.
' This string can use either short (the 8.3 form) or long filenames.

' dwFileAttributes:
' Array of file attribute flags (FILE_ATTRIBUTE_ values). If uFlags does not
' include the SHGFI_USEFILEATTRIBUTES value, this parameter is ignored.

Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Const FILE_ATTRIBUTE_COMPRESSED = &H800

' psfi and cbFileInfo:
' Address and size, in bytes, of the SHFILEINFO structure that receives the file
' information.

' Maximum long filename path length
Private Const MAX_PATH = 260

Type SHFILEINFO   ' shfi
    hIcon As Long
    iIcon As Long
    dwAttributes As Long
    szDisplayName As String * MAX_PATH
    szTypeName As String * 80
End Type

' uFlags:
' Flag that specifies the file information to retrieve. If uFlags includes the SHGFI_ICON
' or SHGFI_SYSICONINDEX value, the return value is the handle to the system image
' list that contains the large icon images. If the SHGFI_SMALLICON value is included
' with SHGFI_ICON or SHGFI_SYSICONINDEX, the return value is the handle to the
' image list that contains the small icon images.

' If uFlags does not include SHGFI_EXETYPE, SHGFI_ICON, SHGFI_SYSICONINDEX,
' or SHGFI_SMALLICON, the return value is nonzero if successful, or zero otherwise.

' This parameter can be a combination of the following values:

' Modifies SHGFI_ICON, causing the function to retrieve the file's large icon.
Private Const SHGFI_LARGEICON = &H0&

' Modifies SHGFI_ICON, causing the function to retrieve the file's small icon.
Private Const SHGFI_SMALLICON = &H1&

' Modifies SHGFI_ICON, causing the function to retrieve the file's open icon. A
' container object displays an open icon to indicate that the container is open.
Private Const SHGFI_OPENICON = &H2&

' Modifies SHGFI_ICON, causing the function to retrieve a shell-sized icon. If
' this flag is not specified, the function sizes the icon according to the system
' metric values.
' The return value is *supposed to be* the handle of the system image list which
' could be passed to the ImageList_GetIconSize function to get the icon size.
' But the return value *is only* nonzero if successful, or zero otherwise.
Private Const SHGFI_SHELLICONSIZE = &H4&

' Indicates that pszPath is the address of an ITEMIDLIST structure rather than
' a path name.
Private Const SHGFI_PIDL = &H8&

' Indicates that the function should not attempt to access the file specified by
' pszPath. Rather, it should act as if the file specified by pszPath exists with
' the file attributes passed in dwFileAttributes. This flag cannot be combined
' with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, or SHGFI_PIDL flags.
Private Const SHGFI_USEFILEATTRIBUTES = &H10&

' Retrieves the handle of the icon that represents the file and the index of the
' icon within the system image list. The handle is copied to the hIcon member
' of the structure specified by psfi, and the index is copied to the iIcon member.

' The return value is *supposed to be* the handle of the system image list,
' .....boolean instead...!!!
' ** SHGFI_ICON creates a copy of the icon in memory. The DestroyIcon **
' ** function must be called to free any memory the icon occupied.          **
Private Const SHGFI_ICON = &H100&

' Retrieves the display name for the file. The name is copied to the szDisplayName
' member of the structure specified by psfi. The returned display name uses the
' long filename, if any, rather than the 8.3 form of the filename.
Private Const SHGFI_DISPLAYNAME = &H200&

' Retrieves the string that describes the file's type. The string is copied to the
' szTypeName member of the structure specified by psfi.
Private Const SHGFI_TYPENAME = &H400&

' Retrieves the file attribute flags. The flags are copied to the dwAttributes member
' of the structure specified by psfi. See the constants at the end of this file.
Private Const SHGFI_ATTRIBUTES = &H800&

' Retrieves the name of the file that contains the icon representing the file. The
' name is copied to the szDisplayName member of the structure specified by psfi.
Private Const SHGFI_ICONLOCATION = &H1000&

' Returns the type of the executable file if pszPath identifies an executable file.
' To retrieve the executable file type, uFlags must specify only SHGFI_EXETYPE.
' The return value specifies the type of the executable file:
' LowWord value       HighWord value         Type
' 0                                                           Nonexecutable file or an error condition.
' "NE" or "PE"          3.0, 3.5, or 4.0           Windows application
' "MZ"                      0                              MS-DOS .EXE, .COM or .BAT file
' "PE"                      0                              Win32 console application
Private Const SHGFI_EXETYPE = &H2000&

' Constants to represent the strings as ASCII char codes
Private Const EXE_WIN16 = &H454E    ' "NE"
Private Const EXE_DOS16 = &H5A4D  ' "MZ"
Private Const EXE_WIN32 = &H4550    ' "PE"
'private Const EXE_DOS32 = &H4543   ' "CE"

' Retrieves the index of the icon within the system image list. The index is copied to
' the iIcon member of the structure specified by psfi. The return value is the handle of
' the system image list.
Private Const SHGFI_SYSICONINDEX = &H4000&

' Modifies SHGFI_ICON, causing the function to add the link overlay to the file's icon.
Private Const SHGFI_LINKOVERLAY = &H8000&

' Modifies SHGFI_ICON, causing the function to blend the file's icon with the system
' highlight color.
Private Const SHGFI_SELECTED = &H10000

Public Function GetExeType(sFilePath As String)
 
Dim dwExeVal As Long
Dim shfi As SHFILEINFO
Dim dwLowWord As Long              ' exe type
Dim dwHighWord As Long
Dim bHighWordLowByte As Byte   ' minor version
Dim bHighWordHighByte As Byte  ' major version
Dim sRtn As String

  dwExeVal = SHGetFileInfo(ByVal sFilePath, 0&, shfi, Len(shfi), SHGFI_EXETYPE)
 
  ' Split the rtn val into it's associated parts
  dwLowWord = dwExeVal And &HFFFF&
  dwHighWord = (dwExeVal And Not &HFFFF&) \ &HFFFF&
  bHighWordLowByte = dwHighWord And &HFF&
  bHighWordHighByte = (dwHighWord And Not &HFF&) \ &HFF&
 
  ' Exe type. The EXE_ constants represent
  ' the strings in ASCII char codes
  Select Case dwLowWord
    Case 0:                  sRtn = "<not executable>"
    Case EXE_WIN16:  sRtn = "Win16"   ' "NE"
    Case EXE_DOS16: sRtn = "DOS"      ' "MZ"
    Case EXE_WIN32:  sRtn = "Win32"   ' "PE"
    Case Else:             sRtn = "<unknown executable>"
  End Select
 
  ' Version
  If bHighWordHighByte Then   ' major version
    sRtn = sRtn & ", v" & _
              Format(bHighWordHighByte, "#") & "." & _
              Format(bHighWordLowByte, "00")   ' minor version
  End If
 
  GetExeType = sRtn

End Function
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LeXienAuthor Commented:
Adjusted points to 18
0
 
LeXienAuthor Commented:
Thanks that worked so well. I just adjusted some of the strings and it worked like a treat. Thanks, i'll c ya around.
Others take note, this guy is great at providing good examples!

;)

The 18 points are yours - i increased them, cause i think u deserve them!
0
 
OakenheadCommented:
This still works 14 years later.

To update to ,Net, put it through the update wizard and add "Imports System.Runtime.InteropServices" at the top. Also, note that GetExeType returns a single-digit major version number; the leading digit of a two-digit number is in bHighWordLowByte. Lastly, you can add a EXE_WIN64 case in GetExeType.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now