【问题标题】:GetLogicalDriveStrings() and char - Where am I doing wronglyGetLogicalDriveStrings() 和 char - 我在哪里做错了
【发布时间】:2013-09-02 12:09:50
【问题描述】:

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

【问题讨论】:

  • 您将“text.ext”分配给extFile 100 次,然后打印一个由lpBuffer[100](超出缓冲区末尾)和名为datFile 的字符串组成的字符串在您的示例中其他任何地方都没有出现。请在发布帮助之前更仔细地检查您的代码。

标签: c++ char msdn const-char


【解决方案1】:

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

A:\&lt;nul&gt;B:\&lt;nul&gt;C:\&lt;nul&gt;&lt;nul&gt;

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

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找到。

【讨论】:

  • 嗯.. 谢谢 :) 只是为了正常运行,在项目属性 Unicode 中设置为未设置
【解决方案2】:

你的意思是把 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?
   }

【讨论】:

  • 嗯,是的,我希望输出为C:\text.ext ... upto nth drive:\text.ext。我已经稍微修改了我的代码,请检查...
  • 查看乔纳森的回答
【解决方案3】:
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;

【讨论】:

    【解决方案4】:
    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; 
            }
        }
    };
    

    【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2015-03-21
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多