this slowpoke moves

Check EXE is 64 bit or 32 bit

const
  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;

  KERNEL32_DLL = 'kernel32.dll';

type
  TGetBinaryType = function (lpApplicationName: PWideChar;
  out lpBinaryType: DWORD): Boolean; stdcall;

//

function GetBinaryType(lpApplicationName: PWideChar; out lpBinaryType: DWORD): Boolean;
var
  DLL_Handle : THandle; // für dynamischen Funktionsimport!
  DLL_GetBinaryType : TGetBinaryType; // für dynamischen Funktionsimport!
begin
  // Handle für die KERNEL32.DLL erhalten
  DLL_Handle := LoadLibrary(PChar(KERNEL32_DLL));
  // Wenn Handle vorhanden, Adressen der Funktionen ermitteln
        if @DLL_GetBinaryType <> nil then
  if DLL_Handle  0 then
    begin
      try
        @DLL_GetBinaryType := GetProcAddress(DLL_Handle, 'GetBinaryTypeW');
        // Wurde GetBinaryTypeW in der DLL gefunden?
        if @DLL_GetBinaryType <> nil then
          begin
            Result := DLL_GetBinaryType(lpApplicationName, lpBinaryType);
          end
        else
          begin
            RaiseLastOSError;
            Result := False;
          end;
      finally
        FreeLibrary(DLL_Handle);
      end;
    end
  else
    begin
      RaiseLastOSError;
      Result := False;
    end;
end;

function IsExecutable32Bit(const lpExeFilename: String): Boolean;
const
  kb32 = 1024 * 32;
var
  Buffer : Array[0..kb32-1] of Byte; // warning: assuming both headers are in there!
  hFile : DWord;
  bRead : DWord;
  bToRead : DWord;
  pDos : PImageDosHeader;
  pNt : PImageNtHeaders;
begin
  Result := False;
  hFile := CreateFile(pChar(lpExeFilename), GENERIC_READ, FILE_SHARE_READ, NIL,
    OPEN_EXISTING, 0, 0);
  if hFile <> INVALID_HANDLE_VALUE then
    try
      bToRead := GetFileSize(hFile, NIL);
      if bToRead > kb32 then bToRead := kb32;
      if not ReadFile(hFile, Buffer, bToRead, bRead, NIL) then Exit;
      if bRead = bToRead then
      begin
        pDos := @Buffer[0];
        if pDos.e_magic = IMAGE_DOS_SIGNATURE then
        begin
          pNt := PImageNtHeaders(LongInt(pDos) + pDos._lfanew);
          if pNt.Signature = IMAGE_NT_SIGNATURE then
            Result := pNt.FileHeader.Machine and IMAGE_FILE_32BIT_MACHINE > 0;
        end; {
        else
          raise Exception.Create('File is not a valid executable.');
        }
      end; {
        else
          raise Exception.Create('File is not an executable.');
        }
    finally
      CloseHandle(hFile);
    end;
end;

function IsExecutable64Bit(const lpExeFilename: String): Boolean;
// since as of now (march 2012), there only exist 32 and 64 bit executables,
// if its not the one, its assumably the other
begin
  Result := not IsExecutable32Bit(lpExeFilename);
end;
Beispiele :
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then begin
  if IsExecutable64Bit(OpenDialog1.FileName) = true then
  ShowMessage('64 bit')
  else
  ShowMessage('fail');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if OpenDialog1.Execute then begin
  if IsExecutable32Bit(OpenDialog1.FileName) = true then
  ShowMessage('32 bit')
  else
  ShowMessage('fail');
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate