GetLogicalDriveStrings() and char - Where am I doing wrongly

11.7k views Asked by At

I want to search a file which may be present in any drives such as C:\, D:\ etc. Using GetLogicalDriveStrings I can able to get the list of drives but when I add anything extra for the output, I am getting a null in the output prompt. Here is my code:

#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;
}

I want above output as C:\text.ext D:\text.ext ... rather I am getting text.ext only. I am using Microsoft Visual C++ 2010 Express

4

There are 4 answers

1
Jonathan Potter On BEST ANSWER

GetLogicalDriveStrings() returns a double-null terminated list of null-terminated strings. E.g., say you had drives A, B and C in your machine. The returned string would look like this:

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

You can use the following code to iterate through the strings in the returned buffer and print each one in turn:

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;
    }
}

Note that the details of how the function works, including the example code that I shamelessly copied and pasted, can be found by reading the docs.

2
doctorlove On

Did you mean to put the printf in the loop?
Currently, you set extFile 100 times (just to be sure?!)

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

You meant to show all the drive letters in a loop:

   for(i = 0; i<100; i++)
   {
        extFile = "text.ext";
        printf("%c%s", lpBuffer[i], extFile); //I guess you mean extFile here?
   }
1
AudioBubble On
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; 
        }
    }
};
0
Michael Haephrati On
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;