Introduction Due to a large extent, the Windows operating system has taken a large extent (for example, without directly accessing physical memory directly under Windows operating systems, various DOS, BIOS interrupts, etc.) make the majority of programming staff for a long time During the development process, it is unknown to develop such subconsciously - directly manipulating hardware devices under Windows operating systems is extreme difficult and very cumbersome, and looks it as a penalty of Windows programming. Although such an argument in most occasions is also an adaptation, it is not that difficult to access all hardware equipment. In fact, Windows also provides another way to access the hardware device in DOS, that is, all hardware devices are "file", which is allowed to read all hardware devices. Write a way to access it. Another purpose of writing this article is to help readers have any fear of hardware programming in a Windows environment. The access to disk sector data has been mentioned, and all devices are operated as files under Windows. If you program to program a series of readers, you can more familiar with the serial ports 1, 2, you can use "COM1", "COM2" as a parameter, "COM1", "COM2" is file The way to store the path indicates the hardware device to be operated. But if you need to read and write a sector of the disk, many readers may not think of using the createfile () function or I don't know how to use it. In fact, similar to the access to the serial port, you need to indicate the hardware device (hard disk) to operate similar to the file storage path. But here is not to identify a physical existing hard drive with "Disk1", "Disk2". Since the logical sector is existing on a logical partition, it is necessary to specify the disk logical partition that needs to be accessed in a particular format. For logical partition x, its format is "//./x:".
HANDLE CreateFile (LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
The CreateFile () function prototype is as shown above, because access is the actually existing disk sector, so it can only set the dwcreationdisposition parameter with an OPEN_EXISTING flag indicates that the existing file (device) will be opened. As for the use of other parameters, the usage is the same as the usual file.
The entire disk logical partition is opened through CreateFile (), and to manipulate some sectors of the partition, so it is necessary to move the pointer to the displayed disk sector to operate by the setFilePointer () function. . SetFilePointer () Function original is:
DWORD SETFILEPOINTER (Handle Hfile); Plong LpdistanceTomovehigh, DWORD DWMOVEMETHOD
Parameter hfile is a file (device) handle for CreateFile (); LDistanceTomove and LPDistanceTomovehigh point out the low-end and high ends to set offset; dwmoveMethod pointed out where to start moving from where the file pointer starts, and possible options with file_start (starting from the file ), File_end (end from the end) and File_Current (from the file current location). After positioning the sector to be accessed, you can implement the corresponding read and write access via the readfile () or writefile () function, and the specific operation and file reading and writing are not too big. Finally, after the access operation is completed, the file handle releases the resource with a CloseHandle () to complete a complete disk sector data access operation. The specific read process is given below:
Bool CDIRECTACCESSHDDLG :: Writecturector (Byte BDrive, DWORD DWSTARTSector, Word Wsectors, Lpbyte LPsectBuff) // Write {IF (BDRIVE == 0) Return 0 for disk sector data; char devname [] = ". //A: "; devName [4] = 'A' bDrive - 1; HANDLE hDev = CreateFile (devName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDev == INVALID_HANDLE_VALUE) return 0; SetFilePointer (hDev, 512 * dwStartSector, 0, FILE_BEGIN); DWORD dwCB; BOOL bRet = WriteFile (hDev, lpSectBuff, 512 * wSectors, & dwCB, NULL); CloseHandle (hDev); return bRet;} BOOL CDirectAccessHDDlg :: ReadSectors (BYTE bDrive, DWORD dwStartSector, Word Wsectors, LPBYTE LPSECTBUFF) // Read {IF (BDRIVE == 0) return 0 for disk sector data; char devname [] = ". //A:"; devname [4] = 'a' bdrive - 1; HANDLE hDev = CreateFile (devName, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDev == INVALID_HANDLE_VALUE) return 0; SetFilePointer (hDev, 512 * dwStartSector, 0, FILE_BEGIN); DWORD dwCB; BOOL Bret = readfile (HDEV, LPSECTBUFF, 512 * WSECTORS, & DWCB, NULL); CloseHandle (HDEV); RETURN BRET;
Disk sector data Direct read and write technology application Previous implementation of the core processing process of read and write access to disk sector data. On this basis, some applications with practical value can be completed, for example, you can implement the contents of the sectors that specify the stand sector in the specified disk partition:
IF (readsectors (udiskid, m_ufrom, (uint) dwsectornum, bbuf) == false) {MessageBox ("The selected disk partition does not exist!", "error", MB_OK | MB_ICONERROR); Return;} For convenience of data display, You can do the following processing to complete the format conversion:
For (dword i = 0; i The display result is shown in the figure above. Another application is similar, that is, backup and recovery processing of disk sector content. Many antivirus software provide such a function: backup of the content of the hard disk boot area, once the hard disk boot sector is damaged by the virus, it is possible to recover the writing of backup data. The backup operation is similar to the previous data display operation, but the read content is directly saved to the specified file without formatting: File.open (filedlg.getpathname (), cfile :: modecreate | cfile :: modeReadwrite; ... if (readsectors (udiskid, m_ufrom, (uint) dwsectornum, bbuf) == false) {MessageBox ("selected Disk partition No! "," Wrong ", MB_ok | MB_ICONERROR); return;} file.write (BBUF, DWSECTORNUM * 512); file.close (); The recovery processing of the data is opposite to, first open the backup file and calculate the number of sectors to be written according to the file length, and then read its content to the cache, and finally write it to the specified sector to complete the data recovery: file.Open (fileDlg.GetPathName (), CFile :: modeReadWrite); DWORD dwSectorNum = file.GetLength (); if (! dwSectorNum% 512 = 0) return; dwSectorNum / = 512; unsigned char * bBuf = new unsigned char [ DWSECTORNUM * 512]; File.Read (BBUF, DWSECTORNUM * 512); if (Writecturectors (udiskid, m_ufrom, (uint) dwsectornum, bbuf) == false) {MessageBox ("The selected disk partition does not exist!", "error!" ", MB_OK | MB_ICONERROR); RETURN;} file.close (); delete [] bbuf; The last application to give is the security erase of disk data. It is well known that the file management system is implemented by the file management system under the operating system. When a file is deleted, the entire content of the file does not have any damage, and if there is no overlay of external data, it is fully able to recover the previously deleted files through various data recovery software. However, in the special confidential industry of military, the government, the department requires that the data is completely deleted, that is, the deleted data is not recoverable. In order to ensure the reliable emptiness of the disk data, you can write all the sectors and then write all 0. The reason why writes multiple times is because a write can only prevent the recovery process of data recovery software. If the number of coverage is not much, it is still possible to restore the previously deleted data by a special instrument that is called "magnetic disk magnifying glass", so it is necessary to repeat data to the sector multiple times, repeated number of times The more the erasure is better. Below is the specific implementation code of this section: unsigned char bbuf [512]; uint i = 0; BOOL BRET = true; while (m_balldisk) {MEMSET (BBUF, 0xFF, SIZEOF (BBUF)); Bret = Writecturectors (udiskid, i , 1, bbuf; MEMSET (BBUF, 0, SIZEOF (BBUF)); Bret = WriteSectors (udiskid, i, 1, bbuf); if (bret == false) {if (i == 0) MessageBox (" Choosing disk partition does not exist! "," Error ", MB_OK | MB_ICONERROR); Else MessageBox (" Disk Data Erase! "," Error ", MB_OK | MB_ICONEROR); Return;} i ;} summary This article describes only the direct reading method of disk sector content and gives several major applications such as display, backup and recovery, complete erase of disk data. Readers can implement other applications as needed, such as using disk sector content, data hidden, disk delete data recovery, etc. The program code described herein is compiled by Microsoft Visual C 6.0 under Windows 2000 Professional SP4.