C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,476 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,741 people online right now. Registration is fast and FREE... Join Now!




Gather & Display all drives on a Windows System

 
Reply to this topicStart new topic

> Gather & Display all drives on a Windows System, Uses Windows API

no2pencil
Group Icon



post 25 Nov, 2008 - 06:40 AM
Post #1


In this Tutorial we are going to go over how to gather & then display all of the drives on your Windows System.

1st, we need to include the Windows.h header, as it is required by the two functions we are going to use from the Widows API.

These functions are GetLogicalDriveStrings & GetDriveType.

CODE

#include <stdio.h>
#include <windows.h> // required for GetLogicalDriveStrings & GetDriveType

#define MAX 256



These functions are fairly simple to use. In order to get started, we are going to need to declare a buffer. The function GetLogicalDriveStrings simply fills a buffer with Null terminated strings. This is both convenient, & inconvenient. It's convenient, because the strings that it prepares can be fed directly into GetDriveType with no modification. It's inconvenient, because of the Null termination we need to parse it with a pointer.

So getting started, lets create a buffer, & a pointer.

CODE

int main(void) {
    int dr_type=99;
    char dr_avail[MAX];
    char *temp=dr_avail;


Now we'll fill our buffer, as well as indicate the max size of the buffer. Since we defined MAX to be 256 earlier, we'll just use that smile.gif

CODE

    GetLogicalDriveStrings(MAX,dr_avail);

Now dr_avail, defined as char, will contain a Null terminated string of all of our drive letters. Now we'll parse that into the pointer at each Null, & then deal with them individually. Once they are seperated into the Temp pointer, we will pass that pointer to GetDriveType. Since the string already holds the format that GetDriveType requires, we don't need to alter anything. GetDriveType requires the drive letter, the colon, & it must have the backspace. Again, this is exactly how GetLogicalDriveStrings prepares it smile.gif

CODE

    while(*temp!=NULL) { // Split the buffer by null
        dr_type=GetDriveType(temp);


Now dr_type holds the int return value from GetDriveType. Lets go over the different return values!

CODE

        switch(dr_type) {
            case 0: // Unknown
            printf("%s : Unknown Drive type\n",temp);
            break;
            case 1: // Invalid
            printf("%s : Drive is invalid\n",temp);
            break;
            case 2: // Removable Drive
            printf("%s : Removable Drive\n",temp);
            break;
            case 3: // Fixed
            printf("%s : Hard Disk (Fixed)\n",temp);
            break;
            case 4: // Remote
            printf("%s : Remote (Network) Drive\n",temp);
            break;
            case 5: // CDROM
            printf("%s : CD-Rom/DVD-Rom\n",temp);
            break;
            case 6: // RamDrive
            printf("%s : Ram Drive\n",temp);
            break;
        }


Yup, just the seven different values. Now we just need to increment our pointer & run the loop again.

CODE

        temp += lstrlen(temp) +1; // incriment the buffer
    }
    return 0;
}


& that's it! I hope you find this useful. Here is the entire code in one chunk.

cpp

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

#define MAX 256

int main(void) {
int dr_type=99;
char dr_avail[MAX];
char *temp=dr_avail;

/* 1st we fill the buffer */
GetLogicalDriveStrings(MAX,dr_avail);
while(*temp!=NULL) { // Split the buffer by null
dr_type=GetDriveType(temp);
switch(dr_type) {
case 0: // Unknown
printf("%s : Unknown Drive type\n",temp);
break;
case 1: // Invalid
printf("%s : Drive is invalid\n",temp);
break;
case 2: // Removable Drive
printf("%s : Removable Drive\n",temp);
break;
case 3: // Fixed
printf("%s : Hard Disk (Fixed)\n",temp);
break;
case 4: // Remote
printf("%s : Remote (Network) Drive\n",temp);
break;
case 5: // CDROM
printf("%s : CD-Rom/DVD-Rom\n",temp);
break;
case 6: // RamDrive
printf("%s : Ram Drive\n",temp);
break;
}
temp += lstrlen(temp) +1; // incriment the buffer
}
return 0;
}
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

likesoursugar
*



post 16 Mar, 2009 - 11:38 AM
Post #2
QUOTE(no2pencil @ 25 Nov, 2008 - 06:40 AM) *

In this Tutorial we are going to go over how to gather & then display all of the drives on your Windows System.


Well I'm sure it's nice but I can't make it work. I got 3 errors:

1 Error 13 error C2664: 'GetLogicalDriveStringsW' : cannot convert parameter 2 from 'char [256]' to 'LPWSTR'

2 Error 15 error C2664: 'GetDriveTypeW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'

3 Error 39 error C2664: 'lstrlenW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'

Go to the top of the page
+Quote Post

JackOfAllTrades
Group Icon



post 18 Mar, 2009 - 11:07 AM
Post #3
QUOTE(likesoursugar @ 16 Mar, 2009 - 02:38 PM) *

QUOTE(no2pencil @ 25 Nov, 2008 - 06:40 AM) *

In this Tutorial we are going to go over how to gather & then display all of the drives on your Windows System.


Well I'm sure it's nice but I can't make it work. I got 3 errors:

1 Error 13 error C2664: 'GetLogicalDriveStringsW' : cannot convert parameter 2 from 'char [256]' to 'LPWSTR'

2 Error 15 error C2664: 'GetDriveTypeW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'

3 Error 39 error C2664: 'lstrlenW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'


If you run into these errors, you're configured to use the Unicode character set. This code doesn't take that into account, so you can get it to run by changing to the ANSI multi-byte character set in Project Properties Page->Configuration Properties->General->Project Defaults->Character Set (VS 2008).
Go to the top of the page
+Quote Post

no2pencil
Group Icon



post 6 Apr, 2009 - 05:23 PM
Post #4
I compiled it with command line tools. Know how to use your IDE, is all I can suggest.

Thanks JackOfAllTrades biggrin.gif icon_up.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 03:30AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month