Practice 7: Read and Write disk sectors in Windows 9x

Source: Internet
Author: User
Tags dio

In Windows NT/2 k/XP, use createfile to open a name similar "\\. \ A: "file", you can deal with the device driver, and access the disk through readfile/writefile in absolute address mode. Windows 9x does not support this simple method. This article introduces a method for implementing direct disk access in Windows 9x: using the system's vwin32.vxd, using deviceiocontrol to call the DOS int21 7305h and opencdh functions. This call supports fat12, fat16, and FAT32, applicable to Windows 95 Sr2 and later versions.

First, let's take a look at the entry parameters of the DOS int21 7305h function:

 
Ax -- function no. 7305hds: BX -- Information Structure of the read/write sector CX -- must be-1dl -- drive letter:1= :,2= B :,3= C:,... si -- read/write MARK: bytes 0 = read,1= Write

If the call is successful, clear the C flag; otherwise, set the C flag.

DS: BX points to a structure, which is defined as follows:

 
Diskio struc dwstartsector dd? ; Start slice wsector DW? ; Number of sectors lpbuffer dd? ; Data buffer address diskio ends

In the write operation, you need to "Lock" the drive. The dos int21 license DH's 4ah/6ah function can lock/unlock a logical drive. Its entry parameters are:

 
Ax -- function number 440 dhbh -- lock level, 0-3 level, directly write the sector with 1BL -- drive Number:1= :,2= B :,3= C:,... ch --0x08Cl --0x4aDX --0
 
Ax -- function number 440 dhbl -- drive Number:1= :,2= B :,3= C:,... ch --0x08Cl --0x6a

If the call is successful, clear the C flag; otherwise, set the C flag.

