Results 1 to 11 of 11

Thread: Get letter drives

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    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
    Matt

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Thumbs up

    Parksie, do you mind explaining breifly how that works. Thanks
    Matt

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    im not sure why but I get a illegal operation whenI try and run it.
    Matt

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Works fine for me...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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?
    Matt

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Because i is only a single character.
    Code:
    char buf[2] = { i, 0 };
    
    SendMessage(drvlstS,LB_ADDSTRING,0,(LPARAM)buf);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Thanks alot Parksie!
    Matt

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No probs

    Oh yeah, anything that goes into that function must be upper case or it won't work.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Another version of parksies function:
    PHP Code:
    // add this include
    #include <ctype.h>
    // and for debug purposes
    #include <assert.h>

    BOOL is_drive(char letterunsigned long drives) {
        
    assert(isalpha(letter));
        return 
    drives & (<< (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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width