Jump to content
meoit

Detect an UEFI Windows and GPT Disk type

Recommended Posts

Hello all !

Sorry for my bad English.

I am trying to create an application and I need get UEFI Windows type and GPT Disk type.

I searched a lot on Google but did not find.

Someone help me.

Share this post


Link to post
Share on other sites
ConsoleWrite(_GetDrivePartitionStyle() & @CRLF)
ConsoleWrite(_WinAPI_GetFirmwareEnvironmentVariable() & @CRLF)

Func _GetDrivePartitionStyle($sDrive = "C")
    Local $tDriveLayout = DllStructCreate('dword PartitionStyle;' & _
            'dword PartitionCount;' & _
            'byte union[40];' & _
            'byte PartitionEntry[8192]')
    Local $hDrive = DllCall("kernel32.dll", "handle", "CreateFileW", _
            "wstr", "\\.\" & $sDrive & ":", _
            "dword", 0, _
            "dword", 0, _
            "ptr", 0, _
            "dword", 3, _ ; OPEN_EXISTING
            "dword", 0, _
            "ptr", 0)
    If @error Or $hDrive[0] = Ptr(-1) Then Return SetError(@error, @extended, 0) ; INVALID_HANDLE_VALUE
    DllCall("kernel32.dll", "int", "DeviceIoControl", _
            "hwnd", $hDrive[0], _
            "dword", 0x00070050, _
            "ptr", 0, _
            "dword", 0, _
            "ptr", DllStructGetPtr($tDriveLayout), _
            "dword", DllStructGetSize($tDriveLayout), _
            "dword*", 0, _
            "ptr", 0)
    DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hDrive[0])
    Switch DllStructGetData($tDriveLayout, "PartitionStyle")
        Case 0
            Return "MBR"
        Case 1
            Return "GPT"
        Case 2
            Return "RAW"
        Case Else
            Return "UNKNOWN"
    EndSwitch
EndFunc   ;==>_GetDrivePartitionStyle

Func _WinAPI_GetFirmwareEnvironmentVariable()
    DllCall("kernel32.dll", "dword", _
            "GetFirmwareEnvironmentVariableW", "wstr", "", _
            "wstr", "{00000000-0000-0000-0000-000000000000}", "wstr", "", "dword", 4096)
    Local $iError = DllCall("kernel32.dll", "dword", "GetLastError")
    Switch $iError[0]
        Case 1
            Return "LEGACY"
        Case 998
            Return "UEFI"
        Case Else
            Return "UNKNOWN"
    EndSwitch
EndFunc   ;==>_WinAPI_GetFirmwareEnvironmentVariable

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
On 12/12/2016 at 4:03 AM, Terenz said:
ConsoleWrite(_GetDrivePartitionStyle() & @CRLF)
ConsoleWrite(_WinAPI_GetFirmwareEnvironmentVariable() & @CRLF)

Func _GetDrivePartitionStyle($sDrive = "C")
    Local $tDriveLayout = DllStructCreate('dword PartitionStyle;' & _
            'dword PartitionCount;' & _
            'byte union[40];' & _
            'byte PartitionEntry[8192]')
    Local $hDrive = DllCall("kernel32.dll", "handle", "CreateFileW", _
            "wstr", "\\.\" & $sDrive & ":", _
            "dword", 0, _
            "dword", 0, _
            "ptr", 0, _
            "dword", 3, _ ; OPEN_EXISTING
            "dword", 0, _
            "ptr", 0)
    If @error Or $hDrive[0] = Ptr(-1) Then Return SetError(@error, @extended, 0) ; INVALID_HANDLE_VALUE
    DllCall("kernel32.dll", "int", "DeviceIoControl", _
            "hwnd", $hDrive[0], _
            "dword", 0x00070050, _
            "ptr", 0, _
            "dword", 0, _
            "ptr", DllStructGetPtr($tDriveLayout), _
            "dword", DllStructGetSize($tDriveLayout), _
            "dword*", 0, _
            "ptr", 0)
    DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hDrive[0])
    Switch DllStructGetData($tDriveLayout, "PartitionStyle")
        Case 0
            Return "MBR"
        Case 1
            Return "GPT"
        Case 2
            Return "RAW"
        Case Else
            Return "UNKNOWN"
    EndSwitch
EndFunc   ;==>_GetDrivePartitionStyle

Func _WinAPI_GetFirmwareEnvironmentVariable()
    DllCall("kernel32.dll", "dword", _
            "GetFirmwareEnvironmentVariableW", "wstr", "", _
            "wstr", "{00000000-0000-0000-0000-000000000000}", "wstr", "", "dword", 4096)
    Local $iError = DllCall("kernel32.dll", "dword", "GetLastError")
    Switch $iError[0]
        Case 1
            Return "LEGACY"
        Case 998
            Return "UEFI"
        Case Else
            Return "UNKNOWN"
    EndSwitch
EndFunc   ;==>_WinAPI_GetFirmwareEnvironmentVariable

 

Thank you for this.  It has been difficult finding a reliable way to determine if Windows is running under UEFI or BIOS mode.

Share this post


Link to post
Share on other sites
  DllCall("kernel32.dll", "int", "DeviceIoControl", _
            "hwnd", $hDrive[0], _
            "dword", 0x00070050, _
            "ptr", 0, _
            "dword", 0, _
            "ptr", DllStructGetPtr($tDriveLayout), _
            "dword", DllStructGetSize($tDriveLayout), _
            "dword*", 0, _
            "ptr", 0)

"0x00070050 " How to understand it here? Please explain it for me. Thank you.

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites
On 12/20/2017 at 3:20 PM, mlazovjp said:

Thank you for this.  It has been difficult finding a reliable way to determine if Windows is running under UEFI or BIOS mode.

While I would use the code Terenz wrote because it's technically much better than what I could do, I felt that I had to try a different way than what Terenz gave us, now we have a second way to determine UEFI or legacy.

I had to try because I'm not familiar with creating DLL calls and I had not so much to do at work today. 🙂

Edited by ModemJunki
Add info

Always carry a towel.

Share this post


Link to post
Share on other sites
On 10/18/2019 at 4:56 PM, Danyfirex said:

Hello. @qhdpc It means this

IOCTL_DISK_GET_DRIVE_LAYOUT_EX = 0x00070050

 

Saludos

Is 0x00070050 corresponding to  IOCTL_DISK_GET_DRIVE_LAYOUT_EX ?

Edited by qhdpc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...