Hi every one
Please help me:
How can i found, if there is any disk in a removable drive (floppy drive or cd drive).
I am programming with C++Builder. If there is any Win API function for this.
Thank you very much.

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <memory.h>
#include "fdc.h"

int main()
{

	int i, j, drive, head, track, start_sect, max_sect, type;
	int times;
	long size;
	int sector;
	char buf[BUFSIZ * 9 + 1];
	char buf1[BUFSIZ * 9 + 1];
	int fd;

	size = (long)(360l * 1024l);

	if((fd = mopen(size)) != -1) {
		fprintf(stderr, "Can't open the output file!\n");
		exit(1);
	}
	for(times = 0; times < 1; ++times) {
		if(!times) {
			printf("insert the first diskette into the drive & hit enter ");
			gets(buf);
		} else {
			lseek(fd, 0l, 0);
			printf("insert the second diskette into the drive & hit enter ");
			gets(buf);
		}
		drive = 0;				/* set drive to A */
		type = 2;				/* set 320/360K disk in 1.2 M drive */
		start_sect = 1;
		max_sect = 9;
		if(setdasd(drive, type)) {
			fprintf(stderr, "Can't set the dasd for drive A!\n");
			close(fd);
			exit(1);
		}
		
		for(track = 0; track < 40; track++) 
			for(head = 0; head < 2; head++)
				for(sector = 1; sector < 10; sector++)
				for(i = 0; i < 3; i++) {
					if(rdtrack(drive, head, track, sector, 1, buf) == 0) {
						if(!times) {
							write(fd, buf, BUFSIZ);
						} else {
							read(fd, buf1, BUFSIZ);
							for(j = 0; j < BUFSIZ; j++)
								if(buf1[i] != buf[i]) {
									printf("difference at head %2d, track %2d, byte %5d \
%x %x\n", head, track, i, buf1[i], buf[i]);
								}
						}
					} else {
						fprintf(stderr, "failure %d on head %d, track %d, sector %d\n", i, head,
						track, sector);
					}
				}
	}
	close(fd);
	printf("done\n");
	exit(0);
	
}

Nice one. The OP asks for windows API and you give him bad C using non-standard headers.
Look up GetDriveType() and GetVolumeInformation() and SetErrorMode() and GetLastError().
Alternatively use DeviceIoControl().

HAHAHAHAHA

Ok So I did not have my first cup of Coffee... I saw C++ and just went to work...

smmmmacccckkk...

Sorry for wrong info.. was just trying to help..

Thank you Stoned_Coder very much.

Your Guides were very useful for me. My problem was solved. My main problem was this: The WinXP shows a message "No disk in CD-ROM ..." each time i reference a file in that drive. The SetErrorMode(SEM_FAILCRITICALERRORS) solved my problem.
I wrote (in C++Builder) the bellow function (Of cource WIN API have functions for this purpose but using them was very difficult and specific to each version of Windows) and tested it. It works.

Please help me if this function has any problem.

bool IsDriveReady(AnsiString tasDrive)
{
   if(tasDrive.IsEmpty())
      tasDrive = GetCurrentDir();
   tasDrive = tasDrive.UpperCase();
   int liDrive = tasDrive[1] - 'A' + 1;
   UINT luiOldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
   bool lbReady = (DiskFree(liDrive) >= 0);
   SetErrorMode(luiOldErrorMode);
   return lbReady;
}

<< moderator edit: added [code][/code] tags >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.