Following code gives empty string and length = 0, but while debugging I can see the childDisplayName has correct name.
CHAR fileSystemName[MAX_PATH + 1] = { 0 };
DWORD serialNumber = 0; DWORD maxComponentLen = 0;
string childDisplayName = "";
DWORD fileSystemFlags = 0;
if (GetVolumeInformationA("C:\\", // L"\\MyServer\MyShare\"
(LPSTR)&childDisplayName, MAX_PATH+1,
&serialNumber, &maxComponentLen,
&fileSystemFlags, fileSystemName, sizeof(fileSystemName)) == true)
{
cout << childDisplayName << "length: "<<childDisplayName.length()<<endl;
}
following code works fine. I am not getting why LPSTR works when I pass char array and does not work when I pass a string.
CHAR fileSystemName[MAX_PATH + 1] = { 0 };
DWORD serialNumber = 0; DWORD maxComponentLen = 0;
CHAR childDisplayName[MAX_PATH + 1] = { 0 };
DWORD fileSystemFlags = 0;
if (GetVolumeInformationA("C:\\", // L"\\MyServer\MyShare\"
childDisplayName, MAX_PATH+1,
&serialNumber, &maxComponentLen,
&fileSystemFlags, fileSystemName, sizeof(fileSystemName)) == true)
{
cout << childDisplayName << "length: "<<strlen(childDisplayName)<<endl;
}



string childDisplayName = "";creates an empty empty string (zero size and unspecified capacity). Using that as a data-buffer to write into is not likely to go well.You can do this:
string childDisplayName(MAX_PATH + 1, ' ');to create a string with the proper space allocated.Secondly, as @churill wrote, the address of a string is not the address of the characters in it. Instead use
childDisplayName.data()to get achar*to the internal storage of the string that you can write in - but make sure not to write outside the range[data(); data() + size()).EDIT: A bit on how
std::stringand.data()works.I made a small example program:
When run this outputs:
What is going on here? First thing to notice is that an empty
std::stringis created with zero size and unspecified capacity - looking in my debugger, I know that on my system that unspecified capacity is 15, so as long as I don't go beyond that nothing should crash. But this is obviously not something you should do in real code (writing here is strictly undefined behavior).This means that the
blastring is size 0, and contains a 15 character char buffer, where I set the first 5 characters to '!'. So when I try to print itssize()or print it as astd::stringit is identical to any regular empty string. However, if I use.c_str()to print the internal buffer directly, then it prints as any oldchar*and just prints whatever is in memory until it encounters a null-character.On the other hand,
bla2is initialized to contain 10 null-characters. That means that its size is 10, and its capacity is at least 10 (in my case it happens to also be 15). This means that after the loop it still reports as size 10, regardless of how many '!'s I put into the buffer, and when I print it as astd::stringit prints all the 10 characters it contains; both the 5 '!'s and the 5 '\0's. However, when I print it as achar*it prints the 5 '!'s and then stop as soon as it encounters a null-character.