It is currently Thu Dec 12, 2019 7:13 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Determine Processor Arch. of Win PE (Portable Executable)
PostPosted: Fri Aug 24, 2018 9:02 am 
Offline
User
User

Joined: Wed Apr 02, 2014 12:45 am
Posts: 29
Hello,

This short code can tell you whether a Windows PE file (*.exe) is 32-bit or 64-bit, using the built-in constants #PB_Processor_X64 and #PB_Processor_X86 as return values.

Code:
EnableExplicit

#IMAGE_DOS_SIGNATURE = "5A4D"
#IMAGE_NT_SIGNATURE = "4550"
#IMAGE_FILE_MACHINE_X86 = "14C"
#IMAGE_FILE_MACHINE_X64 = "8664"

Procedure.i Get_PE_Architecture(exeFilePath.s)
  Protected Result.i = 0 ; File Not Exist / Not a PE
  If FileExists(exeFilePath)
    If ReadFile(0, exeFilePath)
      Protected *fBuff = AllocateMemory(1024)
      ReadData(0, *fBuff, 1024)
      CloseFile(0)
      Protected *imageDosHeader.IMAGE_DOS_HEADER = *fBuff
      Protected *imageNTHeaders.IMAGE_NT_HEADERS = *fBuff + *imageDosHeader\e_lfanew
      ;Protected *imageSectionHeaders.IMAGE_SECTION_HEADERS = *imageNTHeaders\OptionalHeader + *imageNTHeaders\FileHeader\SizeOfOptionalHeader
      If Hex(*imageDosHeader\e_magic, #PB_Word) = #IMAGE_DOS_SIGNATURE And Hex(*imageNTHeaders\Signature, #PB_Word) = #IMAGE_NT_SIGNATURE
        Select Hex(*imageNTHeaders\FileHeader\Machine, #PB_Word)
          Case #IMAGE_FILE_MACHINE_X64
            Result = #PB_Processor_x64 ; 4
          Case #IMAGE_FILE_MACHINE_X86
            Result = #PB_Processor_x86 ; 2
        EndSelect
      EndIf
      FreeMemory(*fBuff)
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure


Last edited by bgeraghty on Fri Aug 24, 2018 9:13 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Determine Processor Arch. of Win PE (Portable Executable
PostPosted: Fri Aug 24, 2018 9:08 am 
Offline
User
User

Joined: Wed Apr 02, 2014 12:45 am
Posts: 29
*

Code:
Procedure.b FileExists(FileFullPath.s, IsFolder.b=#False) ;BOOL File Exists, (Or Folder)
  Protected Result.b = #False
  Select FileSize(FileFullPath)
    Case -2
      If IsFolder : Result = #True : Else : Result = #False : EndIf
    Case -1
      Result = #False
    Default
      If IsFolder : Result = #False : Else : Result = #True : EndIf
  EndSelect
  ProcedureReturn Result
EndProcedure


Top
 Profile  
Reply with quote  
 Post subject: Re: Determine Processor Arch. of Win PE (Portable Executable
PostPosted: Fri Aug 24, 2018 9:09 am 
Offline
Moderator
Moderator
User avatar

Joined: Thu Dec 31, 2009 11:05 pm
Posts: 1108
Location: Berlin (Germany)
Thanks for the code. I only knew the WinAPI solution: GetBinaryType_()

Example:
Code:
EnableExplicit

;Define lpApplicationName$ = "C:\...\YourFile.exe"
Define lpApplicationName$ = ProgramFilename()
Define lpBinaryType

#SCS_32BIT_BINARY = 0
#SCS_64BIT_BINARY = 6
#SCS_DOS_BINARY = 1
#SCS_OS216_BINARY = 5
#SCS_PIF_BINARY = 3
#SCS_POSIX_BINARY = 4
#SCS_WOW_BINARY = 2

GetBinaryType_(@lpApplicationName$,@lpBinaryType)

Select lpBinaryType
  Case #SCS_32BIT_BINARY
    MessageRequester("","A 32-bit Windows-based application.",0)
  Case #SCS_64BIT_BINARY
    MessageRequester("","A 64-bit Windows-based application.",0)
  Case #SCS_DOS_BINARY
    MessageRequester("","An MS-DOS-based application.",0)
  Case #SCS_OS216_BINARY
    MessageRequester("","A 16-bit OS/2-based application.",0)
  Case #SCS_PIF_BINARY
    MessageRequester("","A PIF file that executes an MS-DOS-based application.",0)
  Case #SCS_POSIX_BINARY
    MessageRequester("","A POSIX-based application.",0)
  Case #SCS_WOW_BINARY
    MessageRequester("","A 16-bit Windows-based application.",0)
EndSelect


Your code is better because it is platform-independent.

_________________
ImageImageImageImage Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Determine Processor Arch. of Win PE (Portable Executable
PostPosted: Sun Aug 26, 2018 4:02 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4545
Location: Lyon - France
Thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye