Translating Delphi code to Winbatch

Started by DirkM, June 05, 2014, 05:39:31 AM

Previous topic - Next topic

DirkM

Hello,

I'm looking for a way to detect if a machine was booted UEFI or legacy BIOS. I found this code that was written in Delphi and was wondering if there is a way to translate this into native Winbatch code: http://theroadtodelphi.wordpress.com/2013/02/19/how-distinguish-when-windows-was-installed-in-legacy-bios-or-uefi-mode-using-delphi/

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;

function GetFirmwareEnvironmentVariableA(lpName, lpGuid: LPCSTR; pBuffer: Pointer;
  nSize: DWORD): DWORD; stdcall; external kernel32 name 'GetFirmwareEnvironmentVariableA';

begin
  try
    GetFirmwareEnvironmentVariableA('','{00000000-0000-0000-0000-000000000000}', nil,0);
    if (GetLastError = ERROR_INVALID_FUNCTION) then
      Writeln('Legacy BIOS')
    else
      Writeln('UEFI Boot Mode');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.


Any help would be appreciated.

Thanks,
Dirk

td

Code (winbatch) Select
#DefineFunction GetFirmwareType()
   ERROR_INVALID_FUNCTION = 1
   DllCall("kernel32.dll",long:"GetFirmwareEnvironmentVariableA",lpstr:'',lpstr:'{00000000-0000-0000-0000-000000000000}', lpnull, long:0)
   if DllLastError() == ERROR_INVALID_FUNCTION  then return "Legacy BIOS"
   else return "UEFI Boot Mode"
#EndFunction

; Test
Message("Firmware", "System firmware: ": GetFirmwareType())
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

DirkM

Perfect, works like a charm, thanks a lot!

This might be something for the knowledge base since there is not much out there on the Internet on how to query the boot mode with WMI or vbscript, nor something that works on 32bit machines (since Windows 8.x now supports UEFI on 32bit hardware).

Thanks,
Dirk

Deana

Deana F.
Technical Support
Wilson WindowWare Inc.

snowsnowsnow

Just out of curiosity, why does it matter which boot BIOS was used?

What's the underlying issue here?

DirkM

In my automated image deployment I need to know if I need to deploy an efi partition (for UEFI) or not (for BIOS). There are also some software tools that need different versions/settings for UEFI based machines.

snowsnowsnow

Quote from: DirkM on June 05, 2014, 12:05:45 PM
In my automated image deployment I need to know if I need to deploy an efi partition (for UEFI) or not (for BIOS). There are also some software tools that need different versions/settings for UEFI based machines.

Interesting.  I didn't know that.