GetLogicalDriveStrings()和char - 我在哪里做错了

时间:2013-09-02 12:09:51

标签: c++ char msdn const-char

我想搜索可能存在于任何驱动器中的文件,例如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 个答案:

答案 0 :(得分:6)

GetLogicalDriveStrings()返回以null结尾的空终止字符串列表。例如,假设您的机器中有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;
    }
}

请注意,reading the docs可以找到该功能的详细信息,包括我无耻地复制和粘贴的示例代码。

答案 1 :(得分: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?
   }

答案 2 :(得分: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;

答案 3 :(得分: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; 
        }
    }
};