Can someone explain GetLogicalDrives?

I would like to do a program that copy to usb from pc. Here is the code:
1
2
std::ifstream  src("E:\\myprog.exe", std::ios::binary);
ofstream  dst("C:\\myprog.exe",std::ios::binary);

The problem is that i don't know if the drive letter is "E". I see to use GetLogicalDrives function, i do:
1
2
 int var=GetLogicalDrives();
    std::cout<<var; 
the output is 84 if usb is on else i get 20. Why? I see https://msdn.microsoft.com/it-it/library/windows/desktop/aa364972(v=vs.85).aspx but i'm confused about the output. Sorry but I'm not expert.


It's a bit pattern:

Drive letters:
GFEDCBA

For 20:
10100 -> Letters C, E exists

For 84:
1010100 -> Letters C, E, G exists
     G F E D C B A
84 = 1 0 1 0 1 0 0
20 =     1 0 1 0 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <windows.h>

// return root directory of the first removable drive
// return empty string if there is no mounted removable drive
std::string get_removable_drive_root_dir()
{
    char buffer[1024] {} ;
    const auto result = ::GetLogicalDriveStringsA( sizeof(buffer)-1, buffer ) ;

    const char* drive_root = buffer ;
    const char* end = buffer + result ;
    while( drive_root < end )
    {
        #ifndef NDEBUG
            std::cout << "found drive " << drive_root << '\n' ;
        #endif // NDEBUG

        if( ::GetDriveTypeA(drive_root) == DRIVE_REMOVABLE ) return drive_root ;

        while( *drive_root++ ) ; // skip to past the next null character
    }

    return {} ; // no mounted removable drives
}


Trivial usage example:
1
2
3
4
5
6
7
8
9
10
int main()
{
    const std::string root_dir = get_removable_drive_root_dir() ;

    if( !root_dir.empty() )
    {
        std::ofstream( root_dir + "myprog.exe", std::ios::binary ) <<
                     std::ifstream( "E:\\myprog.exe", std::ios::binary ).rdbuf() ;
    }
}
Last edited on
JLBorges thank you so much, it work. But now the problem is if the user have more usb connect which copy? I try to explain it better: The program copies the file "myprog" if it is present, else it creates a file "myprog" (for example if i have not connetc usb, the program creates "myprog" in destination) . So if i have 2 or more usb connect, the program may overwrite the real file with the file created. How can I to avoid it?
Last edited on
Make a list of the root directories all the removable drives that are present,
and then check in each one if a file with the expected path exists or not.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>

// return root directories of all removable drives
std::vector<std::string> get_removable_drive_root_dirs()
{
    std::vector<std::string> result ;

    char buffer[1024] {} ;
    const auto len = ::GetLogicalDriveStringsA( sizeof(buffer)-1, buffer ) ;

    const char* drive_root = buffer ;
    const char* end = buffer + len ;
    while( drive_root < end )
    {
        if( ::GetDriveTypeA(drive_root) == DRIVE_REMOVABLE )
            result.emplace_back(drive_root) ;

        while( *drive_root++ ) ; // skip to past the next null character
    }

    return result ;
}

bool exists( std::string path ) // return true if file exists
{ return std::ifstream(path).is_open() ; }

#include <iostream>

int main()
{
    const std::string relative_path_to_file = "myprog.exe" ;

    const std::vector<std::string> root_dirs = get_removable_drive_root_dirs() ;

    for( const std::string& dir : root_dirs )
    {
        if( exists( dir + relative_path_to_file ) )
        {
            // file exists in this removable drive
        }

        else
        {
            // file does not exist in this removable drive
        }
    }
 }
Thank you so much.
Topic archived. No new replies allowed.