-2

我的目标是了解 Windows 是否安装在活动磁盘分区上。我可以获得 Windows 的路径:

C:\WINDOWS

然后是分区:

\Device\Harddisk4\Partition4

但问题是如何知道这个分区是否处于活动状态?

4

1 回答 1

1

检查此链接(http://msdn.microsoft.com/en-us/library/windows/desktop/aa365451(v=vs.85).aspx

PARTITION_INFORMATION 具有 BootIndicator。但不能保证正在运行的窗口是由该分区启动的。

已编辑这是在 Windows7 上测试的示例功能。我认为“激活”分区不是你的目标。“激活”具有可启动 USB 设备等含义。我不喜欢 WMI,但它可以帮助您实现目标(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;
}

打电话喜欢

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");
}
于 2014-11-24T04:11:14.873 回答