How to tell if Windows partition is active by a path on it?

430 views Asked by At

My goal is to know if Windows is installed on an active disk partition. I can obtain the path for Windows:

C:\WINDOWS

and then it's partition:

\Device\Harddisk4\Partition4

But the question is how to know if this partition is active?

1

There are 1 answers

8
Woof.S On

Check this Link (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365451(v=vs.85).aspx)

PARTITION_INFORMATION has BootIndicator. but it is not guarantee about the running windows was booted by that partition.

Edited It is a example function tested on Windows7. I think 'activate' partition is not your goal. The 'activate' has meaning such as bootable USB device. I don't like WMI but it could be help your goal (http://msdn.microsoft.com/en-us/library/windows/desktop/bb986746(v=vs.85).aspx)

BOOL
__stdcall
TP_IsPartitionActivated(
__in    LPCWSTR pPartition,
__out   PBOOL   pbIsActivated
)
{
    HANDLE  hDevice = INVALID_HANDLE_VALUE;
    PARTITION_INFORMATION_EX   szPartitionInformation;
    DWORD cbReturned = 0x00;

    if (pPartition == NULL || pbIsActivated == NULL) { return FALSE; }

    __try
    {
        hDevice = CreateFileW(pPartition, 0x00, 0x00, NULL, OPEN_EXISTING, 0x00, NULL);
        if (hDevice == INVALID_HANDLE_VALUE) { return FALSE; }

        RtlZeroMemory(&szPartitionInformation, sizeof(szPartitionInformation));
        if (FALSE != DeviceIoControl(hDevice, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0x00, (LPVOID)&szPartitionInformation, sizeof(PARTITION_INFORMATION_EX), &cbReturned, NULL))
        {
            if (PARTITION_STYLE_MBR == szPartitionInformation.PartitionStyle)
            {
                *pbIsActivated = szPartitionInformation.Mbr.BootIndicator;
            }
            else
            {
            }

            return TRUE;
        }
        else
        {
            cbReturned = GetLastError();
            wprintf(L"%08X(%d)\n", cbReturned, cbReturned);
        }
    }
    __finally
    {
        if (hDevice != INVALID_HANDLE_VALUE) { CloseHandle(hDevice); }
    }

    return FALSE;
}

Call like

WCHAR   szPartition[] = L"\\\\.\\C:";
BOOL    bIsActivated = FALSE;

if (FALSE != TP_IsPartitionActivated(szPartition, &bIsActivated))
{
    wprintf(L"%s \n", bIsActivated == FALSE ? L"not activated" : L"activated");
}
else
{
    wprintf(L"function fail\n");
}