明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

讀取全部的驅(qū)動(dòng)器的信息

[摘要]作者:安靜 VC6.0 W2K編譯通過 這類小程序,能用的地方很多.所以就寫一個(gè)完整的代碼 給大家參考參考 #include "stdafx.h" #include <i...
作者:安靜

VC6.0 W2K編譯通過

這類小程序,能用的地方很多.所以就寫一個(gè)完整的代碼
給大家參考參考

#include "stdafx.h"
#include  <iostream.h>
const DWORD MAXLEN = 100;

void ShowDriveInfo(LPTSTR drive)
{
//輸出設(shè)備類型
UINT result;
result = GetDriveType(drive);

if (result == DRIVE_REMOVABLE)
cout << "可移動(dòng)設(shè)備" ;
else if (result == DRIVE_FIXED)
cout << "硬盤";
else if (result == DRIVE_REMOTE)
cout << "網(wǎng)絡(luò)驅(qū)動(dòng)器" ;
else if (result == DRIVE_CDROM )
cout << " 光驅(qū)";
else if (result == DRIVE_RAMDISK)
cout << "Ram Disk";
else if (result == DRIVE_UNKNOWN)
cout << "未知的設(shè)備";
else
return;
cout << '\t';

//給出空間信息

unsigned __int64 i64FreeBytesToCaller;
unsigned __int64 i64TotalBytes;
unsigned __int64 i64FreeBytes;

DWORD dwSectPerClust;
DWORD dwBytesPerSect;
DWORD dwFreeClusters;
DWORD dwTotalClusters;

DWORD tempTotal;
DWORD tempFree;

BOOL fResult;
typedef DWORD (WINAPI * GETDISKFREESPACEEX)(LPCTSTR ,
PULARGE_INTEGER ,
PULARGE_INTEGER ,
PULARGE_INTEGER );

GETDISKFREESPACEEX pGetDiskFreeSpaceEx;

pGetDiskFreeSpaceEx = (GETDISKFREESPACEEX)GetProcAddress( GetModuleHandle("kernel32.dll"),
"GetDiskFreeSpaceExA");

if (pGetDiskFreeSpaceEx) //如果是Windows NT and Windows 2000使用 GetDiskFreeSpaceEx
{
fResult = pGetDiskFreeSpaceEx ( (LPCTSTR)drive,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);

tempTotal = i64TotalBytes/1024 ;
tempFree = i64FreeBytes/1024;

}

else //如果是Windows 95 OSR2 and Windows 98 使用 GetDiskFreeSpace
{
fResult = GetDiskFreeSpace (drive,
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);

tempTotal = dwTotalClusters*dwBytesPerSect*dwSectPerClust/1024;
tempFree = dwFreeClusters*dwSectPerClust*dwBytesPerSect/1024;



}
if(fResult)
{
cout<<"全部磁盤容量是 "<<((float)(tempTotal)/1024/1024)<<"GB\t";
cout<<"空余磁盤容量是 "<<((float)(tempFree)/1024/1024)<<"GB\t";
}
cout<<endl;

//像光驅(qū),軟盤,不放盤的時(shí)候,GetDiskFreeSpace(Ex)會(huì)出錯(cuò).

}
void GetAllDrive()
{

int len = -1;
char drive[4] ="AAA" ;
LPTSTR lpDriveString = new char[MAXLEN];
DWORD dwBufferLen = MAXLEN ;
len = GetLogicalDriveStrings(dwBufferLen,lpDriveString); //取得全部的盤符
if (len < 0)
cout << "操作失敗" <<endl;
if (len > MAXLEN)
cout << "Buffer不足" <<endl;
if (len > 0)
{
for (int i = 0;i < len;i = i+4) //得到的盤符有4個(gè)字符組成 A:\(null)
{
drive[0] = lpDriveString[i]; //A
drive[1] = lpDriveString[i+1];//:
drive[2] = lpDriveString[i+2];// 第三個(gè)\
//drive[3] = '\0'; // 第四個(gè)為null字符
cout<<drive<<'\t';

ShowDriveInfo(drive);
}
}
/////////////////////////////////////////////
//另外一種方法.
///////////////////////////////////////////////
/*
char D = 'A';
char Drive[4]="X:\\";
for(int i = 0; i < 26;i++)
{
Drive[0] = (char)(D+i);
cout<<Drive<<'\t';
ShowDriveInfo(Drive);
}
*/

}

void main()
{
GetAllDrive();
}