Click to See Complete Forum and Search --> : Get letter drives
MPrestonf12
Dec 28th, 2001, 10:58 AM
im trying to get each drive on my computer. Is their a way to do this? Right now im just having it loop through four times for each drive on my computer but i want it to be portable for other computers with more or less drives.
char buff[10];
int i;
for(i=0;i<4;i++){
GetLogicalDriveStrings(10,buff);
MessageBox(NULL,buff,"dd",MB_OK);
}
this only return "A:/ "four times though.
thanks
parksie
Dec 28th, 2001, 11:19 AM
#include <windows.h>
#include <math.h>
#include <stdio.h>
BOOL is_drive(char letter, unsigned long drives) {
return drives & (unsigned long)pow(2, letter - 'A');
}
int main(void) {
unsigned long drvspec = GetLogicalDrives();
for(char i = 'A'; i <= 'Z'; ++i) {
if(is_drive(i, drvspec)) {
printf("Drive %c\n", i);
}
}
return 0;
}
MPrestonf12
Dec 28th, 2001, 01:50 PM
Parksie, do you mind explaining breifly how that works. Thanks
parksie
Dec 28th, 2001, 01:52 PM
GetLogicalDrives returns an unsigned long (DWORD), where drive A is bit 0, drive B is bit 1, etc.
If they're set, the drive exists. So what that does is just to check through all of them seeing if the corresponding bit is set. I suppose I could drop the call to pow and use a shift (quicker, but I can't remember which way round it is for Intel).
MPrestonf12
Dec 28th, 2001, 02:03 PM
im not sure why but I get a illegal operation whenI try and run it.
parksie
Dec 28th, 2001, 02:06 PM
Works fine for me...
MPrestonf12
Dec 28th, 2001, 02:12 PM
sorry, your code was right but here was the problem.
SendMessage(drvlstS,LB_ADDSTRING,0,(LPARAM)i);
do you know why I can't set that string?
parksie
Dec 28th, 2001, 02:16 PM
Because i is only a single character.char buf[2] = { i, 0 };
SendMessage(drvlstS,LB_ADDSTRING,0,(LPARAM)buf);
MPrestonf12
Dec 28th, 2001, 02:20 PM
Thanks alot Parksie!
parksie
Dec 28th, 2001, 02:24 PM
No probs :)
Oh yeah, anything that goes into that function must be upper case or it won't work.
CornedBee
Jan 6th, 2002, 09:12 AM
Another version of parksies function:
// add this include
#include <ctype.h>
// and for debug purposes
#include <assert.h>
BOOL is_drive(char letter, unsigned long drives) {
assert(isalpha(letter));
return drives & (1 << (toupper(letter) - 'A'));
}
I'm pretty sure that left shift is right, but you must try. If it doesn't work correctly, try
0x80000000 >> (toupper(letter) - 'A')
or stick to parksies way.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.