List Windows Logical Drive Letters

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Ancient Dragon Ancient Dragon is offline Offline Nov 11th, 2009, 1:23 pm |
-7
This demonstrates how to get a list of your computer's logical drive letters under the MS-Windows operating system.
Quick reply to this message  
C Syntax
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. int main()
  6. {
  7. char buf[255];
  8. // get the drive letters as a set of strings
  9. int sz = GetLogicalDriveStrings(sizeof(buf), buf);
  10. if( sz > 0)
  11. {
  12. // buf now contains a list of all the drive letters. Each drive letter is
  13. // terminated with '\0' and the last one is terminated by two consecutive '\0' bytes.
  14. char* p1 = buf;
  15. char* p2;
  16. while( *p1 != '\0' && (p2 = strchr(p1,'\0')) != NULL )
  17. {
  18. printf("%s\n", p1);
  19. p1 = p2+1;
  20. }
  21. }
  22. else
  23. {
  24. // Oops! something went wrong so display the error message
  25. DWORD dwError = GetLastError();
  26. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwError, 0, buf, sizeof(buf), 0);
  27. printf("%s\n", buf);
  28.  
  29. }
  30. }
0
William Hemsworth William Hemsworth is offline Offline | Nov 11th, 2009
really nice snippet (: I'm sure this will come in handy for me sometime.
 
-2
marco93 marco93 is offline Offline | Nov 12th, 2009
See rather the Microsoft sample.
16 years old... (beginner code for a so simple api, posted 10000 times on BBS & Usenet...)
Last edited by marco93; Nov 12th, 2009 at 2:26 pm.
 
-7
Ancient Dragon Ancient Dragon is offline Offline | Nov 12th, 2009
>>See rather the Microsoft sample
What sample? Post link.

[edit] Probably this?

>>posted 10000 times on BBS & Usenet.
Probably -- now its been posted 10001 times
Last edited by Ancient Dragon; Nov 12th, 2009 at 2:51 pm.
 
 


Message:


Thread Tools Search this Thread



Tag cloud for getlasterror, getlogicaldrivestrin, logical_drives, strchr
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC