Thread: GetLocalDrives() Can anybody PLEASE PLEASE help me ?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    16

    GetLocalDrives() Can anybody PLEASE PLEASE help me ?

    If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Can anybody Give me an example of what does it mean ?
    This is from GetLogicalDrives() functions of MSDN.

    Thanks

  2. #2
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    It means that, if the value returned from GetLogicalDrives is 0 then the function failed to execute correctly. To find out why it failed you need to call GetLasterror for the error code that will tell you what went wrong.

    In my windows 7 the function misbehaves instead of returning a value of zero when it can't complete its job an unhandled exception is raised closing your program with out any chance to correct it. I guess it has something to do with user rights.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by cry View Post
    If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Can anybody Give me an example of what does it mean ?
    This is from GetLogicalDrives() functions of MSDN.

    Thanks
    Please stop using "please" in your titles. It makes you look stupid and pathetic. You also don't need to say "help me". Obviously you're here for help. Only an idiot would say that in their title.

    I don't understand what you don't understand. Do you know what the word "function" means? How about "succeed"? Is "bitmask" familiar to you? What part don't you understand? You've highlighted (linked, actually) GetLastError. Is that what you don't understand?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by cry View Post
    [FONT="]If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.[/FONT]
    [FONT="]If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Can anybody Give me an example of what does it mean ?
    This is from GetLogicalDrives() functions of MSDN.

    Thanks [/FONT]
    Please re-read @Laserlight's message here.

    Even better, I think you need to take a course with a qualified instructor, as I think you need more than a good book on the C Programming Language could provide.

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by taazz View Post
    In my windows 7 the function misbehaves instead of returning a value of zero when it can't complete its job an unhandled exception is raised closing your program with out any chance to correct it. I guess it has something to do with user rights.
    That doesn't sound right. If that's true, it's a very serious bug, I find it more believable that the bug is in your program.

  6. #6
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    You think? Here is my code.

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    
    int main()
    {
      DWORD vDrives;
      vDrives = GetLogicalDrives();
      if (vDrives == 0) { //GetLogicalDrives failed for some reason.
          vDrives = GetLastError();
        printf("Error Code %d\n", vDrives);
      } else {
        printf("GetLocalDrives : %d\n",vDrives);
      }
      return 0;
    }
    I'm using codeblocks as my IDE I created a console project and typed the above code.
    On execution I get a system dialog informing me that App2 has stopped working. Selecting <Close the program> I get
    Process returned -1073741819 (0xC0000005) execution time : 58.958 s
    Press any key to continue.
    Last edited by taazz; 05-28-2016 at 01:55 PM.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Run it in a debugger! I can't tell without the stack trace.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It might be related to the misuse of the printf() function. Your format specifier doesn't match the variable type. But as already said, your debugger will be able to tell you where it detects the problem.

    Jim

  9. #9
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Yarin View Post
    Run it in a debugger! I can't tell without the stack trace.
    I thought I did by pressing run. Let me look in to it.

    Quote Originally Posted by jimblumberg View Post
    It might be related to the misuse of the printf() function. Your format specifier doesn't match the variable type. But as already said, your debugger will be able to tell you where it detects the problem.
    Jim
    Well, as far as I can tell, there is no format specifier for unsigned long int. I'll convert it to cout and see how that goes

    Thank you.

  10. #10
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    It seems you were both right. Removing printf from the code it stopped raising the error and pressing run does not start the debugger. Thanks for the info.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well, as far as I can tell, there is no format specifier for unsigned long int.
    There sure is a format specifier that works with an unsigned long int, it is "%lu". You can also use the 'l' modifier with the 'o', 'x', or 'X' specifiers as well.


    Jim

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by taazz View Post
    It seems you were both right. Removing printf from the code it stopped raising the error and pressing run does not start the debugger. Thanks for the info.
    Apparently you're on a 64 bit machine and your system defines longs as 64 bits and ints as 32. I'm not sure why the printf would cause a crash, though. This program on linux doesn't crash. It just prints the low-order 32 bits of the 64 bit value, as expected on a little-endian machine.
    Code:
    #include <stdio.h>
    int main() {
        unsigned long x = 0x123456789ABCDEF0;
        printf("%x\n", x); // prints 9abcdef0
        return 0;
    }
    Your compiler should have warned you about the mismatch. Turn up your warning level.

    Also, your execution time is freakishly long. Almost a minute?!

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm not sure why the printf would cause a crash, though.
    Strange thing, undefined behavior, anything can happen from the program appearing to work correctly, to producing incorrect information, to trashing the system.

    Jim

  14. #14
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by jimblumberg View Post
    Strange thing, undefined behavior, anything can happen from the program appearing to work correctly, to producing incorrect information, to trashing the system.
    True. I still find it hard to imagine what actually happened. I'd like to see the assembly code that crashed.

    taazz never mentioned what OS, machine, or compiler he's using (he mentioned codeblocks, but not which compiler it's configured to use).

    I'd also like to know what happens if he runs the code in post #13.

  15. #15
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by algorism View Post
    Apparently you're on a 64 bit machine and your system defines longs as 64 bits and ints as 32. I'm not sure why the printf would cause a crash, though. This program on linux doesn't crash. It just prints the low-order 32 bits of the 64 bit value, as expected on a little-endian machine.
    Code:
    #include <stdio.h>
    int main() {
        unsigned long x = 0x123456789ABCDEF0;
        printf("%x\n", x); // prints 9abcdef0
        return 0;
    }
    Your compiler should have warned you about the mismatch. Turn up your warning level.

    Also, your execution time is freakishly long. Almost a minute?!
    Yes my system is 64 bit but the compiler is TDM GCC 32bit, the header of the readme.txt file in the installation directory says
    ________________________________________
    _/_ _\_
    __/__/ TDM-GCC Compiler Suite for Windows \__\__
    | « « | GCC 5 Series | » » |
    ――\――\ MinGW 32-bit Edition /――/――
    ―\― ―/―
    ――――――――――――――――――――――――――――――――――――――――
    I installed 32bit specifically to avoid problems with data sizes as I learn C/C++ and postpone the issue to a future date when I'll be more familiar with all the details.In my mind a DWord is a double word, it can only be interpreted as a 32 bit integer, signed or not, 64bit App or 32bit, makes no difference in the size. As far as the printf is concerned it should be only a matter of interpretation.
    Yes I did receive 3 warnings which I took a quick look and moved on to more "pressing" things that I have to learn.
    The 58 seconds execution time includes me, typing in my answer what I see on screen, before pressing the "close program" button to copy the results in the same post. I always do that to avoid "embarrassing" my self with not paying attention when its needed.

    Your code produces the results you predicted
    9abcdef0

    Process returned 0 (0x0) execution time : 0.020 s
    Press any key to continue.
    along with 2 warnings
    D:\C++\app2\main.cpp|3|warning: large integer implicitly truncated to unsigned type [-Woverflow]|
    D:\C++\app2\main.cpp|4|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'long unsigned int' [-Wformat=]|
    and this is the version information of the compiler
    C:\Users\taazz>g++ --version
    g++ (tdm-1) 5.1.0
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    C:\Users\taazz>gcc --version
    gcc (tdm-1) 5.1.0
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Popular pages Recent additions subscribe to a feed

Tags for this Thread