Removable drives

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 3
Reputation: hkiani is an unknown quantity at this point 
Solved Threads: 0
hkiani hkiani is offline Offline
Newbie Poster

Removable drives

 
0
  #1
Aug 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Removable drives

 
0
  #2
Aug 8th, 2005
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <memory.h>
  5. #include "fdc.h"
  6.  
  7. int main()
  8. {
  9.  
  10. int i, j, drive, head, track, start_sect, max_sect, type;
  11. int times;
  12. long size;
  13. int sector;
  14. char buf[BUFSIZ * 9 + 1];
  15. char buf1[BUFSIZ * 9 + 1];
  16. int fd;
  17.  
  18. size = (long)(360l * 1024l);
  19.  
  20. if((fd = mopen(size)) != -1) {
  21. fprintf(stderr, "Can't open the output file!\n");
  22. exit(1);
  23. }
  24. for(times = 0; times < 1; ++times) {
  25. if(!times) {
  26. printf("insert the first diskette into the drive & hit enter ");
  27. gets(buf);
  28. } else {
  29. lseek(fd, 0l, 0);
  30. printf("insert the second diskette into the drive & hit enter ");
  31. gets(buf);
  32. }
  33. drive = 0; /* set drive to A */
  34. type = 2; /* set 320/360K disk in 1.2 M drive */
  35. start_sect = 1;
  36. max_sect = 9;
  37. if(setdasd(drive, type)) {
  38. fprintf(stderr, "Can't set the dasd for drive A!\n");
  39. close(fd);
  40. exit(1);
  41. }
  42.  
  43. for(track = 0; track < 40; track++)
  44. for(head = 0; head < 2; head++)
  45. for(sector = 1; sector < 10; sector++)
  46. for(i = 0; i < 3; i++) {
  47. if(rdtrack(drive, head, track, sector, 1, buf) == 0) {
  48. if(!times) {
  49. write(fd, buf, BUFSIZ);
  50. } else {
  51. read(fd, buf1, BUFSIZ);
  52. for(j = 0; j < BUFSIZ; j++)
  53. if(buf1[i] != buf[i]) {
  54. printf("difference at head %2d, track %2d, byte %5d \
  55. %x %x\n", head, track, i, buf1[i], buf[i]);
  56. }
  57. }
  58. } else {
  59. fprintf(stderr, "failure %d on head %d, track %d, sector %d\n", i, head,
  60. track, sector);
  61. }
  62. }
  63. }
  64. close(fd);
  65. printf("done\n");
  66. exit(0);
  67.  
  68. }
  69.  
  70. 
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Removable drives

 
0
  #3
Aug 8th, 2005
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().
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Removable drives

 
0
  #4
Aug 8th, 2005
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..
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 3
Reputation: hkiani is an unknown quantity at this point 
Solved Threads: 0
hkiani hkiani is offline Offline
Newbie Poster

Re: Removable drives

 
0
  #5
Aug 9th, 2005
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.

  1. bool IsDriveReady(AnsiString tasDrive)
  2. {
  3. if(tasDrive.IsEmpty())
  4. tasDrive = GetCurrentDir();
  5. tasDrive = tasDrive.UpperCase();
  6. int liDrive = tasDrive[1] - 'A' + 1;
  7. UINT luiOldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  8. bool lbReady = (DiskFree(liDrive) >= 0);
  9. SetErrorMode(luiOldErrorMode);
  10. return lbReady;
  11. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC