4

我想搜索可能存在于任何驱动器中的文件,例如 C:\、D:\ 等。使用GetLogicalDriveStrings我可以获得驱动器列表,但是当我为输出添加任何额外内容时,我null在输出提示。这是我的代码:

#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>

// Buffer length
DWORD mydrives = 100;
// Buffer for drive string storage
char lpBuffer[100];
const char *extFile = "text.ext";

// You may want to try the wmain() version
int main(void)
{
    DWORD test;
    int i;
    test = GetLogicalDriveStrings(mydrives, (LPWSTR)lpBuffer);
    if(test != 0)
    {
        printf("GetLogicalDriveStrings() return value: %d, Error (if any): %d \n", test, GetLastError());
        printf("The logical drives of this machine are:\n");
        // Check up to 100 drives...
        for(i = 0; i<100; i++)
        printf("%c%s", lpBuffer[i],extFile);
        printf("\n");
    }
    else
        printf("GetLogicalDriveStrings() is failed lor!!! Error code: %d\n", GetLastError());
    _getch();
    return 0;
}

我想要上面的输出,因为C:\text.ext D:\text.ext ...我得到的text.ext只是。我在用Microsoft Visual C++ 2010 Express

4

4 回答 4

9

GetLogicalDriveStrings()返回以空结尾的字符串的双空结尾列表。例如,假设您的机器中有驱动器 A、B 和 C。返回的字符串如下所示:

A:\<nul>B:\<nul>C:\<nul><nul>

您可以使用以下代码遍历返回缓冲区中的字符串并依次打印每个字符串:

DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH] = {0};
DWORD dwResult = GetLogicalDriveStrings(dwSize,szLogicalDrives);

if (dwResult > 0 && dwResult <= MAX_PATH)
{
    char* szSingleDrive = szLogicalDrives;
    while(*szSingleDrive)
    {
        printf("Drive: %s\n", szSingleDrive);

        // get the next drive
        szSingleDrive += strlen(szSingleDrive) + 1;
    }
}

请注意,该函数的工作原理,包括我厚颜无耻地复制粘贴的示例代码,可以通过阅读文档找到。

于 2013-09-02T12:23:21.957 回答
0

你的意思是把 printf 放在循环中吗?
目前,您设置了 extFile 100 次(只是为了确定?!)

   for(i = 0; i<100; i++)
       extFile = "text.ext";

您的意思是在循环中显示所有驱动器号:

   for(i = 0; i<100; i++)
   {
        extFile = "text.ext";
        printf("%c%s", lpBuffer[i], extFile); //I guess you mean extFile here?
   }
于 2013-09-02T12:17:16.460 回答
0
DWORD dwSize = MAX_PATH;
WCHAR szLogicalDrives[MAX_PATH] = { 0 };
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);

CStringArray m_Drives;
m_Drives.RemoveAll();

if (dwResult > 0 && dwResult <= MAX_PATH)
{
    WCHAR* szSingleDrive = szLogicalDrives;
    while (*szSingleDrive)
    {
        UINT nDriveType = GetDriveType(szSingleDrive);
        m_Drives.Add(CString(szSingleDrive, 2));

        // get the next drive
        szSingleDrive += wcslen(szSingleDrive) + 1;
    }
}
return m_Drives;
于 2015-08-01T21:15:46.177 回答
0
class DriveList {
    protected:

    LPTSTR m_driveList;
    DWORD m_driveCount;
    DWORD m_bufSize = 32 * sizeof(TCHAR);

    public:

    virtual ~DriveList() {
        free(m_driveList);
    }

    DriveList() {
        m_driveList = (LPTSTR)malloc(m_bufSize);
    }

    int getDriveCount() const {
        return m_driveCount;
    }

    TCHAR operator[] (const int index) const {
        return m_driveList[index];
    }

    void loadDriveList() {
        DWORD mask;
        if((mask = GetLogicalDrives()) == 0) {
            throw;
        }

        m_driveCount = 0;
        for(int x = 0; x <= 25; x++ ) {
            if(mask & 1) {
                m_driveList[m_driveCount] = TCHAR(65 + x);
                m_driveCount += 1;
            }
            mask >>= 1; 
        }
    }
};
于 2018-11-19T13:27:30.190 回答