Detect 32 vs 64 bit dll

Does anyone have a nice function that I can call to determine if a specified dll file is 32 or 64 bit?

My app is 32 bit and I only want to load 32 bit dlls that users will put in a folder.

This should do the trick for you:

function Is32BitDLL(const ADLLPath: string): Boolean;
var
  fileHandle: THandle;
  mappingHandle: THandle;
  baseAddress: Pointer;
  dosHeader: PImageDosHeader;
  ntHeaders: PImageNtHeaders;
begin
  Result := False;
  fileHandle := CreateFile(PChar(ADLLPath), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if fileHandle = INVALID_HANDLE_VALUE then
    RaiseLastOSError;

  try
    mappingHandle := CreateFileMapping(fileHandle, nil, PAGE_READONLY, 0, 0, nil);
    if mappingHandle = 0 then
      RaiseLastOSError;

    try
      baseAddress := MapViewOfFile(mappingHandle, FILE_MAP_READ, 0, 0, 0);
      if not Assigned(baseAddress) then
        RaiseLastOSError;

      try
        dosHeader := PImageDosHeader(baseAddress);
        ntHeaders := PImageNtHeaders(PByte(baseAddress) + dosHeader._lfanew);

        Result := ntHeaders.FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
      finally
        UnmapViewOfFile(baseAddress);
      end;
    finally
      CloseHandle(mappingHandle);
    end;
  finally
    CloseHandle(fileHandle);
  end;
end;
2 Likes

Thank you. I’ll try it tomorrow.

1 Like

Going from XP 32 bit address to windows 10 64 bit address for example
the *.DLL’s for all windows graphics are still 23 bit
handles are still 32 bit
What has changed is addressing is gone up to 64 bit
A 64 bit address value in 32 bit addressing environment will only contain a 32 bit address but a 32 bit address value cannot contain a 64 bit address.
Is that what you need to know
So pointers are 64 bit in Windows 10

Thanks Thomas. That works well. I can now show a more precise reason in the log file for the possible failure to load the user’s dll.

1 Like