My problem is at line 54 and line 124.. Not sure how to fix it at all.. I basically just want to store all the values of NameIdx to the vector and print the values of the vector. But instead if shows random/AltKeyCode characters and crashes. I know its probably because the vector has no value at that address but I don't know why because I assigned it one with .push_back.

#define _WIN32_WINNT 0x0601
#define BUFSIZE MAX_PATH
#include <windows.h>
#include <cstdio>
#include <iostream>
#include <WinBase.h>
#include <vector>

#define ARRAYSIZE(a) ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))


using namespace std;

vector<string> DevicePath;

void DisplayVolumePaths(char* VolumeName, vector <string> &DevicePath)
{
    DWORD  CharCount = MAX_PATH + 1;
    char* Names     = NULL;
    char* NameIdx   = NULL;
    BOOL   Success   = FALSE;

    while(1)
    {
        Names = (char*) new BYTE [CharCount * sizeof(CHAR)];

        if (!Names)
        {
            return;
        }

        //  Obtain all of the paths for this volume.
        Success = GetVolumePathNamesForVolumeName(VolumeName, Names, CharCount, &CharCount);
        if(Success)
        {
            break;
        }

        if (GetLastError() != ERROR_MORE_DATA)
        {
            break;
        }

        //  Try again with the new suggested size.
        delete [] Names;
        Names = NULL;
    }

    if (Success)
    {
        //  Display the various paths.
        for (NameIdx = Names; NameIdx[0] != '\0'; NameIdx += strlen(NameIdx) + 1)
        {
            DevicePath.push_back(NameIdx);
        }
    }

    if (Names != NULL)
    {
        delete [] Names;
        Names = NULL;
    }

    return;
}

int main()
{
    DWORD  CharCount            = 0;
    char  DeviceName[MAX_PATH] = "";
    DWORD  Error                = ERROR_SUCCESS;
    HANDLE FindHandle           = INVALID_HANDLE_VALUE;
    BOOL   Found                = FALSE;
    size_t Index                = 0;
    BOOL   Success              = FALSE;
    char  VolumeName[MAX_PATH] = "";

    //
    //  Enumerate all volumes in the system.
    FindHandle = FindFirstVolume(VolumeName, ARRAYSIZE(VolumeName));

    if (FindHandle == INVALID_HANDLE_VALUE)
    {
        Error = GetLastError();
        cout<<"FindFirstVolume failed with error code:\n"<<Error;
        return 0;
    }

    while(1)
    {
        //  Skip the \\?\ prefix and remove the trailing backslash.
        Index = strlen(VolumeName) - 1;

        if (VolumeName[0] != '\\' || VolumeName[1] != '\\' || VolumeName[2] != '?' || VolumeName[3] != '\\' || VolumeName[Index] != '\\')
        {
            Error = ERROR_BAD_PATHNAME;
            cout<<"FindFirstVolume/FindNextVolume returned a bad path:\n"<<VolumeName;
            break;
        }

        //  QueryDosDeviceW does not allow a trailing backslash, so temporarily remove it.
        VolumeName[Index] = '\0';

        CharCount = QueryDosDevice(&VolumeName[4], DeviceName, ARRAYSIZE(DeviceName));

        VolumeName[Index] = '\\';

        if (CharCount == 0)
        {
            Error = GetLastError();
            cout<<"QueryDosDevice failed with error code\n"<<Error;
            break;
        }
/*
        cout<<"\nFound a device:\n"<<DeviceName;
        cout<<"\nVolume name: "<<VolumeName;
        cout<<"\nPaths:";
        DisplayVolumePaths(VolumeName);
        cout<<"\n"<<GetDriveType(VolumeName)<<"\n";
*/

        if(GetDriveType(VolumeName) == 2 || GetDriveType(VolumeName) == 3)
        {
            DisplayVolumePaths(VolumeName, DevicePath);
            cout<<DevicePath[1];
            cout<<"\n";
        }

        //  Move on to the next volume.
        Success = FindNextVolume(FindHandle, VolumeName, ARRAYSIZE(VolumeName));

        if (!Success)
        {
            Error = GetLastError();

            if (Error != ERROR_NO_MORE_FILES)
            {
                cout<<"FindNextVolume failed with error code"<<Error;
                break;
            }

            //  Finished iterating through all the volumes.
            Error = ERROR_SUCCESS;
            break;
        }
    }

    FindVolumeClose(FindHandle);
    FindHandle = INVALID_HANDLE_VALUE;

    return 0;
}

There's a bunch of issues with the code.
1. An argument DevicePath of DisplayVolumePaths shadows the global DevicePath.
2. You are printing DevicePath[1] regardless of how many paths has been found.
3. Most important problem: DevicePath is populated with pointers to memory which is deleted at line 60.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.