Advertisement
hooge

Get App Version 32/64 bit?

Jun 23rd, 2014
5,924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. public enum MachineType {Unknown = 0,  
  5.                           x86 = 0x014c,   // IMAGE_FILE_MACHINE_I386   Intel 386
  6.                           i64 = 0x0200,   // IMAGE_FILE_MACHINE_IA64   Intel Itanium processor family (64-bit)
  7.                           x64 = 0x8664 }  // IMAGE_FILE_MACHINE_AMD64  NOT! AMD 64-bit processor ???
  8.  
  9. //UPDATE : Seems IMAGE_FILE_MACHINE_EM64T = 0x8664, as well
  10. //http://msdn.microsoft.com/en-us/library/windows/desktop/ms680309(v=vs.85).aspx
  11. //Intel, reacting to the market success of AMD, admits it has been developing a clone of the AMD64 extensions named IA-32e (later renamed EM64T, then yet again renamed to Intel 64). Intel ships updated versions of its Xeon and Pentium 4 processor families supporting the new 64-bit instruction set.
  12.  
  13. public string GetAppCompiledMachineType(string fileName)
  14. {
  15.             const int PE_POINTER_OFFSET = 60;            
  16.             const int MACHINE_OFFSET = 4;
  17.             byte[] data = new byte[4096];
  18.  
  19.             using (Stream s = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
  20.                 s.Read(data, 0, 4096);
  21.             }
  22.  
  23.             // dos header is 64 bytes, last element, long (4 bytes) is the address of the PE header
  24.             int PE_HEADER_ADDR = BitConverter.ToInt32(data, PE_POINTER_OFFSET);
  25.             int machineUint = BitConverter.ToUInt16(data, PE_HEADER_ADDR + MACHINE_OFFSET);
  26.             return ((MachineType)machineUint).ToString();
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement