re-post garbled output: GetLogical DriveStrings

This is a discussion on re-post garbled output: GetLogical DriveStrings within the Windows Programming forums, part of the Platform Specific Boards category; why do i get garbled output from this: BOOL getLogicalNames(char *psDriveBuffer, DWORD nBufLen) { DWORD nRes; printf("Getting Logical Names\n"); if ...

  1. #1
    richgi@hotmail.com
    Guest

    re-post garbled output: GetLogical DriveStrings

    why do i get garbled output from this:

    BOOL getLogicalNames(char *psDriveBuffer, DWORD nBufLen)
    {
    DWORD nRes;
    printf("Getting Logical Names\n");

    if ( (nRes = GetLogicalDriveStrings (nBufLen, psDriveBuffer) )== 0 )
    return FALSE;
    else
    {
    printf("%d, %s", nRes, psDriveBuffer);
    return TRUE;
    }
    }

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    psDriveBuffer has to be an array, orelse you'll get an "access violation".
    Try it this way:

    //----------code-start--------------
    char psDriveBuffer[256]; //modify array size as you need
    DWORD nBufLen;

    DWORD nRes;
    nBufLen = 256;
    printf("Getting Logical Names\n");

    nRes = GetLogicalDriveStrings (nBufLen, &psDriveBuffer[0]);

    ...
    //----------code-ends---------------

    Have a nice code!

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    DWORD GetLogicalDriveStrings(
    DWORD nBufferLength, // size of buffer
    LPTSTR lpBuffer // pointer to buffer for drive strings
    );

    nBufferLength
    Specifies the maximum size, in characters, of the buffer pointed to by lpBuffer. This size does not include the terminating null character.

    lpBuffer
    Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, that end with a second null character. The following example shows the buffer contents with <null> representing the terminating null character.
    c:\<null>d:\<null><null>



    If you set the two parameters accordingly, i.e. allocated space
    for the char* and wrote the size of the allocated space to
    your dword, I don't see why you shouldn't get the first
    logical drive. You won't get all, though with your method
    of printing, because printf will go for zero terminated strings,
    and your output has zeros in it that are not meant to be
    the end of the output, just the end of one string.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    To list the drives with printf (as each entry is 4 bytes long e.g. A:\terminating_zero) try this:

    #define MAX_DRIVE_NO 24

    char psDriveBuffer[MAX_DRIVE_NO][4];
    ...
    GetLogicalDriveStrings (nBufLen, &psDriveBuffer[0][0]);

    for ( int i = 0; i <= MAX_DRIVE_NO, i++ )
    {
    printf(%s"psDriveBuffer[i][0]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why this output?
    By dredre in forum C Programming
    Replies: 4
    Last Post: 05-08-2004, 04:09 PM
  2. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM
  3. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 11:11 AM
  4. garbled output: Win32 : GetLogicalDriveStrings()
    By richgi in forum Windows Programming
    Replies: 2
    Last Post: 10-10-2001, 02:15 AM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21