Windows NT2000 reads and writes physical, logical disk sectors

xiaoxiao2021-03-06  41

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

HDRIVEHANDLE: THANDLE;

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Const

BYTESPERSECTOR = 512;

Sectorcount = 1; // Read the sector number

Sectorstart = 0; // Start sector number

DRIVE = '//./c:'; // Drive

VAR

Str: string;

P: pchar;

i: cardinal

Begin

HDRIVEHANDLE: = CreateFile (drive, generic_all, file_share_read or file_share_write,

NIL, Open_EXISTING, 0, 0);

IF (HDRIVEHANDE <> Invalid_Handle_Value) THEN

Begin

P: = allocmem (sectorcount * bytespector); // p must be a new application memory or global variable, cannot be partial variables

// Defined as partial variables that cannot be read and write disk

Fileseek (HDRIVEHANDLE, SECTORSTART * BYTESPERSECTOR, 0); // Start sector

IF FileRead (HDriveHandle, P [0], Sectorcount * Bytespector) <> // Reading Demo

Sectorcount * bytespersector then

Raise Exception.create ('Read Error!');

Str: = '';

For i: = 0 to 512 - 1 DO

Begin

Str: = Str Format ('%. 2x', [Integer (p [i])]);

IF i mod 16 = 15 THEN

Str: = STR # 13;

END;

ShowMessage (STR);

Fileseek (HDRIVEHANDLE, SECTORSTART * BYTESPERSECTOR, 0); // Start sector

IF FileWrite (HDRIVEHANDLE, P [0], Sectorcount * Bytespector) <> // write sector

Sectorcount * bytespersector then

Raise Exception.create ('Write Error!');

FreeMem (p, sectorcount * bytespector);

CloseHandle (HDRIVEHANDLE);

END;

END;

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

Const

BYTESPERSECTOR = 512;

Sectorcount = 1; // Read the sector number

Sectorstart = 0; // Start sector number

Drive = '//./physicaldrive0'; // Physical disk

VAR

Str: string;

P: pchar;

i: cardinal

Begin

HDRIVEHANDLE: = CreateFile (drive, generic_all, file_share_read or file_share_write, nil, open_existing, 0, 0);

IF (HDRIVEHANDE <> Invalid_Handle_Value) THEN

Begin

P: = allocmem (sectorcount * bytespector); // p must be a new application memory or global variable, cannot be partial variables

// Defined as partial variables that cannot be read and write disk

Fileseek (HDRIVEHANDLE, SECTORSTART * BYTESPERSECTOR, 0); // Start sector

IF FileRead (HDriveHandle, P [0], Sectorcount * Bytespector) <> // Reading Demo

Sectorcount * bytespersector then

Raise Exception.create ('Read Error!');

Str: = '';

For i: = 0 to 512 - 1 DO

Begin

Str: = Str Format ('%. 2x', [Integer (p [i])]);

IF i mod 16 = 15 THEN

Str: = STR # 13;

END;

ShowMessage (STR);

Fileseek (HDRIVEHANDLE, SECTORSTART * BYTESPERSECTOR, 0); // Start sector

IF FileWrite (HDRIVEHANDLE, P [0], Sectorcount * Bytespector) <> // write sector

Sectorcount * bytespersector then

Raise Exception.create ('Write Error!');

FreeMem (p, sectorcount * bytespector);

CloseHandle (HDRIVEHANDLE);

END;

END;

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

New Post(0)