返回系统发光字母列表的程序

在我的电脑的例子中,所需的输出应该是:“C:E:F:H:N:”。 我知道这是可能的,但最简单的方法是什么? 在QueryDosDevice输出中input

#ifndef UNICODE #define UNICODE #endif #include <Windows.h> #include <fstream> #include <iostream> const int REPORT_LENGTH = 5000; int main(void) { TCHAR targetPath[REPORT_LENGTH]; std::ofstream oFile; oFile.open("dos device query.txt"); QueryDosDevice(NULL,targetPath,REPORT_LENGTH); for(int i=0; i<REPORT_LENGTH;i++) if (targetPath[i]=='\0')(targetPath[i]='\n'); for(int i=0; i<REPORT_LENGTH; i++) oFile<<static_cast<char>(targetPath[i]); oFile.close(); return 0; } 

会浪费大量的时间和资源。 另外函数GetLogicalDriveStrings已经背叛了我很多。

 #include <Windows.h> int main() { TCHAR buffer[50]; GetLogicalDriveStrings(50,buffer); MessageBox(0,buffer,"Drives in the system",MB_OK); return 0; } 

它只显示“C:\”volumine。

GetLogicalDrives ,尽管不连接到一个字符串(这是留给OP和读者的练习)):

 #include <stdio.h> #include <tchar.h> #include <Windows.h> int __cdecl _tmain(int argc, _TCHAR *argv[]) { // Get the bit mask of drive letters DWORD drives = ::GetLogicalDrives(); // Go through all possible letters from a to z for(int i = 0; i < 26; i++) { // Check if the respective bit is set if(drives & (1 << i)) { // ... and if so, print it _tprintf(TEXT("Drive %c: exists\n"), _T('A') + i); } } return 0; } 

GetLogicalDriveStrings()是要走的路,你只需要正确使用。 你假设它返回一个包含所有驱动器字符串的字符串,但这不是真的。 它返回一个字符串数组,每个驱动器一个,所以你必须循环访问数组:

 #include <windows.h> int main() { TCHAR buffer[(4*26)+1] = {0}; GetLogicalDriveStrings(sizeof(buffer) / sizeof(TCHAR), buffer); for (LPTSTR lpDrive = buffer; *lpDrive != 0; lpDrive += 4) MessageBox(NULL, lpDrive, "Drive in the system", MB_OK); return 0; }