VC语言

本类阅读TOP10

·VC++下使用ADO编写数据库程序
·VC++ 学习笔记(二)
·Windows消息大全
·每个开发人员现在应该下载的十种必备工具
·在2000和xp下,隐藏进程,VC6.0测试通过!!!
·用Visual C++打造IE浏览器(1)
·Netmsg 局域网聊天程序
·教你用VC6做QQ对对碰外挂程序
·VC++学习笔记(四)
·VC++中经常使用的函数!~~

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Reading/Writing Disk Sectors (Absolute Disk Read/Write)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

Windows 9x

Windows 9x provides a vxd called vwin32.vxd using which msdos functions including absolute disk read/write can be performed. A handle to vwin32.vxd must be first created using the CreateFile function. After creating the handle DeviceIoControl function must be used with the correct control code to read or write from the disk drives. The control code depends on the type of the function you want to perform.

The control codes for vwin32.vxd are ...

Code Description
VWIN32_DIOC_DOS_IOCTL (1) Interrupt 21h Function 4400h through 4411h
VWIN32_DIOC_DOS_INT25 (2) Performs the Absolute Disk Read command (Interrupt 25h)
VWIN32_DIOC_DOS_INT26 (3) Performs the Absolute Disk Write command (Interrupt 25h)
VWIN32_DIOC_DOS_INT13 (4) Performs Interrupt 13h commands
VWIN32_DIOC_DOS_DRIVEINFO (6) Interrupt 21h Function 730X commands

The lpvInBuffer & lpvOutBuffer params of DeviceIoControl function must point to addresses of DIOC_REGISTERS structures . This structure contains the state of the processor registers before ioctl and after ioctl.

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;
}



相关文章

相关软件




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有