Trying to eject a USB device - Windows

Hello,

Windows 10 32 bit
Qt 5.7.0
gcc v 5.3.0

I'm trying to eject a USB device. I know its drive letter and drive number.

I am trying to use this routine:-
https://support.microsoft.com/en-us/kb/165721
I have a compile error with one of the functions.
The error is with the 'wsprintf' lines.
I don't know enough 'C' or 'C++' to be able to correct.
Perhaps some one can offer advice?
Or an alternative solution.

Regards
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
HANDLE OpenVolume(TCHAR cDriveLetter)
{
    HANDLE hVolume;
    UINT uDriveType;
    TCHAR szVolumeName[8];
    TCHAR szRootName[5];
    DWORD dwAccessFlags;

    wsprintf(szRootName, szRootFormat, cDriveLetter); //*** error here

    uDriveType = GetDriveType(szRootName);
    switch(uDriveType) {
    case DRIVE_REMOVABLE:
        dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
        break;
    case DRIVE_CDROM:
        dwAccessFlags = GENERIC_READ;
        break;
    default:
        _tprintf(TEXT("Cannot eject.  Drive type is incorrect.\n"));
        return INVALID_HANDLE_VALUE;
    }

    wsprintf(szVolumeName, szVolumeFormat, cDriveLetter); //*** error here

    hVolume = CreateFile(   szVolumeName,
                            dwAccessFlags,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            OPEN_EXISTING,
                            0,
                            NULL );
    if (hVolume == INVALID_HANDLE_VALUE)
        ReportError(TEXT("CreateFile"));

    return hVolume;
}
1
2
3
error: 'wsprintf_instead_use_StringCbPrintf_or_StringCchPrintf' was not declared in this scope
     wsprintf(szRootName, szRootFormat, cDriveLetter);
     ^
Last edited on
To use wsprintf you need to include <windows.h>
Thanks for your reply.
I've got the following includes:-

1
2
3
4
#include <windows.h>
#include <winioctl.h>
#include <tchar.h>
#include <stdio.h> 
OpenVolume (narrow character version GetDriveTypeA, CreateFileA):

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
#include <windows.h>
#include <string>
#include <iostream>

HANDLE OpenVolume( char cDriveLetter )
{
    const std::string szRootName = cDriveLetter + std::string( ":\\" ) ;
    const UINT uDriveType = GetDriveTypeA( szRootName.c_str() );

    DWORD dwAccessFlags;
    switch( uDriveType )
    {
        case DRIVE_REMOVABLE:
            dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
            break;
        case DRIVE_CDROM:
            dwAccessFlags = GENERIC_READ;
            break;
        default:
            std::cerr << "Cannot eject.  Drive type is incorrect.\n" ;
            return INVALID_HANDLE_VALUE;
    }

    const std::string szVolumeName = std::string( "\\\\.\\" ) + cDriveLetter + ':' ;
    
    const HANDLE hVolume = CreateFileA( szVolumeName.c_str(),
                                        dwAccessFlags,
                                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                                        nullptr,
                                        OPEN_EXISTING,
                                        0,
                                        nullptr );

    if( hVolume == INVALID_HANDLE_VALUE ) std::cerr << "CreateFile *** error: " << GetLastError() << '\n' ;

    return hVolume;
}

http://rextester.com/EILG35799
Hello JLBorges,

Thanks for the link.
Works fine.
Just changed the 'dwAccessFlags' a little, I already know the device type.

Regards

1
2
3
4
5
6
7
8
9
10
11
12
13
    DWORD dwAccessFlags;
    switch( uDriveType )
    {
        case DRIVE_REMOVABLE:
            dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
            break;
        case DRIVE_CDROM:
            dwAccessFlags = GENERIC_READ;
            break;
        default:
            std::cerr << "Cannot eject.  Drive type is incorrect.\n";
            return INVALID_HANDLE_VALUE;
    }
Topic archived. No new replies allowed.