views:

51

answers:

3

When i execute my code i am getting this error

LPTSTR lpBuffer;
::GetLogicalDriveStrings(1024,lpBuffer);
while(*lpBuffer != NULL)
{
  printf("%s\n", lpBuffer); // or MessageBox(NULL, temp, "Test", 0); or whatever
  lpBuffer += lstrlen(lpBuffer)+1;
  printf("sizeof(lpBuffer) %d\n",lstrlen(lpBuffer));
}

OutPut

C

sizeof(lpBuffer) 3

D

sizeof(lpBuffer) 3

E

sizeof(lpBuffer) 3

F

sizeof(lpBuffer) 0

A: 

lpBuffer points to random memory. You need something like this:

LPTSTR lpBuffer = new TCHAR[1025];

edit: Corrected the array size to be 1025 instead of 1024, because the length parameter is 1024. This API requires careful reading.

Windows programmer
Thanks.. Its workign fine But y first 3 output getting size as 3.
A string like "E:\" has length 3.
Windows programmer
Once again my thanksif its thaking 3 as size then y last statment have size 0Fsizeof(lpBuffer) 0
@sijith: you can not rely on that size as the behavior is undefined. It can be 3 or it can be 100 since the memory you are pointing is just a random location.
Naveen
String "F:\" has length 3 not zero. That one is followed by an empty string and you're getting the length of that empty string, which is 0.
Windows programmer
A: 

You are supposed to pass a memory address where the string will be copied. However you have not allocated any space for holding the characters. You need to allocate space before passing it to the GetLogicalDriveStrings function. You can allocate the memory on heap as @Windows programmer suppgested or if the maximum length of the string is known at compile time you can allocate it on stack using TCHAR lpBuffer[1024]; Additinally, you are using printf to print the unicode (may be as it depends on compiler flag). This will not work and will print only first character.

Naveen
A: 

You need to actually pass in a buffer - note that the size of the buffer you pass in needs to be one less than the actual size of the buffer to account for the final terminating '\0' character (I have no idea why the API was designed like that).

Here's a slightly modified version of your example:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

enum {
    BUFSIZE = 1024
};

int _tmain (int argc, TCHAR *argv[])
{
    TCHAR szTemp[BUFSIZE];
    LPTSTR lpBuffer = szTemp;   // point lpBuffer to the buffer we've allocated


    szTemp[0] = _T( '\0');  // I'm not sure if this is necessary, but it was
                            //   in the example given for GetLogicalDriveStrings()

    GetLogicalDriveStrings( BUFSIZE-1, lpBuffer);   // note: BUFSIZE minus 1

    while(*lpBuffer != _T('\0'))
    {
      _tprintf( _T("%s\n"), lpBuffer);
      lpBuffer += lstrlen(lpBuffer)+1;
      _tprintf( _T("length of lpBuffer: %d\n"),lstrlen(lpBuffer));
    }

    return 0;
}
Michael Burr
You can not use `printf` with unicode strings. It may encounter `\0` character after the first character it self.
Naveen
The second parameter to main has trouble in Unicode builds too.
Windows programmer
Oh I see, the original program had the same bugs and Michael Burr didn't notice what he copied :-)
Windows programmer
OK, OK, it's not so much that I didn't notice, but I just dropped the original example into my standard test project, which isn't a TCHAR program. I honestly don't think it detracts from the point of the answer, but I edited the answer to fix it up anyway (I think), as I probably should have in the first place (or gotten rid of `TCHAR` stuff altogether) in case someone drops this into a VS project that was set up for UNICODE.
Michael Burr
Now you really bugged it. In a Unicode build, _tprintf will map to the Unicode version, but your "format" parameters are still ANSI because you didn't fit them to a _T().
Windows programmer
@Windows programmer: Fixed.
Michael Burr