Sometimes when we double click on a USB drive in Windows File Explorer there is a message "There is no disc in the drive". I want to identify this issue in my application prior to reading any file on the disc.
How is it possible?
I am on Windows Platform and using Visual C++ for development.
If you know the drive letter, you can try the following:
HANDLE h = CreateFile("\\\\.\\E:", 0, 0, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND)
printf("The drive E: is not ready\n");
else
printf("Unknown error %lu\n", (int)err);
}
else
{
CloseHandle(h); /* don't forget to close the handle! */
printf("The drive E: is ready\n");
}
That is, open the drive without requesting read or write access. It should fail only if the drive is not ready. It works with a USB memory stick.