Bonjour à tous,
Je désire créer un petit prog pour lire les secteur d'un dique.(en hexa)
Voici mon code :
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
//LIRE LES SECTEUR D UN LECTEUR
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 ((LPCWSTR) "\\\\.\\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( (LPCTSTR)_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 = (WORD)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);
cout<<buffer<<endl;
if (!fResult || (reg.reg_Flags & 0x0001)) return NULL;
}
cout<<buffer<<endl;
CloseHandle(hDevice);
return buffer;
}
int LoadSectorBuffer(char* m_strSectorContents)
{
char *m_strSectorContentsHex;
int m_intDrive=1;
DWORD m_dwStartingLogicalSector=2;
int m_intNumberOfSectors=2;
char hexcode[10];
m_strSectorContents = (char *)malloc (512);
m_strSectorContentsHex = (char *)malloc (512*5);
//m_strSectorContents = ReadSectors (m_intDrive, m_dwStartingLogicalSector, m_intNumberOfSectors);
// return if NULL
if (!m_strSectorContents) return -1;
// load HEX buffer
sprintf (hexcode, "%X ", m_strSectorContents[0] & 0xff);
strcpy (m_strSectorContentsHex, hexcode);
for (int i=1; i<512; i++)
{
sprintf (hexcode, "%02X ", m_strSectorContents[i] & 0xff);
strcat (m_strSectorContentsHex, hexcode);
}
//Instrcution supprimer
return 0;
}
int main()
{
char* m_strSectorContents;
m_strSectorContents = ReadSectors(2, 2, 0);
LoadSectorBuffer(m_strSectorContents);
return 0;
}
Dans ma fonction readsectors, lorsque je crée un handle sur le disque avec la fonction createfile, cela me return faux et sort de la fonction avec le if qui suit. J'ai trouver ce morceau de code sur codeguru et jessais de le faire fonctionner en mode console.
Si vous pouvez m'aider..
Merci
Vive le rugby XIII