Thread: GetLogicalDriveStrings() problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    35

    GetLogicalDriveStrings() problem

    I have a simple program:
    Code:
    #include <windows.h> 
    #include <stdio.h> 
    
    void main() 
    { 
      char cmd[50]; 
      GetLogicalDriveStrings(50, cmd); 
      puts(cmd); 
    }
    When done, it outputs:
    A:\
    And that's all! But i have more logical drives!!!
    Where is the problem?
    Thx.
    im from LMoldovaZ

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    From GetLogicalDriveStrings:
    Quote Originally Posted by msdn,
    pBuffer
    [out] Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, plus with an additional null character. Each string is a device name.
    Also, main returns int.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    Ok. And if I want, for example, to show all of my logical drives, how can I do this? I mean that I don't want simply write smth like that:
    Code:
    for(int i=0; i<an_int_value; i++)
      printf("%c", cmd[i]);
    I need smth better
    im from LMoldovaZ

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    TCHAR buf[256], * pbuf = buf;
    DWORD i = 0;
    
    ::GetLogicalDriveStrings(sizeof buf, buf);
    
    while( *pbuf )
    {
            // deal wit it deal wit it
            pbuf += _tcslen( pbuf ) + 1;
    }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    Thx. Now it's clear. But new problem appeared.. After I obtain list of my drives, i try to obtain list of subdirectories. I use next code:
    Code:
    char cPath[MAX_PATH]; 
    printf("\nEnter the path  "); 
    gets(cPath); 
    hFile = FindFirstFile(cPath, &lpfd); 
    
    if(hFile == INVALID_HANDLE_VALUE) 
    { 
      printf("\"%s\" - No such directory\n", cPath); 
      main(); 
    } 
    .......
    And every time hFile == INVALID_HANDLE_VALUE, even if drive which I enter into cPath does exist.
    im from LMoldovaZ

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use fgets(cPath, sizeof (cPath), stdin)
    and remove '\n' from the string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    it steel doesn't work... But! Smth. interesting happened: even if I call FindFirstFile like this:
    Code:
    hFile = FindFirstFile("c:\\", &lpfd);
    It can't find that directory!
    im from LMoldovaZ

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    To find a diretory - you should past its name without \
    like "d:\\work"

    and don't call main function by yourself
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    ok. but if i want to see what contains my directory c:\ ?
    I try to find it even like that:
    Code:
    hFile = FindFirstFile("c:", &lpfd);
    And it steel doesn't work...
    im from LMoldovaZ

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to see what is in the c: use c:\\*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    Ohh! Thank so much! I forgot about asterisk (*)!! Finally it works! Thanks!
    im from LMoldovaZ

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    One correction:
    Code:
    void main()
    Should be:
    Code:
    int main()
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    and don't call main function by yourself
    why? if I wnat to use recursion from the main function?

    2 manutd:
    I have read the faq, reffered to type of main(), and i don't see a big difference between
    void main() and int main().
    Can you explain?
    im from LMoldovaZ

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > and i don't see a big difference between void main() and int main().
    > gets(cPath);
    Is that also your explanation for using gets() rather than fgets(), because you can't see the difference?

    > why?
    1. It's crummy to use a recursive call when all you wanted was a while loop.
    2. Calling main is actually illegal in C++, and is probably illegal in C99.
    3. If someone knows this about your code, they might just try a stack overflow attack to see what happens. But since you used gets(), why would they bother?

    But if you just want to go your own way with the "works for me" attitude, then this isn't the place for you. We'll stop talking it you stop listening.


    > if I wnat to use recursion from the main function?
    So do
    Code:
    int main ( int argc, char *argv[] ) { return mymain(argc,argv); }
    and recurse on mymain. It's no big hardship if you really want to do it.

    > Can you explain?
    What, didn't you read the FAQ?
    What part of the FAQ is giving you trouble with this concept?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    I don't want to "work for me". I just try to understand my own mystakes, and I think that this forum can help me. And, as you can see, I'm not stop to listen to you, I just give you some questions to find some answers.
    What part of the FAQ is giving you trouble with this concept?
    No troubles. I just read the FAQ, and didn't find big difference beetwen void main and int main, probably it's caused by my worst english...
    Anyway, thanks for your answer
    im from LMoldovaZ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM