954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

List Windows Logical Drive Letters

By Melvin on Nov 11th, 2009 11:23 pm

This demonstrates how to get a list of your computer's logical drive letters under the MS-Windows operating system.

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


int main()
{
    char buf[255];
    // get the drive letters as a set of strings
    int sz = GetLogicalDriveStrings(sizeof(buf), buf);
    if( sz > 0)
    {
        // buf now contains a list of all the drive letters.  Each drive letter is
        // terminated with '\0' and the last one is terminated by two consecutive '\0' bytes.
        char* p1 = buf;
        char* p2;
        while( *p1 != '\0' && (p2 = strchr(p1,'\0')) != NULL )
        {
            printf("%s\n", p1);
            p1 = p2+1;
        }
    }
    else
    {
        // Oops! something went wrong so display the error message
        DWORD dwError = GetLastError();
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwError, 0, buf, sizeof(buf), 0);
        printf("%s\n", buf);

    }
}

really nice snippet (: I'm sure this will come in handy for me sometime.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

See rather the Microsoft sample.
16 years old... (beginner code for a so simple api, posted 10000 times on BBS & Usenet...)

marco93
Junior Poster
132 posts since Apr 2008
Reputation Points: -76
Solved Threads: 14
 

>>See rather the Microsoft sample
What sample? Post link.

[edit] Probably this ?

>>posted 10000 times on BBS & Usenet.
Probably -- now its been posted 10001 times :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: