-
Get letter drives
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.
Code:
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
-
Code:
#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;
}
-
Parksie, do you mind explaining breifly how that works. Thanks
-
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).
-
im not sure why but I get a illegal operation whenI try and run it.
-
-
sorry, your code was right but here was the problem.
Code:
SendMessage(drvlstS,LB_ADDSTRING,0,(LPARAM)i);
do you know why I can't set that string?
-
Because i is only a single character.
Code:
char buf[2] = { i, 0 };
SendMessage(drvlstS,LB_ADDSTRING,0,(LPARAM)buf);
-
-
No probs :)
Oh yeah, anything that goes into that function must be upper case or it won't work.
-
Another version of parksies function:
PHP Code:
// 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.