DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Removable drives (http://www.daniweb.com/forums/thread30040.html)

hkiani Aug 8th, 2005 5:22 am
Removable drives
 
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.

techniner Aug 8th, 2005 7:00 am
Re: Removable drives
 
#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);
       
}
                                       


Stoned_coder Aug 8th, 2005 9:26 am
Re: Removable drives
 
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().

techniner Aug 8th, 2005 11:42 am
Re: Removable drives
 
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..

hkiani Aug 9th, 2005 1:14 am
Re: Removable drives
 
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 >>


All times are GMT -4. The time now is 7:39 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC