CPB Mailing List
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Directly Writing to drives
I use the following code to write sectors directly to a floppy.
// Definition used by DeviceIoControl() API call. Specifies the
control code for the operation.
// This value identifies the specific operation to be performed and the
type of device on which
// the operation is to be performed.
#define VWIN32_DIOC_DOS_INT13 (4)
// structure representing registers used by DeviceIoControl() API call
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, *PDIOC_REGISTERS;
HANDLE hDevice; // Handle to the device that is to
perform the I/O operation
DIOC_REGISTERS reg; // registers used by
DeviceIoControl() API call
BOOL fResult; // Status of DeviceIoControl() API call. If
the function
// succeeds, the return value is
nonzero.
DWORD byteCnt; // number of bytes copied
int nDrive = 0; // drive indicator (Drive A: = 0)
//* Open library containing necessary API function
hDevice = CreateFile("\\\\.\\vwin32", 0, 0, NULL, 0,
FILE_FLAG_DELETE_ON_CLOSE, NULL);
.
.
.
.
//* Write this track to floppy
reg.reg_EAX = 0x0312; // write 1 track
reg.reg_ECX = (firstSector.track() << 8) | 0x01;
reg.reg_EDX = (firstSector.head() << 8) | nDrive;
reg.reg_EBX = (DWORD) &trackBuf[0]; // address of data to write
reg.reg_Flags = 0x0001; // assume error
fResult = DeviceIoControl(hDevice, VWIN32_DIOC_DOS_INT13, ®,
sizeof(reg), ®, sizeof(reg), &byteCnt, 0);
----------
> From: David Gerhart <dgerhart@inter-coastal.net>
> To: cpb-thread@zdtips.com
> Subject: Directly Writing to drives
> Date: Tuesday, October 06, 1998 10:49 AM
>
> Almost a year ago I saved this message because I thought I would try
direct
> access someday. Well I recently got some time to mess with this but I
> haven't been able to make it work.
>
> The code below from Ashley Sutton looks good but it seems I am missing an
> include file because some of the values are not defined.
>
> Specifically
>
> READ_WRITE
> ABSDISKIO_WRITE
> ABSDISKIO_READ
>
> Does anyone know what the values for these are or know the header file I
> need to include?
>
> Better yet, does someone have an example of how to do read and write
> directly to drives?
>
> -----Original Message-----
> From: Sutton, Ashley [mailto:Ashley.Sutton@olsy.co.uk]
> Sent: Monday, November 17, 1997 4:06 AM
> To: 'cpb-thread@zdtips.com'
> Subject: RE: Writing to floppy
>
>
> Craig,
>
> This may help:
>
> #pragma pack(push,1) // Pack structure using byte packing
> typedef struct stBOOTSECTOR { // DISK IMAGE HEADER
> char cJumpInstruction[3], // Jump instruction (Machine code)
> cOEMNameVer[8]; // OEM Name and version number
> WORD wBytePerSec; // Bytes per sector
> BYTE bSecsPerCluster; // Sectors per cluster
> WORD wReservedSecs; // Reserved sectors
> BYTE bNoOfFATS; // Number of FATS
> WORD wEntriesInRoot, // Entries in root directory
> wTotalSecs; // Total sectors on disk
> BYTE bMediumDescriptor; // Media Descriptor
> WORD wSecsPerFAT, // Sectors per FAT
> wSecsPerTrack, // Sectors per track
> wNoOfHeads, // Number of heads
> wReserved; // Reserved
> }BOOTSECTOR, *pBOOTSECTOR;
> #pragma pack(pop)
>
> class TFile
> {
> HANDLE hFile;
> public:
> ~TFile(){CloseHandle(hFile);};
> TFile(){hFile = INVALID_HANDLE_VALUE;};
> TFile(const char* fName, DWORD dwAccess, DWORD dwCreation, DWORD
> dwFlags)
> {
> Open(fName, dwAccess, dwCreation, dwFlags);
> };
> Close(void){CloseHandle(hFile);};
> Open(const char* fName, DWORD dwAccess, DWORD dwCreation, DWORD
> dwFlags)
> { // If another file is already open then close it
> if(hFile != INVALID_HANDLE_VALUE)
> CloseHandle(hFile);
> hFile = CreateFile(fName, dwAccess, 0, 0, dwCreation, dwFlags, 0);
> if(hFile == INVALID_HANDLE_VALUE){
> switch(dwCreation){
> case OPEN_EXISTING :
> switch(dwAccess){
> case READ_WRITE : throw "Error opening file for
> updating.";
> case GENERIC_READ : throw "Error opening file for
> reading.";
> case GENERIC_WRITE : throw "Error opening file for
> writing.";
> default : throw "Error opening file.";
> }
> case CREATE_NEW :
> switch(dwAccess){
> case GENERIC_WRITE :
> default : throw "Error creating new file.";
> }
> }
> }
> };
> operator HANDLE()
> {
> if(hFile == INVALID_HANDLE_VALUE)
> throw "File handle not initialised.";
> else
> return hFile;
> };
> };
>
> //
> ------------------------------------------------------------------------
> ----
> bool AbsDiskIO(char *cBuffer, WORD iFrom, WORD iSectors, BYTE bOp)
> {
> bool blStatus = false; // Status: ERROR = false, OK = true
> DWORD cb; // Number of bytes returned by
> DeviceIoControl
> DIOC_REGISTERS reg; // Register structure used in
> DeviceIoControl call
> TFile hFile; // File handle
>
> // Windows 95 and Operation either READ or WRITE?
> if((int)GetVersion() < 0 && (bOp == ABSDISKIO_WRITE || bOp ==
> ABSDISKIO_READ)){
> // YES: Open device driver
> hFile.Open("\\\\.\\VWIN32",READ_WRITE,0,FILE_FLAG_DELETE_ON_CLOSE)
> ;
> // OK: Read/Write sectors information. Make upto 3 attempts if
> unsucessful
> for(int i = 3 ; blStatus == false && i > 0; i--){
> reg.reg_EAX = 0x0000; // AX = Drive A:
> reg.reg_EBX = (DWORD)cBuffer; // Buffer Address
> reg.reg_ECX = iSectors; // Number of sectors
> reg.reg_EDX = iFrom; // Starting at sector
> reg.reg_Flags = CARRY_FLAG; // Assume failure
> blStatus = (DeviceIoControl(hFile,bOp,
> ®,sizeof(reg),®,sizeof(reg),&cb,0) && !(reg.reg_Flags &
> CARRY_FLAG));
> }
> }
> return(blStatus);
> }
>
> //
> ------------------------------------------------------------------------
> ----
> bool GetFloppyBootSector(pBOOTSECTOR pBootSector)
> {
> bool blStatus; // Status: ERROR = false, OK = true
> char cBuffer[512]; // Temporary Buffer
>
> // Read Boot Sector and save if OK
> if(blStatus = AbsDiskIO(cBuffer, 0, 1, ABSDISKIO_READ))
> memcpy((char *)pBootSector, cBuffer, sizeof(BOOTSECTOR));
> return(blStatus);
> }
>
> //
> ------------------------------------------------------------------------
> ----
> char *GetFloppyType(pBOOTSECTOR stBootSector)
> { // Return floppy media type
> switch(stBootSector->bMediumDescriptor){
> case 0xF0 : return("1.44Mb");
> case 0xF8 : return("Fixed Disk");
> case 0xF9 : return(stBootSector->wSecsPerTrack == 9 ? "720K" :
> "1.2Mb");
> case 0xFC : return("180K");
> case 0xFD : return("360K");
> case 0xFE : return("160K");
> case 0xFF : return("320K");
> default : return("Unknown");
> }
> }
>
> Note:
>
> The TFile class is used to ensure that if an exception occurs during
> file handling the exception will cause the destructor for TFile to be
> called which will ensure that the handle is closed. By using the
> operator HANDLE() the instance can be used as if it were a normal
> handle. This class also ensure that if the handle is used to open up
> another file the old handle is first closed.
>
>
> Regards, Ashley
>
> >-----Original Message-----
> >From: Craig Plecas [SMTP:cplecas@evi-inc.com]
> >Sent: 13 November 1997 22:32
> >To: cpb-thread@zdtips.com
> >Subject: Re: Writing to floppy
> >
> >
> >
> >I was able to do writes to physical sectors on the floppy using the
> >DeviceIOControl function with VWIN32_DIOC_DOS_INT13 (4) as the control
> >code. There is also a code to use INT 25H to write a sector.
> >
> >----------
> >From: David <perfectpc@worldnet.att.net>
> >To: 'cpb-thread@zdtips.com'
> >Subject: RE: Writing to floppy
> >Date: Wednesday, November 12, 1997 8:11 PM
> >
> >There seem to be some obscure commands in the WIN32 Reference help file.
> >(I have also wanted to be able to do this.)
> >I currently have to go through a lot of trouble to get data off flash
> >memory cards because I can't access the sectors directly. (I could do
> >direct writes/reads easily in an earlier DOS version of the program.)
> >
> >I guess Microsoft is trying to move us away from low level manipulation.
> >
> >Perhaps the way to do it under 95/NT is to write a device driver. (Oh
> >good! Another learning curve.;)
> >
> >
> >
> >----------
> >From: Craig Plecas[SMTP:cplecas@evi-inc.com]
> >Sent: Tuesday, November 04, 1997 12:25 PM
> >To: CPB List
> >Subject: Writing to floppy
> >
> >I wish to write to specific sectors on the floppy from within my win95
app.
> > On a whim I tried to include dos.h and use BIOS interrupt INT 13H,
> >function 3 to no avail. Does anyone know of any Windows functions that
can
> >be called to perform this task? Any guidance would be greatly
appreciated.
> >
> >Thanx,
> >Craig Plecas
> >EVI, Inc.
> >cplecas@evi-inc.com
> >
> >----------
>
>
W Komornicki's Home Page |
Main Index |
Thread Index