Reading/Writing Disk Sectors (Absolute Disk Read/Write)

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 extention 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 extention
  // 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 <windows.h> #include <winioctl.h> 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 = GetDriveGeometry (&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.BytesPerSector); 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); }
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值