指定したドライブまたは、パーティションのドライブのモデル名、接続I/F情報をIOCTL_STORAGE_QUERY_PROPERTYを使って取得します。
#include <stdio.h> #include <tchar.h> #include <iostream> #include <fstream> #include <windows.h> /* ファイルサイズの取得 */ int _tmain ( int argc , _TCHAR* argv[] ) { // 標準出力にユニコードを表示できるようにする setlocale( LC_ALL, "Japanese" ); // "i:"はドライブレターです。パーティションを指定しても取得できます。 HANDLE hDevice = CreateFile(L"¥¥¥¥.¥¥i:", 0, // HANDLE hDevice = CreateFile(L"¥¥¥¥?¥¥GLOBALROOT¥¥Device¥¥Harddisk9¥¥Partition1", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if ( hDevice != INVALID_HANDLE_VALUE ) { DWORD dwResult = 0; STORAGE_PROPERTY_QUERY tStragePropertyQuery; tStragePropertyQuery.PropertyId = StorageDeviceProperty; tStragePropertyQuery.QueryType = PropertyStandardQuery; // メモリの確保 DWORD dwInfoSize = 4096; STORAGE_DEVICE_DESCRIPTOR* tpStorageDeviceDescripter = (STORAGE_DEVICE_DESCRIPTOR*)malloc( dwInfoSize ); if ( NULL == tpStorageDeviceDescripter ) { goto err; } /* IOCTL_STORAGE_QUERY_PROPERTY */ BOOL bResult = ::DeviceIoControl( (HANDLE) hDevice // handle to device , IOCTL_STORAGE_QUERY_PROPERTY // dwIoControlCode , &tStragePropertyQuery // lpInBuffer , (DWORD)sizeof( tStragePropertyQuery ) // nInBufferSize , (LPVOID)tpStorageDeviceDescripter // 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 ); goto err; } std::wcout << L"Version = " << tpStorageDeviceDescripter->Version << std::endl; std::wcout << L"Size = " << tpStorageDeviceDescripter->Size << std::endl; std::wcout << L"DeviceType = " << tpStorageDeviceDescripter->DeviceType << std::endl; std::wcout << L"DeviceTypeModifier = " << tpStorageDeviceDescripter->DeviceTypeModifier << std::endl; std::wcout << L"RemovableMedia = " << tpStorageDeviceDescripter->RemovableMedia << std::endl; std::wcout << L"CommandQueueing = " << tpStorageDeviceDescripter->CommandQueueing << std::endl; std::wcout << L"VendorId = " << (CHAR*)tpStorageDeviceDescripter + tpStorageDeviceDescripter->VendorIdOffset << std::endl; std::wcout << L"ProductId = " << (CHAR*)tpStorageDeviceDescripter + tpStorageDeviceDescripter->ProductIdOffset << std::endl; std::wcout << L"ProductRevision = " << (CHAR*)tpStorageDeviceDescripter + tpStorageDeviceDescripter->ProductRevisionOffset << std::endl; if ( ( 0 < tpStorageDeviceDescripter->SerialNumberOffset ) && ( -1 != tpStorageDeviceDescripter->SerialNumberOffset ) ) { std::wcout << L"SerialNumber = " << (CHAR*)tpStorageDeviceDescripter + tpStorageDeviceDescripter->SerialNumberOffset << std::endl; } else { std::wcout << L"SerialNumber = " << std::endl; } std::wcout << L"BusType = " << tpStorageDeviceDescripter->BusType; switch( tpStorageDeviceDescripter->BusType ) { case BusTypeUnknown: std::wcout << L"(Unknown)" << std::endl; break; case BusTypeScsi: std::wcout << L"(Scsi)" << std::endl; break; case BusTypeAtapi: std::wcout << L"(Atapi)" << std::endl; break; case BusTypeAta: std::wcout << L"(Ata)" << std::endl; break; case BusType1394: std::wcout << L"(1394)" << std::endl; break; case BusTypeSsa: std::wcout << L"(Ssa)" << std::endl; break; case BusTypeFibre: std::wcout << L"(Fibre)" << std::endl; break; case BusTypeUsb: std::wcout << L"(Usb)" << std::endl; break; case BusTypeRAID: std::wcout << L"(RAID)" << std::endl; break; case BusTypeiScsi: std::wcout << L"(iScsi)" << std::endl; break; case BusTypeSas: std::wcout << L"(Sas)" << std::endl; break; case BusTypeSata: std::wcout << L"(Sata)" << std::endl; break; case BusTypeSd: std::wcout << L"(Sd)" << std::endl; break; case BusTypeMmc: std::wcout << L"(Mmc)" << std::endl; break; case BusTypeVirtual: std::wcout << L"(Virtual)" << std::endl; break; case BusTypeFileBackedVirtual: std::wcout << L"(FileBackedVirtual)" << std::endl; break; case BusTypeMax: std::wcout << L"(Max)" << std::endl; break; case BusTypeMaxReserved: std::wcout << L"(MaxReserved)" << std::endl; break; } std::wcout << L"RawPropertiesLength = " << tpStorageDeviceDescripter->RawPropertiesLength << std::endl; // std::wcout << L"RawDeviceProperties = " << tpStorageDeviceDescripter->RawDeviceProperties << std::endl; err: // メモリ解放 if ( NULL != tpStorageDeviceDescripter ) { free( tpStorageDeviceDescripter ); } // ハンドルのクローズ ::CloseHandle( hDevice ); } // 正常終了 return( 0 ); }
Version = 40 Size = 120 DeviceType = 0 DeviceTypeModifier = 0 RemovableMedia = 1 CommandQueueing = 0 VendorId = BUFFALO ProductId = ClipDrive ProductRevision = 1.11 SerialNumber = 230760A43EB9F10E BusType = 7(Usb) RawPropertiesLength = 36