views:

17

answers:

1

Hello,

I would like to know why when I try to create a handle to a USB flash drive, I receive a path not found error.

HANDLE aFile = CreateFile(_T("\\\\.\\F:\\"), GENERIC_READ, 0, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (aFile == INVALID_HANDLE_VALUE)
    {
        printf("\n");
        printf("Bad handle value. Error %d \n", GetLastError());
    }

From there I want to read a stream of 512 bytes (the boot sector) to a .bin file, but I can't seem to get past the handle creation first. Does Windows prevent applications from opening a handle to removable drives?

+1  A: 

That code has two problems. First, the path. You are actually specifying the root folder of the drive; what you really need is the volume. Remove the trailing backslash from the path; i.e. _T("\\\\.\\F:"). Secondly, you need to specify FILE_SHARE_READ | FILE_SHARE_WRITE; you are trying to open it in exclusive mode, and this will fail. See MSDN documentation for CreateFile for more information.

Luke
Excellent! Everything works now :)
ffrstar777