I want to know if the computer I'm using has UEFI or Legacy BIOS.
Is it somehow possible to detect this from Delphi code?
I found ExGetFirmwareEnvironmentVariable which seem interesting, but I'm not sure if it'll work or how to use it.
It would be nice to have something like this:
function IsUEFI: Boolean;
begin
// Code to detect if it's UEFI or not.
end;
2 Answers 2
From what I understand in the comment from @dumbass is that GetFirmwareType is the correct way to detect UEFI.
Therefore, I've gone ahead and wrote a standalone wrapper function for it like this:
function IsUEFI: Boolean;
type
TFirmwareType = (
FirmwareTypeUnknown,
FirmwareTypeBios,
FirmwareTypeUefi,
FirmwareTypeMax
);
TGetFirmwareType = function(out FirmwareType: TFirmwareType): BOOL; stdcall;
begin
Result := False;
var Kernel: HMODULE := GetModuleHandle('kernel32.dll');
if Kernel = 0 then
Exit;
var GetFirmwareType: TGetFirmwareType;
@GetFirmwareType := GetProcAddress(Kernel, 'GetFirmwareType');
if not Assigned(GetFirmwareType) then
Exit;
var FirmwareType: TFirmwareType;
Result := GetFirmwareType(FirmwareType) and (FirmwareType = FirmwareTypeUefi);
end;
When I run:
if IsUEFI then
Memo1.Lines.Add('Yes')
else
Memo1.Lines.Add('No');
it does return "Yes" which is true for my computer. So I can only assume that it's working. I don't have a Legacy BIOS computer right now to test it on.
Comments
I do also like what @Dave Nottage suggested.
Here's a standalone wrapper function for his method:
function IsUEFI: Boolean;
type
TGetFirmwareEnvironmentVariableA = function(lpName: PAnsiChar; lpGuid: PAnsiChar; pBuffer: Pointer; nSize: DWORD): DWORD; stdcall;
begin
Result := False;
var Kernel: HMODULE := GetModuleHandle('kernel32.dll');
if Kernel = 0 then
Exit;
var GetFirmwareEnvVar: TGetFirmwareEnvironmentVariableA;
@GetFirmwareEnvVar := GetProcAddress(Kernel, 'GetFirmwareEnvironmentVariableA');
if not Assigned(GetFirmwareEnvVar) then
Exit;
SetLastError(0);
GetFirmwareEnvVar('', '{00000000-0000-0000-0000-000000000000}', nil, 0);
Result := GetLastError <> ERROR_INVALID_FUNCTION;
end;
Again, when I run it like this:
if IsUEFI then
Memo1.Lines.Add('Yes')
else
Memo1.Lines.Add('No');
it does return "Yes" which is true for my computer. So I can only assume that it's working. I don't have a Legacy BIOS computer right now to test it on.
1 Comment
Explore related questions
See similar questions with these tags.
Result := GetFirmwareType(FirmwareType) and (FirmwareType = FirmwareTypeUefi)to know whether it's UEFI or not?