0

Is there a straightforward way to examine a Delphi BPL file for 32/64 bitness? I want to be able to do this outside of attempting to load the BPL itself - e.g. open the BPL as a binary file and read a specific address in the contents of the file that indicates whether it's compiled for 32-bit or 64-bit.

The referenced answer is for Python, not Delphi, and not everyone is aware that BPLs are actually just an extended for of DLLs.

1
  • 1
    I think this is a valid question as it is not obvious to everybody that BPLs are DLLs. The linked answer does not mention BPLs and it relates to Python only.
    – dummzeuch
    Sep 22 at 7:25

2 Answers 2

2

As we are speaking of BPL files here (which basically are DLLs following a certain convention), one can simply compare some Delphi BPLs coming with every installation and are located in separate subfolders of the redist folder.

At position $50 there is an ASCI string containing This program must be run under Win32 and This program must be run under Win64 respectively.

2
  • 1
    Uwe - That's it. Two bytes at offset $72 will be "33 32" for 32-bit or "36 34" for 64-bit. This applies to Delphi-built BPLs only - the stub text may be different in other PE files. Checking the characteristics field of the PE header (offset $18) is more reliable.
    – SteveS
    Sep 21 at 21:15
  • I was going to add example code but the question was closed... :(
    – SteveS
    Sep 22 at 18:35
1

Here's code to detect this:

uses
  WinApi.Windows,
  System.Classes,
  System.SysUtils;

type
  TModuleBitness = (bnUnknown, bnBadMZSig, bnBadPESig, bn32Bit, bn64Bit);

const
  MinModuleSize = SizeOf(TImageDOSHeader) + 
                  SizeOf(UInt32) + 
                  SizeOf(TImageFileHeader);

function CheckFileBitness(const FN: String): TModuleBitness;
var
  F:    TFileStream;
  I:    UInt32;
  DHdr: TImageDOSHeader;
  COFF: TImageFileHeader;
begin
  //assume unknown type
  Result := bnUnknown;
  //if the file doesn't even exist, bail
  if not FileExists(FN) then
    Exit;
  //open the file
  F := TFileStream.Create(FN, fmOpenRead);
  try
    //make sure we have a big enough file
    if F.Size < MinModuleSize then
      Exit;
    //start at the beginning
    F.Seek(0, 0);
    //read DOS header
    F.Read(DHdr, SizeOf(DHdr));
    //is it valid?
    if DHdr.e_magic <> IMAGE_DOS_SIGNATURE then
      Exit(bnBadMZSig);

    //seek to offset of PE signature
    F.Seek(DHdr._lfanew, 0);
    //read PE signature DWORD
    F.Read(I, SizeOf(I));
    //is it valid?
    if I <> IMAGE_NT_SIGNATURE then
      Exit(bnBadPESig);

    //read the COFF image file header
    F.Read(COFF, SizeOf(COFF));
    //is it x64?
    if COFF.Machine = IMAGE_FILE_MACHINE_AMD64 then
      Result := bn64Bit
      //is it x86?
    else if COFF.Machine = IMAGE_FILE_MACHINE_I386 then
      Result := bn32Bit;
  finally
    F.Free;
  end;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.