GetLogicalDriveStrings() problem

This is a discussion on GetLogicalDriveStrings() problem within the Windows Programming forums, part of the Platform Specific Boards category; I have a simple program: Code: #include <windows.h> #include <stdio.h> void main() { char cmd[50]; GetLogicalDriveStrings(50, cmd); puts(cmd); } When ...

  1. #1
    LMZ
    LMZ is offline
    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
    LMZ
    LMZ is offline
    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;
    }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  5. #5
    LMZ
    LMZ is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    use fgets(cPath, sizeof (cPath), stdin)
    and remove '\n' from the string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    LMZ
    LMZ is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    To find a diretory - you should past its name without \
    like "d:\\work"

    and don't call main function by yourself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    LMZ
    LMZ is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    to see what is in the c: use c:\\*
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    LMZ
    LMZ is offline
    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
    LMZ
    LMZ is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    LMZ
    LMZ is offline
    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

Page 1 of 2 12 LastLast
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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21