枚举系统磁盘驱动器(使用GetLogicalDriveStrings API函数,system("pause"); 很实用,还用到wcslen等函数)

代码如下:

[cpp] view plain copy
 
  1. #include "stdafx.h"  
  2. #include <vector>  
  3. #include <string>  
  4. #include <Windows.h>  
  5. #include <tchar.h>  
  6.   
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     TCHAR  drives[128];             //存储所以驱动器名称  
  11.     wchar_t* pDrive;                //驱动器指针  
  12.     std::vector<std::wstring> strArray;  
  13.   
  14.     //取得系统的第一个逻辑驱动器  
  15.     if (!GetLogicalDriveStrings(sizeof(drives), drives))  
  16.     {  
  17.         printf("获取驱动器失败 ");  
  18.         return false;  
  19.     }  
  20.     pDrive = drives; //指向第一个逻辑驱动器  
  21.     //将驱动器字符放入列表框中  
  22.     while(*pDrive)  
  23.     {  
  24.         //将驱动器名称加入列表中  
  25.         strArray.push_back(pDrive);  
  26.   
  27.         //指向下一个驱动器标识符  
  28.         pDrive += wcslen(pDrive) + 1;  
  29.     }  
  30.   
  31.     for (int i=0; i<strArray.size(); ++i)  
  32.     {  
  33.         printf("%ls ", strArray[i].c_str());  
  34.     }  
  35.   
  36.     system("pause");  
  37.     return 0;  
  38. }  

https://blog.csdn.net/hellokandy/article/details/73649501

原文地址:https://www.cnblogs.com/findumars/p/8734189.html