設置 | 登錄 | 註冊

作者共發了8篇帖子。

【API】獲取驅動器列表及其詳細信息

1樓 巨大八爪鱼 2016-3-3 22:09
#include <iostream>
#include <tchar.h>
#include <Windows.h>

using namespace std;

int main(void)
{
    DWORD drives = GetLogicalDrives();
    cout << "Drives Mask: " << drives << endl;

    TCHAR str[100];
    LPTSTR pStr = str;
    HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    GetLogicalDriveStrings(sizeof(str), str);
    while (*pStr != '\0')
    {
        size_t len = _tcslen(pStr);
        WriteConsole(hConsoleOutput, pStr, len, NULL, NULL);
        cout << ": ";

        UINT uType = GetDriveType(pStr);
        switch (uType)
        {
        case DRIVE_NO_ROOT_DIR:
            cout << "空驅動器";
            break;
        case DRIVE_REMOVABLE:
            cout << "可移動磁碟";
            break;
        case DRIVE_FIXED:
            cout << "硬碟";
            break;
        case DRIVE_REMOTE:
            cout << "網絡驅動器";
            break;
        case DRIVE_CDROM:
            cout << "光碟機";
            break;
        case DRIVE_RAMDISK:
            cout << "只讀存儲器";
            break;
        case DRIVE_UNKNOWN:
        default:
            cout << "未知";
            break;
        }

        cout << endl;
        pStr += len + 1;
    }

    HANDLE hFindVolume = FindFirstVolume(str, sizeof(str));
    do
    {
        WriteConsole(hConsoleOutput, str, _tcslen(str), NULL, NULL);
        cout << endl;
    } while (FindNextVolume(hFindVolume, str, sizeof(str)));
    FindVolumeClose(hFindVolume);

    DWORD dwSerialNumber, dwMaxCompLen, dwFlags;
    TCHAR szType[10];
    cout << endl;
    GetVolumeInformation(TEXT("C:\\"), str, sizeof(str), &dwSerialNumber, &dwMaxCompLen, &dwFlags, szType, sizeof(szType));
    cout << "dwSerialNumber: " << dwSerialNumber << endl;
    cout << "dwMaxCompLen: " << dwMaxCompLen << endl;
    cout << "dwFlags: " << dwFlags << endl;
    cout << "szType: ";
    WriteConsole(hConsoleOutput, szType, _tcslen(szType), NULL, NULL);
    cout << endl;

    system("pause");
    return 0;
}
2樓 巨大八爪鱼 2016-3-3 22:09
【輸出】
Drives Mask: 124
C:\: 硬碟
D:\: 硬碟
E:\: 硬碟
F:\: 硬碟
G:\: 光碟機
\\?\Volume{5e889bb2-6c1b-11e5-8413-806e6f6e6963}\
\\?\Volume{5e889bb4-6c1b-11e5-8413-806e6f6e6963}\
\\?\Volume{5e889bb5-6c1b-11e5-8413-806e6f6e6963}\
\\?\Volume{5e889bb6-6c1b-11e5-8413-806e6f6e6963}\
\\?\Volume{5e889bb3-6c1b-11e5-8413-806e6f6e6963}\
\\?\Volume{5e889bb7-6c1b-11e5-8413-806e6f6e6963}\
\\?\Volume{5e889bba-6c1b-11e5-8413-806e6f6e6963}\

dwSerialNumber: 3500692001
dwMaxCompLen: 255
dwFlags: 65470719
szType: NTFS
Press any key to continue . . .
4樓 巨大八爪鱼 2016-7-17 18:35
GetLogicalDriveStrings(sizeof(str), str)
這句代碼中第一個參數沒有寫對
應該是
GetLogicalDriveStrings(_countof(str), str);
才對。
第一個參數是緩衝區最大可容納的字符數,包括\0和\0\0在內。
5樓 巨大八爪鱼 2016-7-17 18:40

【循環輸出驅動器列表的示例】

#include <iostream>
#include <Windows.h>
using namespace std;
#ifdef _UNICODE
ostream &operator << (ostream &os, const wchar_t *wstr)
{
 if (os == cout)
  WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wstr, wcslen(wstr), NULL, NULL);
 return os;
}
#endif
int main(void)
{
 int i = 0;
 TCHAR szDrives[100];
 GetLogicalDriveStrings(_countof(szDrives), szDrives);
 while (szDrives[i] != '\0')
 {
  cout << szDrives + i << endl;
  i += lstrlen(szDrives + i) + 1;
 }
 return 0;
}
【輸出】

C:\
D:\
E:\
F:\
G:\

6樓 巨大八爪鱼 2016-7-17 19:44

1樓的程序中所有的sizeof都要改成_countof,因為API要求的單位都是字符個數。

另外,對於FindFirstVolume和FindNextVolume函數,如果緩衝區大小不夠,那麼直接操作失敗。

7樓 巨大八爪鱼 2016-7-17 19:49

【獲取磁碟卷標的方法】

TCHAR szBuffer[MAX_PATH + 1];
GetVolumeInformation(TEXT("F:\\"), szBuffer, _countof(szBuffer), NULL, NULL, NULL, NULL, NULL);

如果緩衝區不夠大,那麼函數會執行失敗。

獲得的卷標可能會是空字符串。(依磁碟屬性裡面的文本框裡顯示的為準。注意空字符串和「本地磁碟」是有區別的,儘管在「我的電腦」里顯示都是「本地磁碟」)

8樓 巨大八爪鱼 2016-7-17 19:52

【設置磁碟卷標】

SetVolumeLabel(TEXT("F:\\"), TEXT("新卷標"));

【清空磁碟卷標】
SetVolumeLabel(TEXT("F:\\"), NULL);

 

這兩個函數在Win7下必須要以管理員方式運行程序才能執行成功。

9樓 巨大八爪鱼 2016-7-17 20:07

【獲取驅動器全部非根目錄掛載點】

TCHAR szBuffer[MAX_PATH + 1];
HANDLE hFind = FindFirstVolumeMountPoint(TEXT("C:\\"), szBuffer, _countof(szBuffer));
if (hFind == INVALID_HANDLE_VALUE)
{
 DWORD dwErr = GetLastError();
 cout << L"操作失敗, 錯誤碼: " << GetLastError() << endl;
 if (dwErr == ERROR_ACCESS_DENIED)
  cout << L"無權限" << endl;
 else if (dwErr == ERROR_NO_MORE_FILES)
  cout << L"無非根目錄的掛載點" << endl;
}
else
{
 do
 {
  cout << szBuffer << endl;
 } while (FindNextVolumeMountPoint(TEXT("C:\\"), szBuffer, _countof(szBuffer)));
 FindVolumeMountPointClose(hFind);
}

【運行結果一般為】

操作失敗, 錯誤碼: 18
無掛載點

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。