ReadingWriting Disk Sectors (Absolute Disk ReadWrite)

xiaoxiao2021-03-06  51

Windows NT / 2K / XP

In Windows NT / 2K / XP the CreateFile function can be used to open a disk drive or a partition on it. After getting a handle to the disk drive using CreateFile function the ReadFile function can be used to read sectors and the WriteFile function can be Used to write to the drive.if you want to open a logical drive give the filename param of the createfile function as ".//a:" or ".//c:" ... etc. and if you want to open A Physical Drive for Raw Reading / Writing Give The FileName Param as ".//physicaldrive0" or ".//physicaldrive1" ... etc

Code to read Disk Sectors from Win 9X / NT / 2K / XP

Reads [NumberOfsectors] Disk Sectors from [Drive {0 = a, 1 = B, 2 = C, ...}] Drive Starting from [StartingLogicalsector]

TO Read Sectors from Win 9X INT 21H'S 7305H Extension IS Used (Extended Absolute Disk Read / Write). For More Info About Int 21h'S 7305H Extention Refer Ralf Brown's Interrupt List.

Char * Readsectors (Int Drive, DWORD StartingLogicalsector, Int Numberofsectors)

{

// All MSDOS Data Structures Must Be Packed ON A 1 byte Boundary

#pragma pack (1)

Struct

{

DWORD Startingsector;

Word Numberofsectors;

DWORD PBUFFER;

CONTROLBLOCK;

#pragma pack ()

#pragma pack (1)

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;

#pragma pack ()

Char * buffer = (char *) Malloc (512 * numberofsectors);

Handle HDevice;

DIOC_REGISTERS REG;

Bool Fresult;

DWORD CB;

// Creating Handle to vwin32.vxd (win 9x)

HDevice = CREATEFILE (".//vwin32",

0,

0,

NULL,

0,

FILE_FLAG_DELETE_ON_CLOSE,

NULL);

IF (HDevice == Invalid_Handle_Value) {

// Win NT / 2K / XP Code

Handle HDevice;

DWORD BYTESREAD;

Char _Devicename [] = ". //a:";

_Devicename [4] = drive;

// Creating a handle to disk drive using createfile () function ..

HDevice = CreateFile (_DeviceName,

Generic_read, file_share_read | file_share_write,

NULL, OPEN_EXISTING, 0, NULL

IF (HDevice == Invalid_Handle_Value)

Return NULL;

// setting the pointer to point to the start of the sector we want to read ..

SetFilePointer (HDevice, (StartingLogicalsector * 512), NULL, FILE_BEGIN;

IF (! Readfile (HDevice, Buffer, 512 * Numberofsectors, & Bytesread, null)

Return NULL;

}

Else

{

// code for win 95/98

Controlblock.startingsector = (dword) StartingLogicalsector;

Controlblock.numberofsectors = (dword) Numberofsectors;

Controlblock.pbuffer = (dword) buffer;

/ / -------------------------------------------------------------------------------------------- -----------

// 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 Extension

// DS: bx -> Base Addr of the

// Control Block Structure

// DL Must Contain the Drive Number

// (01H = A:, 02H = B: ETC)

/ / -------------------------------------------------------------------------------------------- -----------

REG.REG_ESI = 0x00;

REG.REG_ECX = -1;

REG.REG_EBX = (DWORD) (& ControlBlock);

REG.REG_EDX = Drive 1;

REG.REG_EAX = 0x7305;

// 6 == vwin32_dioc_dos_driveinfo

FResult = DeviceioControl (HDevice,

6,

& (REG),

Sizeof (REG),

& (REG),

Sizeof (REG),

& CB,

0);

IF (! FRESULT || (reg.reg_flags & 0x0001)) Return NULL;

}

CloseHandle (HDEvice);

Return buffer;

}

-------------------------------------------------- -------------------------------------------------- --------------------------------------- # include #include

Bool getDriveGeometry (Disk_geometry * PDG)

{

Handle Hdevice; // Handle to the Drive To Be EXAMINED

Bool Bresult; // Results Flag

DWORD JUNK; // Discard Results

HDevice = Createfile (".// PhysicalDrive0", // Drive To Open

0, // no access to the drive

File_share_read | // Share Mode

File_share_write,

Null, // Default Security Attributes

Open_existing, // disposition

0, // File Attributes

Null); // Do Not Copy File Attributes

IF (hdevice == invalid_handle_value) // canNot Open the Drive

{

Return (False);

}

BRESULT = Deviceiocontrol (HDevice, // device to be queried

IOCTL_DISK_GET_DRIVE_GEOMETRY, / / ​​OPERATION TO PERFORM

NULL, 0, // NO Input Buffer

PDG, SizeOf (* PDG), // Output Buffer

& junk, // # bytes returned

(LPOVERLAPPED) NULL); // Synchronous I / O

CloseHandle (HDEvice);

Return (BRESULT);

}

Int main (int Argc, char * argv [])

{

Disk_geometry PDG; // Disk Drive Geometry Structure

Bool Bresult; // Generic Results Flag

Ulonglong disksize; // size of the drive, in bytes

BRESULT = GETDRIVEMETRY (& PDG);

IF (BRESULT)

{

Printf ("cylinders =% i64D / n", pdg.cylinders;

Printf ("Tracks Per Cylinder =% LD / N", (Ulong) pdg.trackspercylinder

Printf ("Sectors Per TRACK =% LD / N", (Ulong) PDG.sectorspertrack);

Printf ("Bytes Per Sector =% LD / N", (Ulong) pdg.bytespector;

Disksize = pdg.cylinders.quadpart * (ulong) pdg.trackspercylinder * (ulong) pdg.sectorspertrack * (ulong) pdg.bytespersector;

Printf ("Disk size =% i64d (bytes) =% i64D (MB) / N", Disksize,

Disksize / (1024 * 1024));

}

Else

{

Printf ("GetDriveGeometry Failed. Error% LD. / N", getLastError ());

}

Return (Int) BRESULT

}

转载请注明原文地址:https://www.9cbs.com/read-83277.html

New Post(0)