Hi Joey,
I've done more researching and consulting recently. If you're interested,
we can use DeviceIoControl with IOCTL_DISK_GET_DRIVE_LAYOUT_EX to get the
partition types on the drive. As long as we can find one partition with LDM
related information, we know the disk is a Dynamic disk.
Here's the sample code of a C++ Win32 Console application which prints out
is the first disk Dynamic/Basic disk:
#include "stdafx.h"
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <winioctl.h>
// #include <diskguid.h>
const GUID PARTITION_LDM_DATA_GUID = {0xAF9B60A0L, 0x1431, 0x4F62, 0xBC,
0x68, 0x33, 0x11, 0x71, 0x4A, 0x69, 0xAD}; // Logical Disk Manager data
partition
#define PHYSICALDRIVE TEXT("PhysicalDrive")
void ErrorExit(LPTSTR lpszFunction)
{
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
wprintf(TEXT("%s failed with error %d: %s\n"),
lpszFunction, dw, lpMsgBuf);
LocalFree(lpMsgBuf);
ExitProcess(dw);
}
bool IsPartitionDynamic(PARTITION_INFORMATION_EX *pPie)
{
bool ret = false;
if (pPie->PartitionStyle == PARTITION_STYLE_MBR)
{
ret = pPie->Mbr.PartitionType == PARTITION_LDM;
} else if (pPie->PartitionStyle == PARTITION_STYLE_GPT)
{
ret = IsEqualGUID(pPie->Gpt.PartitionType, PARTITION_LDM_DATA_GUID);
}
return ret;
}
bool IsDiskDynamic(int nDiskNo)
{
bool ret = false;
TCHAR szDiskPath[MAX_PATH];
wsprintf(szDiskPath, TEXT("\\\\.\\%s%u"), PHYSICALDRIVE, nDiskNo);
HANDLE hDisk = CreateFile(szDiskPath, GENERIC_READ, FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDisk != INVALID_HANDLE_VALUE) {
DRIVE_LAYOUT_INFORMATION_EX *pInfo;
DWORD dwBytesReturn = 0;
int estimatedPartitionCount = 4;
loop:
DWORD dwSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) +
estimatedPartitionCount * sizeof(PARTITION_INFORMATION_EX);
pInfo = (DRIVE_LAYOUT_INFORMATION_EX *) new BYTE[dwSize];
if (DeviceIoControl(hDisk, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0,
(LPVOID) pInfo,
dwSize,
&dwBytesReturn,
NULL))
{
for (DWORD i = 0; i < pInfo->PartitionCount; i++)
{
if (IsPartitionDynamic(pInfo->PartitionEntry + i))
{
ret = true;
break;
}
}
} else {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
estimatedPartitionCount *= 2;
delete pInfo;
goto loop;
} else {
ErrorExit(TEXT("DeviceIoControl"));
}
}
CloseHandle(hDisk);
delete pInfo;
} else {
ErrorExit(TEXT("CreateFile"));
}
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
wprintf(TEXT("Disk 0 is %s disk.\n"), IsDiskDynamic(0) ? TEXT("Dynamic") :
TEXT("Basic"));
return 0;
}
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.