Почему я не могу передать строку в функцию GetDriveTypesA ()? - PullRequest
0 голосов
/ 14 марта 2019

Итак, цель программы - узнать, какие диски доступны и какие они есть.Однако я не могу передать строковую переменную прямо в GetDriveTypesA().Но, если я просто сделаю GetDriveTypesA("C:\\"), он будет успешно работать.

Пожалуйста, кто-нибудь может помочь мне найти решение этой проблемы, программа ниже.Спасибо:)

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main() {
    int i;
    int driveType;
    std::string path;
    int binary[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //To save the binary number of the GetLogicalDrives
    std::string  actualDrives[26];
    std::string letters[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; //letters of the alphabet
    DWORD drives = GetLogicalDrives(); //finds the bit mask of logical drives

    for(i=0; drives>0; i++) { //changes the integer into a binary number
        binary[i]=drives%2;
        drives= drives/2;
    }
    for (i=0; i<26; i++){ //finds out what drive letters are available
        if (binary[i]==1){
            actualDrives[i] = letters[i];
        }
    }
    for (i=0; i<26; i++){ \trying to find the drive type
        if (actualDrives[i] != ""){
        cout << "Actual drive:";
        cout<< actualDrives[i] <<endl;
        path = actualDrives[i] + ":\\";
        cout << path <<endl;
        driveType = GetDriveTypeA(path); //the part of the code that does not work but does work if a string is just entered
        cout<< driveType << endl;
        }
    }

}

1 Ответ

4 голосов
/ 14 марта 2019

Поскольку GetDriveTypeA() принимает C строку , а не C ++ std::string.

UINT GetDriveTypeA(
  LPCSTR lpRootPathName
);

Используйте std::string.c_str() метод:

driveType = GetDriveTypeA(path.c_str());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...