ドライブのパーティションのレイアウト情報を取得します。
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <windows.h>
//#include <Mountmgr.h> // --> WindowsDDKです。
#pragma comment( lib, "Rpcrt4.lib" )
/*
GUIDを文字列に変換する
*/
std::wstring GuidToString
(
GUID* pGuid
)
{
std::wstring oGuidString;
RPC_WSTR waString;
// GUIDを文字列へ変換する
if ( RPC_S_OK == ::UuidToString( pGuid, &waString ) ) {
// GUIDを結果にセット
oGuidString = (WCHAR*)waString;
// GUID文字列の破棄
RpcStringFree( &waString );
}
// GUIDを返す
return( oGuidString );
}
/*
メイン
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
// 標準出力にユニコードを表示できるようにする
setlocale( LC_ALL, "Japanese" );
// "PhysicalDrive"は物理ドライブ番号です。
HANDLE hDevice = CreateFile(L"¥¥¥¥.¥¥PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if ( hDevice == INVALID_HANDLE_VALUE ) {
std::wcout << L"CreateFile失敗(" << ::GetLastError() << L")" << std::endl;
}
else {
/*
IOCTL_DISK_GET_DRIVE_LAYOUT_EX
*/
{
DWORD dwInfoSize = sizeof( DRIVE_LAYOUT_INFORMATION_EX ) + sizeof( PARTITION_INFORMATION_EX ) * 3;
DRIVE_LAYOUT_INFORMATION_EX* tpDriveLayoutInformationEx = (DRIVE_LAYOUT_INFORMATION_EX*)malloc( dwInfoSize );
if ( NULL == tpDriveLayoutInformationEx ) {
// メモリ不足
::CloseHandle( hDevice );
return( -1 );
}
DWORD dwResult = 0;
/*
IOCTL_DISK_GET_DRIVE_LAYOUT_EX
*/
BOOL bResult = ::DeviceIoControl(
(HANDLE) hDevice // handle to device
, IOCTL_DISK_GET_DRIVE_LAYOUT_EX // dwIoControlCode
, NULL // lpInBuffer
, 0 // nInBufferSize
, (LPVOID)tpDriveLayoutInformationEx // output buffer
, (DWORD)dwInfoSize // size of output buffer
, (LPDWORD)&dwResult // number of bytes returned
, (LPOVERLAPPED)NULL // OVERLAPPED structure
);
if ( 0 == bResult ) {
// 取得失敗
std::wcout << L"IOCTL_DISK_GET_DRIVE_LAYOUT_EX失敗(" << ::GetLastError() << L")" << std::endl;
::CloseHandle( hDevice );
return( -1 );
}
// 1MB
int n1MB = 1024 * 1024;
std::wcout << L"PartitionCount : " << tpDriveLayoutInformationEx->PartitionCount << std::endl;
switch( tpDriveLayoutInformationEx->PartitionStyle ) {
case PARTITION_STYLE_MBR:
{
std::wcout << L"PARTITION_STYLE_MBR" << std::endl;
std::wcout << L"¥tMbr.Signature : " << tpDriveLayoutInformationEx->Mbr.Signature << std::endl;
}
break;
case PARTITION_STYLE_GPT:
{
std::wcout << L"PARTITION_STYLE_GPT" << std::endl;
std::wcout << L"¥tGpt.DiskId : " << GuidToString( &tpDriveLayoutInformationEx->Gpt.DiskId ).c_str() << std::endl;
std::wcout << L"¥tGpt.MaxPartitionCount : " << tpDriveLayoutInformationEx->Gpt.MaxPartitionCount << std::endl;
std::wcout << L"¥tGpt.StartingUsableOffset : " << tpDriveLayoutInformationEx->Gpt.StartingUsableOffset.QuadPart << std::endl;
std::wcout << L"¥tGpt.UsableLength : " << tpDriveLayoutInformationEx->Gpt.UsableLength.QuadPart << std::endl;
}
break;
case PARTITION_STYLE_RAW:
{
std::wcout << L"PARTITION_STYLE_RAW" << std::endl;
}
break;
}
for ( int nI = 0; nI < (int)tpDriveLayoutInformationEx->PartitionCount; nI++ ) {
PARTITION_INFORMATION_EX& tPartitionInformationEx = tpDriveLayoutInformationEx->PartitionEntry[ nI ];
std::wcout << L"IOCTL_DISK_GET_PARTITION_INFO_EX[" << nI << L"]" << std::endl;
std::wcout << L"¥t" << L"PartitionNumber : " << tPartitionInformationEx.PartitionNumber << std::endl;
std::wcout << L"¥t" << L"PartitionStyle : " << tPartitionInformationEx.PartitionStyle << std::endl;
std::wcout << L"¥t" << L"StartingOffset : " << tPartitionInformationEx.StartingOffset.QuadPart << L"( " << tPartitionInformationEx.StartingOffset.QuadPart / 512 << L"Sector )"<< std::endl;
std::wcout << L"¥t" << L"PartitionLength : " << tPartitionInformationEx.PartitionLength.QuadPart << L"( " << tPartitionInformationEx.PartitionLength.QuadPart / n1MB << L"MB )" << std::endl;
std::wcout << L"¥t" << L"RewritePartition : " << tPartitionInformationEx.RewritePartition << std::endl;
switch( tPartitionInformationEx.PartitionStyle ) {
case PARTITION_STYLE_MBR:
{
std::wcout << L"¥t" << L"Mbr.BootIndicator : " << tPartitionInformationEx.Mbr.BootIndicator << std::endl;
std::wcout << L"¥t" << L"Mbr.HiddenSectors : " << tPartitionInformationEx.Mbr.HiddenSectors << std::endl;
std::wcout << L"¥t" << L"Mbr.PartitionType : " << tPartitionInformationEx.Mbr.PartitionType << std::endl;
std::wcout << L"¥t" << L"Mbr.RecognizedPartition : " << tPartitionInformationEx.Mbr.RecognizedPartition << std::endl;
}
break;
case PARTITION_STYLE_GPT:
{
std::wcout << L"¥t" << L"Mbr.Attributes : " << tPartitionInformationEx.Gpt.Attributes << std::endl;
std::wcout << L"¥t" << L"Mbr.Name : " << tPartitionInformationEx.Gpt.Name << std::endl;
std::wcout << L"¥t" << L"Mbr.PartitionId : " << GuidToString( &tPartitionInformationEx.Gpt.PartitionId ).c_str() << std::endl;
std::wcout << L"¥t" << L"Mbr.PartitionType : " << GuidToString( &tPartitionInformationEx.Gpt.PartitionType ).c_str() << std::endl;
}
break;
case PARTITION_STYLE_RAW:
{
}
break;
}
}
// メモリの解放
free( tpDriveLayoutInformationEx );
}
::CloseHandle( hDevice );
}
// 正常終了
return 0;
}
PartitionCount : 4
PARTITION_STYLE_MBR
Mbr.Signature : 1784180224
IOCTL_DISK_GET_PARTITION_INFO_EX[0]
PartitionNumber : 1
PartitionStyle : 0
StartingOffset : 32256( 63Sector )
PartitionLength : 1500299265024( 1430796MB )
RewritePartition : 0
Mbr.BootIndicator : 0
Mbr.HiddenSectors : 63
Mbr.PartitionType : 7
Mbr.RecognizedPartition : 1
IOCTL_DISK_GET_PARTITION_INFO_EX[1]
PartitionNumber : 0
PartitionStyle : 0
StartingOffset : 0( 0Sector )
PartitionLength : 0( 0MB )
RewritePartition : 0
Mbr.BootIndicator : 0
Mbr.HiddenSectors : 0
Mbr.PartitionType : 0
Mbr.RecognizedPartition : 0
IOCTL_DISK_GET_PARTITION_INFO_EX[2]
PartitionNumber : 0
PartitionStyle : 0
StartingOffset : 0( 0Sector )
PartitionLength : 0( 0MB )
RewritePartition : 0
Mbr.BootIndicator : 0
Mbr.HiddenSectors : 0
Mbr.PartitionType : 0
Mbr.RecognizedPartition : 0
IOCTL_DISK_GET_PARTITION_INFO_EX[3]
PartitionNumber : 0
PartitionStyle : 0
StartingOffset : 0( 0Sector )
PartitionLength : 0( 0MB )
RewritePartition : 0
Mbr.BootIndicator : 0
Mbr.HiddenSectors : 0
Mbr.PartitionType : 0
Mbr.RecognizedPartition : 0