日期:2014-05-17 浏览次数:20901 次
场景:
1.有时候要对磁盘进行扫描操作,比如一些系统软件需要知道磁盘的信息。这时候就需要获取磁盘的盘符,类型和容量。
文件test_usb.cpp
#include <stdio.h> #include <stdint.h> #include <windows.h> #include <iostream> using namespace std; void TestDrive() { cout << "TestDrive" << endl; char* lpDrives = (char*)malloc(MAX_PATH*sizeof(char)); memset(lpDrives,0,MAX_PATH*sizeof(char)); DWORD dwLen = ::GetLogicalDriveStringsA(MAX_PATH, lpDrives); cout << "dwLen: " << dwLen << endl; DWORD driver_number = dwLen / 4; cout << "driver_number: " << driver_number << endl; string driver; LPTSTR start; for(DWORD nIndex = 0; nIndex < driver_number; nIndex++) { start = lpDrives + nIndex * 4; cout << "........................................." << endl; driver.append(start); cout << "lpDrives + nIndex * 4: " << lpDrives + nIndex * 4 << endl; cout << "DRIVE_REMOVABLE: " << DRIVE_REMOVABLE << endl; cout << "::GetDriveType(lpDrives + nIndex * 4): " << ::GetDriveType(lpDrives + nIndex * 4) << endl; if(::GetDriveType(start) == DRIVE_REMOVABLE) { driver.append(start); if( GetVolumeInformation(start,0,0,0,0,0,0,0) ) //判断驱动是否准备就绪 { cout << "驱动准备就绪 " << endl; } } uint64_t lpFreeBytesAvailable = 0; uint64_t lpTotalNumberOfBytes = 0; uint64_t lpTotalNumberOfFreeBytes = 0; GetDiskFreeSpaceExA(lpDrives + nIndex * 4,(PULARGE_INTEGER)&lpFreeBytesAvailable, (PULARGE_INTEGER)&lpTotalNumberOfBytes,(PULARGE_INTEGER)&lpTotalNumberOfFreeBytes); cout << "lpFreeBytesAvailable: " << lpFreeBytesAvailable << endl; cout << "lpTotalNumberOfBytes: " << lpTotalNumberOfBytes << endl; cout << "lpTotalNumberOfFreeBytes: " << lpTotalNumberOfFreeBytes << endl; } cout << "driver: " << driver << endl; } int main(int argc, char *argv[]) { //TestWchar(); TestDrive(); return 0; }
输出:
---------- CompileLinkRun ---------- TestDrive dwLen: 24 driver_number: 6 ......................................... lpDrives + nIndex * 4: C:\ DRIVE_REMOVABLE: 2 ::GetDriveType(lpDrives + nIndex * 4): 3 lpFreeBytesAvailable: 12637241344 lpTotalNumberOfBytes: 159210532864 lpTotalNumberOfFreeBytes: 12637241344 ......................................... lpDrives + nIndex * 4: D:\ DRIVE_REMOVABLE: 2 ::GetDriveType(lpDrives + nIndex * 4): 5 lpFreeBytesAvailable: 0 lpTotalNumberOfBytes: 0 lpTotalNumberOfFreeBytes: 0 ......................................... lpDrives + nIndex * 4: E:\ DRIVE_REMOVABLE: 2 ::GetDriveType(lpDrives + nIndex * 4): 4 lpFreeBytesAvailable: 12637241344 lpTotalNumberOfBytes: 159210532864 lpTotalNumberOfFreeBytes: 12637241344 ......................................... lpDrives + nIndex * 4: F:\ DRIVE_REMOVABLE: 2 ::GetDriveType(lpDrives + nIndex * 4): 3 lpFreeBytesAvailable: 52076490752 lpTotalNumberOfBytes: 160000000000 lpTotalNumberOfFreeBytes: 52076490752 ......................................... lpDrives + nIndex * 4: G:\ DRIVE_REMOVABLE: 2 ::GetDriveType(lpDrives + nIndex * 4): 5 lpFreeBytesAvailable: 0 lpTotalNumberOfBytes: 0 lpTotalNumberOfFreeBytes: 0 ......................................... lpDrives + nIndex * 4: H:\ DRIVE_REMOVABLE: 2 ::GetDriveType(lpDrives + nIndex * 4): 2 驱动准备就绪 lpFreeBytesAvailable: 3812806656 lpTotalNumberOfBytes: 3860234240 lpTotalNumberOfFreeBytes: 3812806656 driver: C:\D:\E:\F:\G:\H:\H:\ Output completed (4 sec consumed) - Normal Termination