Hi All,
I have been pulling my hair out trying to figure out where I going wrong with this... I want to read a physical disk, goto offset 'xxx' and read x number of bytes into a buffer. I then want to save the buffer as a file. My program keeps returning a 4 byte file filled with random values even when I set a file size of 1000, source below, would be grateful for help on this!
I have been pulling my hair out trying to figure out where I going wrong with this... I want to read a physical disk, goto offset 'xxx' and read x number of bytes into a buffer. I then want to save the buffer as a file. My program keeps returning a 4 byte file filled with random values even when I set a file size of 1000, source below, would be grateful for help on this!
Code:
var
buf2:array of byte; // Dynamic array to hold file data
hDiskfile : THandle; // Disk
hSavefile : THandle; // File to Save
CPConverted: LARGE_INTEGER;
BytesWritten , intReadedAmount: Cardinal;
begin
CPConverted.QuadPart := Offset; // This is the location of the data on the disk
SetLength(buf2, FileSize); // Size of the file to save
if WIN32PLATFORM = VER_PLATFORM_WIN32_NT then
begin
// open disk
hDiskFile := CreateFile(pChar('\\.\PhysicalDrive0'), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// goto location on disk and read data into buffer
SetFilePointer(hDiskFile, Longint(CPConverted.LowPart), @CPConverted.HighPart, FILE_BEGIN);
ReadFile(hDiskFile, buf2, sizeof(buf2), intreadedamount, nil);
// create new file and save contents of buffer into new file
hsavefile := CreateFile(pChar('C:\Test.txt'), GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
WriteFile(hsavefile,buf2,sizeof(buf2),BytesWritten,nil);
CloseHandle(hcarvefile);
CloseHandle(hDiskFile);
end;
end;