Card Reader Detection in C++

Started by
6 comments, last by streamer 16 years, 10 months ago
Hello. How can I retrieve information about if card is inserted into card reader (same as floppy), and not to get the message box "Please insert disk in drive F:"? I'm using this:

   TCHAR buffer[256];
   unsigned long dwDrive = 1, dwDrives = GetLogicalDrives();
   char cLetter;
bool bFileFound = false;
    for(cLetter = 'A'; cLetter <= 'Z'; cLetter++){
        if(dwDrives & dwDrive) 
		{
			
			sprintf(buffer,"%c:/getthisfile.log", cLetter);
			if( (stream  = fopen( buffer, "r" )) != NULL )
			{
				bFileFound = true;
				fclose(stream);
				break;
			}
		}
		   dwDrive <<= 1;
	}




Thank you. [Edited by - streamer on June 4, 2007 9:22:24 AM]
Advertisement
You can't using C++ alone.

You'll need to use OS calls, if it is possible at all.

For floppy disks, applications uses a way that forced the drive read continously, even if no disk was inserted. This isn't the best method, since it forces the driver to do what it shouldn't, and could even damage it, so only a few very specific applications did that.

If card reader uses floppy disk interface via BIOS, this probably won't work, since the hardware is somewhat different.
Ohm Sorry.
I need not to read always. I need to check some files when application starts.
Just once.
You may be able to check for the file's existance using the stat() function before trying to open the file for reading. I don't know for sure if this will stop the OS from asking you to insert a floppy/card, but it might. The only other option will be using the Win32 API, perhaps a call to CreateFile() with some special flags.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Quote:Original post by joanusdmentia
You may be able to check for the file's existance using the stat() function before trying to open the file for reading. I don't know for sure if this will stop the OS from asking you to insert a floppy/card, but it might. The only other option will be using the Win32 API, perhaps a call to CreateFile() with some special flags.


Working! File writing if fails it means that there is no drive (or drive is write protected but that doesn't count [smile]).
Thank you.

int iRating;iRating++;
Ops. It works for floppies, CD-s, and Hard disks, but not for card readers.
I don't think it's possible to do without using platform specific code.

Here's one way of doing it in windows:
//	Get a bitmask for all valid drives.	DWORD d = GetLogicalDrives();	char rootPath[] = "@:\\";//	Loop through all drives	while (d > 0){	//	Test if drive is valid (bit set)		bool isValid = ((d & 1) != 0);	//	Adjust root path and get the next bit		++ rootPath[0];		d >>= 1;	//	If the drive isn't valid, continue with the next		if (!isValid)			continue;	//	We've got a valid drive, reject all non removable drives		if (GetDriveType(rootPath) != DRIVE_REMOVABLE)			continue;	//	Get the volume name of the inserted media, this will fail if no media is present		char volumeName[MAX_PATH + 1] = "";		if (GetVolumeInformation(rootPath, volumeName, sizeof(volumeName), NULL, NULL, NULL, NULL, 0) == FALSE)			continue;	//	Reject volumes without a name, maybe not needed		if (volumeName[0] == 0)			continue;	//	Found a valid removeable media, do something with it		printf("Found valid removable volume at %s named %s", rootPath, volumeName);	}


My 2c
There is strange thing happening. When I startup the windows my detection of card works perfectly. Then I insert one usb stick and when I remove it, whole code goes mad. Then every try to access card reader results in "please insert a disk int drive G or F".

This topic is closed to new replies.

Advertisement