Call the above interrupt through IOCTL Code such as vwin32_dioc_dos_driveinfo. Key to achieving absolute disk read/writeCodeAs follows:

 // Int21 IOCTL code  # Define Vwin32_dioc_dos_ioctl1  # Define Vwin32_dioc_dos_driveinfo 6   // Register group  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, * region; // Io parameters (note the byte alignment Mode)  # Pragma Pack ( 1 ) Typedef   Struct _ Diskio {DWORD dwstartsector; // Start sector Word wsectors; // Number of sectors     Void * Pbuffer; // Buffer pointer } Diskio, * pdiskio; # Pragma Pack () bool absdiskread (byte ndisknumber, // Disk number, 1 = A:, 2 = B:, 3 = C :,... DWORD dwstartsector, // Start sector Word wsectors, // Number of sectors      Void * Pbuffer) // Data buffer pointer {Handle hdevice; dioc_registers regs; diskio Dio; DWORD dwoutbytes; bool bresult; // Open the device and obtain the VxD handle Hdevice = createfile ( "\\\\. \ Vwin32" , // Device path Generic_read | generic_write, // Read/write Method File_pai_read | file_pai_write, // Share mode Null, // Default Security Descriptor Open_existing, // Creation Method File_attribute_normal, // File attributes Null ); // You do not need to refer to the template file       If (Hdevice = invalid_handle_value ){ Return False ;} // Fill in the diskio parameter structure Dio. dwstartsector = dwstartsector; dio. wsectors = wsectors; dio. pbuffer = pbuffer; // Fill register group-interrupt entry parameters Memset (& regs, 0 , Sizeof (Dioc_registers); regs. reg_eax = Zero X 7305 ; // AX = 0x7305 Regs. reg_ebx = (DWORD) & Dio; // EBX = DS: BX = parameter pointer Regs. reg_ecx = 0 xFFFF ; // Cx =-1 Regs. reg_edx = ndisknumber; // DL = disk number Regs. reg_esi = 0 ;// Si = 0 -- read operation       // Use vwin32_dioc_dos_driveinfo to read the disk Dwoutbytes = 0 ; Bresult = deviceiocontrol (hdevice, // Device handle Vwin32_dioc_dos_driveinfo, // Int21 & Regs, Sizeof (Regs ), // Output data buffer and length & Regs, Sizeof (Regs ), // Output data buffer and length & Dwoutbytes, // Output Data Length Null ); // Use synchronous I/O      // Check whether the deviceiocontrol and int21 are correct. Bresult = bresult &&! (Regs. reg_flags & 1 ); Closehandle (hdevice ); Return Bresult;} bool absdiskwrite (byte ndisknumber, // Disk number, 1 = A:, 2 = B:, 3 = C :,... DWORD dwstartsector, // Start sector Word wsectors, // Number of sectors      Void * Pbuffer) // Data buffer pointer {Handle hdevice; dioc_registers regs; diskio Dio; DWORD dwoutbytes; bool bresult; // Open the device and obtain the VxD handle Hdevice = createfile ( "\\\\. \ Vwin32" , // Device path Generic_read | generic_write, // Read/write Method File_pai_read | file_pai_write, // Share mode Null, // Default Security Descriptor Open_existing, // Creation Method File_attribute_normal, // File attributes Null ); // You do not need to refer to the template file       If (Hdevice = invalid_handle_value ){ Return False ;} // Fill in the diskio parameter structure Dio. dwstartsector = dwstartsector; dio. wsectors = wsectors; dio. pbuffer = pbuffer; // Fill register group-interrupt entry parameters Memset (& regs, 0 , Sizeof (Dioc_registers); regs. reg_eax = Zero X 7305 ; // AX = 0x7305 Regs. reg_ebx = (DWORD) & Dio; // EBX = DS: BX = parameter pointer Regs. reg_ecx = 0 xFFFF ; // Cx =-1 Regs. reg_edx = ndisknumber; // DL = disk number Regs. reg_esi = Zero X 6001 ;// Si = 0x6001 -- normal write operation       // Use vwin32_dioc_dos_driveinfo to write data to a disk Dwoutbytes = 0 ; Bresult = deviceiocontrol (hdevice, // Device handle Vwin32_dioc_dos_driveinfo, // Int21 & Regs, Sizeof (Regs ), // Output data buffer and length & Regs, Sizeof (Regs ), // Output data buffer and length & Dwoutbytes, // Output Data Length Null ); // Use synchronous I/O      // Check whether the deviceiocontrol and int21 are correct. Bresult = bresult &&! (Regs. reg_flags & 1 ); Closehandle (hdevice ); Return Bresult;} bool lockvolume (byte ndisknumber) // Disk number, 1 = A:, 2 = B:, 3 = C :,... {Handle hdevice; dioc_registers regs; DWORD dwoutbytes; bool bresult; // Open the device and obtain the VxD handle Hdevice = createfile ( "\\\\. \ Vwin32" , // Device path Generic_read | generic_write, // Read/write Method File_pai_read | file_pai_write, // Share mode Null, // Default Security Descriptor Open_existing, // Creation Method File_attribute_normal, // File attributes Null ); // You do not need to refer to the template file        If (Hdevice = invalid_handle_value ){ Return False ;} // Fill register group-interrupt entry parameters Memset (& regs, 0 , Sizeof (Dioc_registers); regs. reg_eax = 0x0000d ;// AX = 0x0000d Regs. reg_ebx = Zero X 0100 | Ndisknumber; // BH = Lock level, BL = disk number Regs. reg_ecx = 0x084a ; Regs. reg_edx = 0 ; // Use vwin32_dioc_dos_driveinfo to read the disk Dwoutbytes = 0 ; Bresult = deviceiocontrol (hdevice, // Device handle Vwin32_dioc_dos_ioctl, // Int21 & Regs, Sizeof (Regs ), // Input data buffer and length & Regs,Sizeof (Regs ), // Output data buffer and length & Dwoutbytes, // Output Data Length Null ); // Use synchronous I/O       // Check whether the deviceiocontrol and int21 are correct. Bresult = bresult &&! (Regs. reg_flags & 1 ); Closehandle (hdevice ); Return Bresult;} bool unlockvolume (byte ndisknumber) // Disk number, 1 = A:, 2 = B:, 3 = C :,... {Handle hdevice; dioc_registers regs; DWORD dwoutbytes; bool bresult; // Open the device and obtain the VxD handle Hdevice = createfile ("\\\\. \ Vwin32" , // Device path Generic_read | generic_write, // Read/write Method File_pai_read | file_pai_write, // Share mode Null, // Default Security Descriptor Open_existing, // Creation Method File_attribute_normal, // File attributes Null ); // You do not need to refer to the template file        If (Hdevice = invalid_handle_value ){ Return False ;}// Fill register group-interrupt entry parameters Memset (& regs, 0 , Sizeof (Dioc_registers); regs. reg_eax = 0x0000d ; // AX = 0x0000d Regs. reg_ebx = ndisknumber; // BL = disk number Regs. reg_ecx = 0x086a ; // Use vwin32_dioc_dos_driveinfo to read the disk Dwoutbytes = 0 ; Bresult = deviceiocontrol (hdevice, // Device handle Vwin32_dioc_dos_ioctl, // Int21 & Regs,Sizeof (Regs ), // Input data buffer and length & Regs, Sizeof (Regs ), // Output data buffer and length & Dwoutbytes, // Output Data Length Null ); // Use synchronous I/O        // Check whether the deviceiocontrol and int21 are correct. Bresult = bresult &&! (Regs. reg_flags & 1 ); Closehandle (hdevice ); Return Bresult ;}

In the following example, data from 10 sectors is read from the 0 sector of disk A and stored in the file:

Unsigned CharBuf [512*10];If(Absdiskread (1,0,10, Buf) {file * fp = fopen ("A. dat","W + B"); Fwrite (BUF,512,10, FP); fclose (FP );}

In the following example, read the 8,888th sector of drive D and write it back:

Unsigned CharBuf [512]; Lockvolume (4);If(Absdiskread (4,8888,1, Buf )){......If(Absdiskwrite (4,8888,1, Buf) {...} unlockvolume (4);

In the write mode, the bit 0 of the SI register is set to 1, and the bit 15-13 must have different values in different areas of the Disk:

bit 15 bit 14 bit 13 description
0 0 0 Other/unknown.
0 0 1 fat data.
0 1 0 directory data.
0 1 1 normal file data.
1 0 0 reserved.

If you do not follow the above values, although the write operation is successful, the system cannot automatically complete the relevant functions, may cause problems such as fat data backup and drive data compression.

[Related resources]

  • Bhw98 columns: http://www.csdn.net/develop/author/netauthor/bhw98/
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.