Turns out that LoadLibrary()
fails in the same way that LoadPackage()
does, which was hiding the error from me in my search program. The solution to that problem is to read the imports list directly from the file rather than using LoadLibrary()
and GetPackageInfo()
. Doing this required me to get a lot more familiar with the PE file structure than I was before. Here's code to get the imports list from a BPL directly rather than through LoadLibrary()
:
uses
WinApi.Windows,
System.Classes,
System.SysUtils;
const
MinModuleSize = SizeOf(TImageDOSHeader) +
SizeOf(UInt32) +
SizeOf(TImageFileHeader);
function GetPEImportsList(const FN: String; SL: TStringList): Boolean;
var
F: TFileStream;
I: UInt32;
W: Word;
Is32: Boolean;
RVAOfs: Cardinal;
S: AnsiString;
CA: Array[0..7] of AnsiChar; //used to get the section name
NA: Array[0..127] of AnsiChar; //arbitrarily long BPL name buffer
Buff: TBytes;
//all of these structure types are declared in WinApi.Windows
DHdr: TImageDOSHeader;
COFF: TImageFileHeader;
Op32: TImageOptionalHeader32;
Op64: TImageOptionalHeader64;
ISec: TImageSectionHeader;
IDes: TImageImportDescriptor;
begin
//assume failure
Result := False;
Assert(Assigned(SL), 'StringList not assigned');
SL.Clear;
//if the file doesn't even exist, bail
if not FileExists(FN) then
Exit;
//open the file
F := TFileStream.Create(FN, fmOpenRead or fmShareCompat);
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;
//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;
//read the COFF image file header and get bitness
F.Read(COFF, SizeOf(COFF));
Is32 := COFF.Machine = IMAGE_FILE_MACHINE_I386;
//does an optional header exist? If not, it's not a valid file
if COFF.SizeOfOptionalHeader = 0 then
Exit;
//read the "optional" (it's not) header as appropriate for
//32- or 64-bit modules, and verify the magic number. We
//don't do anything with this header here beyond the verify,
//we're mostly just reading it to get past it.
if Is32 then begin
F.Read(Op32, SizeOf(Op32));
if Op32.Magic <> $10B then //whoops, not really a 32-bit image
Exit;
end
else begin
F.Read(Op64, SizeOf(Op64));
if Op64.Magic <> $20B then //whoops, not really a 64-bit image
Exit;
end;
//Following the optional header is a set of section headers. The
//names of the required BPLs are linked via records in the Imports
//(.idata) section. So we start by finding the imports section by
//walking the section headers. On most BPLs this will be in header
//#5, but we do the walk for safety in case it changes in the future.
W := 0;
repeat
Inc(W);
F.Read(ISec, SizeOf(ISec));
Move(ISec.Name[0], CA[0], 8); //done for type safety
until (StrPas(CA) = '.idata') or (W >= 8);
if StrPas(CA) <> '.idata' then
Exit;
//Many addresses in PE structs are RVAs (Relative Virtual Addresses,
//the address once loaded into memory) rather than Raw addresses
//(physical offset in the file.) The RVA will always be greater than
//the Raw address, so to get the Raw address for an RVA address, we
//must subtract the difference between the ISec.VirtualAddress and
//ISec.PointerToRawData values from any RVA address in the structs.
RVAOfs := ISec.VirtualAddress - ISec.PointerToRawData;
//Find and read all the data in the imports table into a buffer. We
//do this because we must seek around in the file to get the actual
//text-string name of each import.
SetLength(Buff, ISec.SizeOfRawData);
FillChar(Buff[0], ISec.SizeOfRawData, 0);
F.Seek(ISec.PointerToRawData, 0);
F.Read(Buff, ISec.SizeOfRawData);
//now walk thru the buffer, getting each import descriptor and
//using its Name pointer to find the text name of the import in
//the file
W := 0;
while W < ISec.SizeOfRawData do begin
//load the Image Import Descriptor struct from the buffer
Move(Buff[w], IDes, SizeOf(IDes));
//IIDes.Name is an RVA. If it's zero, we're done
if IDes.Name = 0 then
Break;
//seek in the file to the correct spot and read the name
F.Seek(IDes.Name - RVAOfs, 0);
FillChar(NA[0], SizeOf(NA), 0);
F.Read(NA[0], SizeOf(NA));
//if it's the name of a BPL, add it to our stringlist
S := StrPas(NA);
if (S <> '') and (Lowercase(ExtractFileExt(S)) = '.bpl') then
SL.Add(S);
//move to the next record
Inc(W, SizeOf(IDes));
end;
//we're done, return success
Result := True;
finally
F.Free;
end;
end;