使用 Delphi 2007+ 或 Lazarus(Win64) 我正在寻找一种方法来确定 dll 是编译为 x64 还是 x86?
问问题
3047 次
2 回答
17
您应该阅读并解析 PE 标头。
像这样:
function Isx64(const Strm: TStream): Boolean;
const
IMAGE_FILE_MACHINE_I386 = $014c; // Intel x86
IMAGE_FILE_MACHINE_IA64 = $0200; // Intel Itanium Processor Family (IPF)
IMAGE_FILE_MACHINE_AMD64 = $8664; // x64 (AMD64 or EM64T)
// You'll unlikely encounter the things below:
IMAGE_FILE_MACHINE_R3000_BE = $160; // MIPS big-endian
IMAGE_FILE_MACHINE_R3000 = $162; // MIPS little-endian, 0x160 big-endian
IMAGE_FILE_MACHINE_R4000 = $166; // MIPS little-endian
IMAGE_FILE_MACHINE_R10000 = $168; // MIPS little-endian
IMAGE_FILE_MACHINE_ALPHA = $184; // Alpha_AXP }
IMAGE_FILE_MACHINE_POWERPC = $1F0; // IBM PowerPC Little-Endian
var
Header: TImageDosHeader;
ImageNtHeaders: TImageNtHeaders;
begin
Strm.ReadBuffer(Header, SizeOf(Header));
if (Header.e_magic <> IMAGE_DOS_SIGNATURE) or
(Header._lfanew = 0) then
raise Exception.Create('Invalid executable');
Strm.Position := Header._lfanew;
Strm.ReadBuffer(ImageNtHeaders, SizeOf(ImageNtHeaders));
if ImageNtHeaders.Signature <> IMAGE_NT_SIGNATURE then
raise Exception.Create('Invalid executable');
Result := ImageNtHeaders.FileHeader.Machine <> IMAGE_FILE_MACHINE_I386;
end;
于 2010-05-01T07:24:25.267 回答
4
您可以使用 JCL 中的 JclPeImage。以下应用程序显示了如何做到这一点。
program Isx64ImageTest;
{$APPTYPE CONSOLE}
uses
SysUtils, JclWin32, JclPEImage;
var
PEImage: TJclPeImage;
begin
PEImage := TJclPeImage.Create;
try
//usage is "Isx64ImageTest filename"
PEImage.FileName := ParamStr(1);
//print the machine value as string
WriteLn(Format('Machine value of image %s is %s',
[PEImage.FileName, PEImage.HeaderValues[JclPeHeader_Machine]]));
//check for a special machine value
case PEImage.LoadedImage.FileHeader^.FileHeader.Machine of
IMAGE_FILE_MACHINE_I386: begin end;
IMAGE_FILE_MACHINE_AMD64: begin end;
else
begin
end;
end;
finally
PEImage.Free;
end;
end.
于 2010-05-01T15:37:32.870 回答