1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812:
| //***************************************************************************** //* //* //* DiskIO.cpp //* //* //***************************************************************************** #include #include "DiskIO.h" #define VWIN32_DIOC_DOS_IOCTL 1 #define VWIN32_DIOC_DOS_DRIVEINFO 6 #define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM #define IOCTL_CDROM_GET_DRIVE_GEOMETRY CTL_CODE(IOCTL_CDROM_BASE, 0x0013, METHOD_BUFFERED, FILE_READ_ACCESS) static char iSectors[]= { 0, // Unknown Format is unknown 15, // F5_1Pt2_512 5.25" 1.2MB 512 bytes/sector 18, // F3_1Pt44_512 3.5" 1.44MB 512 bytes/sector 36, // F3_2Pt88_512 3.5" 2.88MB 512 bytes/sector 0, // F3_20Pt8_512 3.5" 20.8MB 512 bytes/sector 9, // F3_720_512 3.5" 720KB 512 bytes/sector 9, // F5_360_512 5.25" 360KB 512 bytes/sector 8, // F5_320_512 5.25" 320KB 512 bytes/sector }; static char iTracks[]= { 80, // Unknown Format is unknown 80, // F5_1Pt2_512 5.25" 1.2MB 512 bytes/sector 80, // F3_1Pt44_512 3.5" 1.44MB 512 bytes/sector 80, // F3_2Pt88_512 3.5" 2.88MB 512 bytes/sector 0, // F3_20Pt8_512 3.5" 20.8MB 512 bytes/sector 80, // F3_720_512 3.5" 720KB 512 bytes/sector 40, // F5_360_512 5.25" 360KB 512 bytes/sector 40, // F5_320_512 5.25" 320KB 512 bytes/sector }; #pragma pack(1) typedef struct _ExtGetDskFreSpcStruc { WORD wSize; WORD wLevel; DWORD dwSectorsPerCluster; DWORD dwBytessPerSector; DWORD dwAvailableClusters; DWORD dwTotalClusters; DWORD dwAvailablePhysSectors; DWORD dwTotalPhysSectors; DWORD dwAvailableAllocationUnits; DWORD dwTotalAllocationUnits; DWORD dwRsvd[2]; }ExtGetDskFreSpcStruc; typedef struct { DWORD dwStartingSector ; WORD wNumberOfSectors ; LPVOID pBuffer; }CONTROL_BLOCK; typedef struct _DIOC_REGISTERS { DWORD reg_EBX; DWORD reg_EDX; DWORD reg_ECX; DWORD reg_EAX; DWORD reg_EDI; DWORD reg_ESI; DWORD reg_Flags; }DIOC_REGISTERS; typedef struct _FVBLOCK { BYTE fvSpecFunc; // special functions = 0 WORD fvHead; // head to format/verify WORD fvCylinder; // cylinder to format/verify WORD fvTracks; // tracks to format/verify }FVBLOCK; #pragma pack() static HANDLE hHandles[64]; //***************************************************************************** //* //* IsWinNt //* //***************************************************************************** inline BOOL IsWinNt() { DWORD dwOsVersion=0; dwOsVersion=GetVersion(); if((dwOsVersion & 0xFF00)==0x0100 && (dwOsVersion & 0x00FF)<=0x0004) { return FALSE; } return TRUE; } //***************************************************************************** //* //* ReadSector //* //***************************************************************************** // Auslesen eines oder mehrere logischer Sektoren (immer 512 Byte groß !!!) // iDrive : Laufwerksnummer (A=0 B=1 ... 0x8000=Pysical_0 0x8001=Pysical_1 ... ) // dwStart : Nummer des ersten Sektors // dwCount : Ist die Anzahl der Sektoren (1 bis 0xFFFF) // pBuffer : Zeiger auf den Puffer in dem die Sektoren geschrieben werden. // Ergibt TRUE wenn die Sektoren gelesen wurden BOOL ReadSector(int iDrive, DWORD dwStart, DWORD dwCount,void *pBuffer) { HANDLE hDevice; BOOL bResult; DWORD dwBytes,dwPos; BOOL bPysical=FALSE; if(iDrive&0x8000) { bPysical=TRUE; iDrive&=~0x8000; } if(iDrive <0 || iDrive> 26)return 0; if(IsWinNt()) // WinNT Code { char cDeviceName[32]; LONG lHigh,lSave; if(bPysical) { strcpy(cDeviceName,"\\\\.\\PhysicalDrive0"); itoa(iDrive,cDeviceName+17,10); } else{ strcpy(cDeviceName,"\\\\.\\A:"); cDeviceName[4] += iDrive; } hDevice = CreateFile(cDeviceName,GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(hDevice == INVALID_HANDLE_VALUE)return FALSE; lHigh=dwStart>>(32-9); lSave=lHigh; dwStart*=512; dwCount*=512; // Setting the pointer to point to the start of the sector we want to read .. dwPos=SetFilePointer(hDevice, dwStart, &lHigh, FILE_BEGIN); if(dwPos!=dwStart || lSave!=lHigh) { CloseHandle(hDevice); return FALSE; } bResult=ReadFile(hDevice,pBuffer,dwCount,&dwBytes,NULL); if(!bResult || dwCount!=dwBytes) { CloseHandle(hDevice); return FALSE; } } else{ // Win95/98 Code DIOC_REGISTERS sReg; CONTROL_BLOCK sBlock; if(bPysical)return FALSE; hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL); sBlock.dwStartingSector = dwStart; sBlock.wNumberOfSectors = (WORD)dwCount; sBlock.pBuffer = pBuffer; //----------------------------------------------------------- // SI contains read/write mode flags // SI=0h for read and SI=1h for write // CX must be equal to ffffh for // int 21h's 7305h extention // DS:BX -> base addr of the // control block structure // DL must contain the iDrive number // (01h=A:, 02h=B: etc) //----------------------------------------------------------- sReg.reg_ESI = 0x00; sReg.reg_ECX = -1; sReg.reg_EBX = (DWORD)(&sBlock); sReg.reg_EDX = iDrive+1; sReg.reg_EAX = 0x7305; bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_DRIVEINFO,&(sReg),sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0); if(!bResult || (sReg.reg_Flags & 0x0001)) { CloseHandle(hDevice); return FALSE; } } CloseHandle(hDevice); return TRUE; } //***************************************************************************** //* //* ReadSector //* //***************************************************************************** // Auslesen eines oder mehrere logischer Sektoren (immer 512 Byte groß !!!) // hDrive : Ist das Handle des Laufwerkes // dwStart : Nummer des ersten Sektors // dwCount : Ist die Anzahl der Sektoren (1 bis 0xFFFF) // pBuffer : Zeiger auf den Puffer in dem die Sektoren geschrieben werden. // Ergibt TRUE wenn die Sektoren gelesen wurden BOOL ReadSector(void *hDrive, DWORD dwStart,DWORD dwCount,void *pBuffer) { BOOL bResult; DWORD dwBytes; HANDLE hDevice; int iCount=0; unsigned uNr; uNr=(int)hDrive-1; if(uNr>=64)return FALSE; hDevice=hHandles[(int)hDrive-1]; if(IsWinNt()) { // WinNT Code LONG lHigh; lHigh=dwStart>>(32-9); dwStart*=512; dwCount*=512; for(iCount=0;iCount<5;iCount++) { // Setting the pointer to point to the start of the sector we want to read .. if(SetFilePointer(hDevice, dwStart, &lHigh, FILE_BEGIN)==0xFFFFFFFF) if(GetLastError()) { return FALSE; } bResult=ReadFile(hDevice,pBuffer,dwCount,&dwBytes,NULL); if( bResult && dwCount==dwBytes)break; } if(iCount>=5)return FALSE; } else{ // Win95/98 Code DIOC_REGISTERS sReg; CONTROL_BLOCK sBlock; sBlock.dwStartingSector = dwStart; sBlock.wNumberOfSectors = (WORD)dwCount; sBlock.pBuffer = pBuffer; //----------------------------------------------------------- // SI contains read/write mode flags // SI=0h for read and SI=1h for write // CX must be equal to ffffh for // int 21h's 7305h extention // DS:BX -> base addr of the // control block structure // DL must contain the iDrive number // (01h=A:, 02h=B: etc) //----------------------------------------------------------- sReg.reg_ESI = 0x00; sReg.reg_ECX = -1; sReg.reg_EBX = (DWORD)(&sBlock); sReg.reg_EDX = (DWORD)hDrive; sReg.reg_EAX = 0x7305; for(iCount=0;iCount<5;iCount++) { bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_DRIVEINFO,&(sReg),sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0); if( bResult && !(sReg.reg_Flags & 0x0001))break; } if(iCount>=5)return FALSE; } return TRUE; } //***************************************************************************** //* //* FlushSector //* //***************************************************************************** // Schreibt alle Buffer auf die Disk zurück // hDrive : Ist das Handle des Laufwerkes // Ergibt TRUE wenn die Sektoren gespeichert wurden BOOL FlushSector(void *hDrive) { HANDLE hDevice; unsigned uNr; uNr=(int)hDrive-1; if(uNr>=64)return FALSE; hDevice=hHandles[(int)hDrive-1]; if(IsWinNt()) { return FlushFileBuffers(hDevice); } return FALSE; } //***************************************************************************** //* //* WriteSector //* //***************************************************************************** // Schreiben eines oder mehrere logischer Sektoren (immer 512 Byte groß !!!) // iDrive : Laufwerksnummer (A=0 B=1 ... 0x8000=Pysical_0 0x8001=Pysical_1 ... ) // dwStart : Nummer des ersten Sektors // dwCount : Ist die Anzahl der Sektoren (1 bis 0xFFFF) // pBuffer : Zeiger auf den Puffer in dem die Sektoren Daten stehen werden. // Ergibt TRUE wenn die Sektoren geschrieben wurden BOOL WriteSector(int iDrive, DWORD dwStart, DWORD dwCount,void *pBuffer) { HANDLE hDevice; BOOL bResult; DWORD dwBytes; BOOL bPysical=FALSE; if(iDrive&0x8000) { bPysical=TRUE; iDrive&=~0x8000; } if(iDrive <0 || iDrive> 26)return 0; if(IsWinNt()) { // WinNT Code char cDeviceName[32]; LONG lHigh; if(bPysical) { strcpy(cDeviceName,"\\\\.\\PhysicalDrive0"); itoa(iDrive,cDeviceName+17,10); } else{ strcpy(cDeviceName,"\\\\.\\A:"); cDeviceName[4] += iDrive; } hDevice = CreateFile(cDeviceName,GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(hDevice == INVALID_HANDLE_VALUE)return FALSE; lHigh=dwStart>>(32-9); dwStart*=512; dwCount*=512; // Setting the pointer to point to the start of the sector we want to read .. if(SetFilePointer(hDevice, dwStart, &lHigh, FILE_BEGIN)==0xFFFFFFFF) if(GetLastError()) { CloseHandle(hDevice); return FALSE; } bResult=WriteFile(hDevice,pBuffer,dwCount,&dwBytes,NULL); if(!bResult || dwCount!=dwBytes) { CloseHandle(hDevice); return FALSE; } } else{ // Win95/98 Code DIOC_REGISTERS sReg; CONTROL_BLOCK sBlock; if(bPysical)return FALSE; hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL); sBlock.dwStartingSector = dwStart; sBlock.wNumberOfSectors = (WORD)dwCount; sBlock.pBuffer = pBuffer; //----------------------------------------------------------- // SI contains read/write mode flags // SI=0h for read and SI=1h for write // CX must be equal to ffffh for // int 21h's 7305h extention // DS:BX -> base addr of the // control block structure // DL must contain the iDrive number // (01h=A:, 02h=B: etc) //----------------------------------------------------------- memset(&sReg,0,sizeof(sReg)); sReg.reg_ESI = 0x01; sReg.reg_ECX = -1; sReg.reg_EBX = (DWORD)(&sBlock); sReg.reg_EDX = iDrive+1; sReg.reg_EAX = 0x7305; bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_DRIVEINFO,&(sReg),sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0); if(!bResult || (sReg.reg_Flags & 0x0001)) { CloseHandle(hDevice); return FALSE; } } CloseHandle(hDevice); return TRUE; } //***************************************************************************** //* //* WriteSector //* //***************************************************************************** // Schreiben eines oder mehrere logischer Sektoren (immer 512 Byte groß !!!) // hDrive : Ist das Handle des Laufwerkes // dwStart : Nummer des ersten Sektors // dwCount : Ist die Anzahl der Sektoren (1 bis 0xFFFF) // pBuffer : Zeiger auf den Puffer in dem die Sektoren Daten stehen werden. // Ergibt TRUE wenn die Sektoren geschrieben wurden BOOL WriteSector(void *hDrive,DWORD dwStart,DWORD dwCount,void *pBuffer) { HANDLE hDevice; BOOL bResult; DWORD dwBytes; int iCount; unsigned uNr; uNr=(int)hDrive-1; if(uNr>=64)return FALSE; hDevice=hHandles[uNr]; if(IsWinNt()) { // WinNT Code LONG lHigh; lHigh=dwStart>>(32-9); dwStart*=512; dwCount*=512; for(iCount=0;iCount<5;iCount++) { // Setting the pointer to point to the start of the sector we want to read .. if(SetFilePointer(hDevice, dwStart, &lHigh, FILE_BEGIN)==0xFFFFFFFF) if(GetLastError()) { return FALSE; } bResult=WriteFile(hDevice,pBuffer,dwCount,&dwBytes,NULL); if( bResult && dwCount==dwBytes)break; } if(iCount>=5) { return FALSE; } } else{ // Win95/98 Code DIOC_REGISTERS sReg; CONTROL_BLOCK sBlock; sBlock.dwStartingSector = dwStart; sBlock.wNumberOfSectors = (WORD)dwCount; sBlock.pBuffer = pBuffer; //----------------------------------------------------------- // SI contains read/write mode flags // SI=0h for read and SI=1h for write // CX must be equal to ffffh for // int 21h's 7305h extention // DS:BX -> base addr of the // control block structure // DL must contain the iDrive number // (01h=A:, 02h=B: etc) //----------------------------------------------------------- memset(&sReg,0,sizeof(sReg)); sReg.reg_ESI = 0x01; sReg.reg_ECX = -1; sReg.reg_EBX = (DWORD)(&sBlock); sReg.reg_EDX = (DWORD)hDrive; sReg.reg_EAX = 0x7305; for(iCount=0;iCount<5;iCount++) { bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_DRIVEINFO,&(sReg),sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0); if( bResult && !(sReg.reg_Flags & 0x0001))break; } if(iCount>=5) { return FALSE; } } return TRUE; } //***************************************************************************** //* //* FormatDisk //* //***************************************************************************** // Formatiert eine Diskette // iDrive : Lafwerksnummer (0=A 1=B 2=C ...) // iType : Art der Diskkete (z.B. F3_1Pt44_512 F3_720_512) // Ergibt TRUE wenn die Diskette formatiert wurde. BOOL FormatDisk(int iDrive,MEDIA_TYPE iType) { HANDLE hDevice; BOOL bResult; DIOC_REGISTERS sReg; DWORD dwBytes; char cDeviceName[] = "\\\\.\\A:"; if(iType<0 || iType>=sizeof(iSectors)/sizeof(iSectors[0]))return FALSE; if(!iSectors[iType])return FALSE; cDeviceName[4] += iDrive; if(GetDriveType(cDeviceName+4)!=DRIVE_REMOVABLE)return FALSE; if(IsWinNt()) { // WinNT code HANDLE hDevice; DWORD dwBytes; FORMAT_PARAMETERS sFormat; // Creating a handle to iDrive a: using CreateFile () function .. hDevice = CreateFile(cDeviceName,GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if( hDevice == INVALID_HANDLE_VALUE)return FALSE; sFormat.StartCylinderNumber = 0; sFormat.StartHeadNumber = 0; sFormat.EndCylinderNumber = iTracks[iType]-1; sFormat.EndHeadNumber = 1; sFormat.MediaType = iType; if(!DeviceIoControl(hDevice,IOCTL_DISK_FORMAT_TRACKS,&sFormat,sizeof(FORMAT_PARAMETERS),NULL,0,&dwBytes,NULL)) { CloseHandle(hDevice); return FALSE; } } else{ FVBLOCK sBlock; int iHead,iTrack; hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL); memset(&sReg,0,sizeof(sReg)); for(iHead=0;iHead<2;iHead++) for(iTrack=0;iTrack<iTracks[iType];iTrack++) { sBlock.fvSpecFunc = 0; // special functions = 0 sBlock.fvHead = iHead; // head to format/verify sBlock.fvCylinder = iTrack; // cylinder to format/verify sBlock.fvTracks = 0; // tracks to format/verify sReg.reg_EAX = 0x440D; // IOCTL for block devices sReg.reg_EBX = iDrive+1; // zero-based iDrive ID sReg.reg_ECX = 0x0842; // Get Media ID command 0x??42 sReg.reg_EDX = (DWORD)&sBlock; // receives media ID info sReg.reg_Flags=0x0001; // 6 == VWIN32_DIOC_DOS_DRIVEINFO bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_IOCTL,&sReg,sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0); if(!bResult || (sReg.reg_Flags & 0x0001)) { int x=0; CloseHandle(hDevice); return FALSE; } } } CloseHandle(hDevice); return TRUE; } //***************************************************************************** //* //* GetPhysicalCount //* //***************************************************************************** // Ergibt die Größe eines Laufwerks in 512 Byte Sektoren // ist pSize ungleich Null so wird dort die wirkliche Sektorgröße gespeichert // Ergibt 0 bei einem Fehler static DWORD GetPhysicalCount(int iDrive, DWORD *pSize) { HANDLE hDevice; DWORD dwMax; if(pSize)*pSize=0; if(IsWinNt()) { // WinNT Code char cDeviceName[32] = "\\\\.\\PhysicalDrive0"; DISK_GEOMETRY sDisc; DWORD dwBytes; // Creating a handle to iDrive a: using CreateFile () function .. itoa(iDrive,cDeviceName+17,10); hDevice = CreateFile(cDeviceName,GENERIC_READ,FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_FLAG_WRITE_THROUGH, NULL); if(hDevice == INVALID_HANDLE_VALUE)return 0; if(!DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &sDisc, sizeof(sDisc), &dwBytes, NULL)) { CloseHandle(hDevice); return 0; } dwMax = sDisc.Cylinders.LowPart; dwMax *= sDisc.SectorsPerTrack; dwMax *= sDisc.TracksPerCylinder; dwMax *=(sDisc.BytesPerSector)>>9; if(pSize)*pSize=sDisc.BytesPerSector; } else{ // Win95/98 Code return 0; } CloseHandle(hDevice); return dwMax; } //***************************************************************************** //* //* GetSectorCount //* //***************************************************************************** // Ergibt die Größe eines Laufwerks in 512 Byte Sektoren // iDrive : Laufwerksnummer (A=0 B=1 ... 0x8000=Pysical_0 0x8001=Pysical_1 ... ) // pSize : Wenn ungleich Null, dann wird dort die wirkliche // Sektorgröße gespeichert. // Ergibt 0 bei einem Fehler DWORD GetSectorCount(int iDrive, DWORD *pSize) { ULARGE_INTEGER uDummy,uTotal; HANDLE hDevice; DWORD dwBytes,dwReturn; char cDeviceName[] = "\\\\.\\A:"; if(iDrive&0x8000) { return GetPhysicalCount(iDrive&~0x8000,pSize); } if(pSize)*pSize=0; cDeviceName[4] += iDrive; if(IsWinNt()) { // WinNT Code PARTITION_INFORMATION sInfo; DISK_GEOMETRY sDisk; BOOL bDisk; hDevice = CreateFile(cDeviceName,GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(hDevice == INVALID_HANDLE_VALUE)return 0; bDisk=DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&sDisk,sizeof(sDisk),&dwBytes,NULL); if(DeviceIoControl(hDevice,IOCTL_DISK_GET_PARTITION_INFO,NULL,0,&sInfo,sizeof(sInfo),&dwBytes,NULL)) { CloseHandle(hDevice); if(pSize)*pSize=(bDisk)? sDisk.BytesPerSector:512; return (DWORD)(sInfo.PartitionLength.QuadPart>>9); } if(!bDisk) { bDisk=DeviceIoControl(hDevice,IOCTL_CDROM_GET_DRIVE_GEOMETRY,NULL,0,&sDisk,sizeof(sDisk),&dwBytes, NULL); } if(bDisk) { CloseHandle(hDevice); if(pSize)*pSize=sDisk.BytesPerSector; if(GetDiskFreeSpaceEx(cDeviceName+4,&uDummy,&uTotal,NULL)) { return (DWORD)(uTotal.QuadPart>>9); } return (DWORD)((sDisk.Cylinders.QuadPart* (__int64)sDisk.TracksPerCylinder* (__int64)sDisk.SectorsPerTrack* (__int64)sDisk.BytesPerSector)>>9); } CloseHandle(hDevice); if(GetDiskFreeSpaceEx(cDeviceName+4,&uDummy,&uTotal,NULL)) { if(pSize)*pSize=512; return (DWORD)(uTotal.QuadPart>>9); } if(pSize)*pSize=0; return FALSE; } else{ // Win95/98 Code BYTE bBoot[512]; if(GetDriveType(cDeviceName+4)==DRIVE_CDROM) { if(!GetDiskFreeSpaceEx(cDeviceName+4,&uDummy,&uTotal,NULL))return 0; if(pSize)*pSize=2048; return (DWORD)(uTotal.QuadPart>>9); } if(!ReadSector(iDrive,0,1,bBoot))return 0; // Read Bootsector if(bBoot[510]!=0x55)return 0; if(bBoot[511]!=0xAA)return 0; if(!memcmp(bBoot+0x52,"FAT32 ",8)) { dwReturn=*(DWORD*)(bBoot+0x20); } else if(!memcmp(bBoot+0x36,"FAT16 ",8)) { dwReturn=*(WORD*)(bBoot+0x13); if(dwReturn==0)dwReturn=*(DWORD*)(bBoot+0x20); } else if(!memcmp(bBoot+0x36,"FAT12 ",8)) { dwReturn=*(WORD*)(bBoot+0x13); if(dwReturn==0)dwReturn=*(DWORD*)(bBoot+0x20); } else{ return 0; } if(dwReturn==0)return 0; if(pSize)*pSize=*(WORD*)(bBoot+0x0B); return dwReturn; } CloseHandle(hDevice); return 0; } //***************************************************************************** //* //* HasAccess //* //***************************************************************************** // Ergibt ob man Zugriff hat auf ein Laufwerk // iDrive : Laufwerksnummer (A=0 B=1 ... 0x8000=Pysical_0 0x8001=Pysical_1 ... ) // bWrite : Einen Schreibzugriff prüfen // Ergibt 0 = Kein Zugriff // 1 = Zugriff aber keine Disk ist im Laufwerk // 2 = Zugriff mit Disk int HasAccess(int iDrive,BOOL bWrite) { BOOL bOk; void *hDevice; char cBuffer[64],cFsName[64]; DWORD dwMaxChars,dwSerialNbr,dwFlags; char cName[]="A:\\"; hDevice=OpenDevice(iDrive,(bWrite)? 2:1); if(!hDevice)return 0; CloseDevice(hDevice); if(iDrive&0x8000)return 2; cName[0]+=iDrive; bOk=GetVolumeInformation(cName,cBuffer,sizeof(cBuffer),&dwSerialNbr,&dwMaxChars,&dwFlags,cFsName,sizeof(cFsName)); if(bOk==0 && GetLastError()==ERROR_NOT_READY)return 1; return 2; } //***************************************************************************** //* //* CloseDevice //* //***************************************************************************** // Schliest ein Laufwerks Device BOOL CloseDevice(void *hDrive) { HANDLE hDevice; DWORD dwBytes; unsigned uNr; uNr=(int)hDrive-1; if(uNr>=64)return FALSE; if(!hDrive)return FALSE; hDevice=hHandles[(int)hDrive-1]; DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME ,NULL,0,NULL,0,&dwBytes,0); DeviceIoControl(hDevice,FSCTL_DISMOUNT_VOLUME,NULL,0,NULL,0,&dwBytes,0); return CloseHandle(hDevice); } //***************************************************************************** //* //* OpenDevice //* //***************************************************************************** // Öffnet ein Laufwerks Device // iDrive : Laufwerksnummer (A=0 B=1 ... 0x8000=Pysical_0 0x8001=Pysical_1 ... ) // iAccess : Zugriffsart (0=Keinen 1=Read 2=Write&Read) // bLock : Das Laufwerk sperren // Ergibt 0 bei einem Fehler void *OpenDevice(int iDrive,int iAccess,BOOL bLock) { HANDLE hDevice; DWORD dwBytes; static const int iMode[3]={GENERIC_ALL,GENERIC_READ,GENERIC_WRITE|GENERIC_READ}; BOOL bPysical=FALSE; if(iDrive&0x8000) { bPysical=TRUE; iDrive&=~0x8000; } if(iDrive <0 || iDrive> 26)return 0; if(iAccess<0 || iAccess>=3)return 0; if(IsWinNt()) // WinNT Code { char cDeviceName[32]; if(bPysical) { strcpy(cDeviceName,"\\\\.\\PhysicalDrive0"); itoa(iDrive,cDeviceName+17,10); iDrive+=32; } else{ strcpy(cDeviceName,"\\\\.\\A:"); cDeviceName[4] += iDrive; } hDevice = CreateFile(cDeviceName,iMode[iAccess], FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(hDevice == INVALID_HANDLE_VALUE)return NULL; if(bLock) { dwBytes=0; if(!DeviceIoControl(hDevice,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&dwBytes,0)) { CloseHandle(hDevice); return FALSE; } } } else{ // Win98 Code if(bPysical)return FALSE; hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL); } if(hDevice == INVALID_HANDLE_VALUE)return NULL; hHandles[iDrive]=hDevice; return (void*)(iDrive+1); } //***************************************************************************** //* //* GetDriveData //* //***************************************************************************** // Auslesen der Daten eines Laufwerek // iDrive : Laufwerksnummer (A=0 B=1 ... 0x8000=Pysical_0 0x8001=Pysical_1 ... ) // pDiscData : Zeiger auf den Puffer in dem die Sektoren geschrieben werden. // Ergibt TRUE wenn die Daten gelesen wurden. BOOL GetDriveData(int iDrive,DISK_GEOMETRY *pDiscData) { HANDLE hDevice; BOOL bResult; DWORD dwBytes; BOOL bPysical=FALSE; pDiscData->Cylinders.QuadPart = 1023; pDiscData->TracksPerCylinder = 255; pDiscData->SectorsPerTrack = 63; if(iDrive&0x8000) { bPysical=TRUE; iDrive&=~0x8000; } if(iDrive <0 || iDrive> 26)return 0; if(IsWinNt()) // WinNT Code { char cDeviceName[32]; if(bPysical) { strcpy(cDeviceName,"\\\\.\\PhysicalDrive0"); itoa(iDrive,cDeviceName+17,10); } else{ strcpy(cDeviceName,"\\\\.\\A:"); cDeviceName[4] += iDrive; } hDevice = CreateFile(cDeviceName,GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(hDevice == INVALID_HANDLE_VALUE)return FALSE; bResult=DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, pDiscData, sizeof(DISK_GEOMETRY), &dwBytes, NULL); CloseHandle(hDevice); return bResult; } return FALSE; } |