ディスクのサイズやセクタサイズなどのジオメトリ情報を取得します。
#include <stdio.h> #include <tchar.h> #include <iostream> #include <windows.h> 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 ) { /* IOCTL_DISK_GET_DRIVE_GEOMETRY_EX */ { DWORD dwInfoSize = sizeof( DISK_GEOMETRY_EX ) + sizeof( DISK_PARTITION_INFO ) + sizeof( DISK_DETECTION_INFO ); DISK_GEOMETRY_EX* tpDiskGeometryEx = (DISK_GEOMETRY_EX*)malloc( dwInfoSize ); if ( NULL == tpDiskGeometryEx ) { // メモリ不足 ::CloseHandle( hDevice ); return( -1 ); } DWORD dwResult = 0; /* IOCTL_DISK_GET_DRIVE_GEOMETRY_EX */ BOOL bResult = ::DeviceIoControl( (HANDLE) hDevice // handle to device , IOCTL_DISK_GET_DRIVE_GEOMETRY_EX // dwIoControlCode , NULL // lpInBuffer , 0 // nInBufferSize , (LPVOID)tpDiskGeometryEx // 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_GEOMETRY_EX失敗(" << ::GetLastError() << L")" << std::endl; ::CloseHandle( hDevice ); return( -1 ); } std::wcout << L"DISK_GEOMETRY_EX" << std::endl; std::wcout << L"¥t" << L"DiskSize : " << tpDiskGeometryEx->DiskSize.QuadPart << std::endl; std::wcout << L"¥t" << L"Geometry.MediaType : " << tpDiskGeometryEx->Geometry.MediaType << std::endl; std::wcout << L"¥t" << L"Geometry.BytesPerSector : " << tpDiskGeometryEx->Geometry.BytesPerSector << std::endl; std::wcout << L"¥t" << L"Geometry.SectorsPerTrack : " << tpDiskGeometryEx->Geometry.SectorsPerTrack << std::endl; std::wcout << L"¥t" << L"Geometry.TracksPerCylinder : " << tpDiskGeometryEx->Geometry.TracksPerCylinder << std::endl; std::wcout << L"¥t" << L"Geometry.Cylinders : " << tpDiskGeometryEx->Geometry.Cylinders.QuadPart << std::endl; } ::CloseHandle( hDevice ); } // 正常終了 return 0; }
DISK_GEOMETRY_EX DiskSize : 1500301910016 Geometry.MediaType : 12 Geometry.BytesPerSector : 512 Geometry.SectorsPerTrack : 63 Geometry.TracksPerCylinder : 255 Geometry.Cylinders : 182401