-2

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?

c00000fd
  • 20,994
  • 29
  • 177
  • 400

1 Answers1

1

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");
}
Woof.S
  • 21
  • 2
  • If you don't mind me asking, why would I want to use it then if `it is not guaranteed`? – c00000fd Nov 24 '14 at 04:33
  • @c00000fd: This answers your question, i.e., it shows whether the partition is active or not. Whether or not the instance of Windows that is currently running booted via this partition is a different question. – Harry Johnston Nov 24 '14 at 04:44
  • @HarryJohnston: OK. I see. It's probably the way he phrased it. – c00000fd Nov 24 '14 at 05:00
  • I just spent some time trying to understand how to use that PARTITION_INFORMATION structure and I can't seem to find a way how to make it work. The best example I was able to find is this question: http://stackoverflow.com/questions/13608934/getting-info-about-partitions So assuming that I use `CreateFile` on `L"\\\\?\\GLOBALROOT\\Device\\Harddisk4\\Partition4"` then `DeviceIoControl` returns error code 1, or ERROR_INVALID_FUNCTION. Can you provide a code sample if you know how to use it? – c00000fd Nov 24 '14 at 08:02
  • I'm not sure if this is your problem, but since you're accessing a device rather than a file it should be a dot rather than a question mark after the two backslashes. (Sometimes this distinction doesn't matter and sometimes it does, and I'm not sure of the rules.) It might also be worth trying \\.\C: – Harry Johnston Nov 24 '14 at 20:23
  • `IOCTL_DISK_GET_DRIVE_LAYOUT` is another alternative. – Harry Johnston Nov 24 '14 at 20:23
  • @HarryJohnston: I'm curious if it is necessary to start your sentence from `"I'm not sure if this is your problem..."`? – c00000fd Nov 25 '14 at 09:33
  • `\\.\C:` refers to a DOS drive that may be comprised of several partitions. – c00000fd Nov 25 '14 at 09:52