🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #11052

Problems writing boot record to removable media under WinME

#11052
From: Walt Morrey
Date: Mon, 01 Mar 2004 17:42
140 lines
5216 bytes
I extracted and modified the following code and am trying to use it to re-write the Boot record on a 
removable drive.  I do that checking before calling this subroutine.

Since I'm running right now on a WinME machine, the code skips the win NT/2K/XP code and goes to the 
code for win 95/98.  I tried using DeviceIoControl with both VWIN32_DIOC_DOS_DRIVEINFO and 
VWIN32_DIOC_DOS_INT26.  Both fail with a system comment telling me:

		Old Disk Utility Write Error
You are using a disk utility designed for an older version of MS-DOS or Windows...

What do I need to do to get this to work?


.... CALLING CODE
....
    if( str1[0] > 'a' )		// str1 contains "f:\\" - a removable media drive
       drive = str1[0] - 'a';
    else
       drive = str1[0] - 'A';
    WriteSectors(drive, 0, 4, (char*)(&Boot));
....

//-----------------------------------------------------------------
//Code to write disk sectors from win 9x/NT/2K/XP
//
//Writes [numberofsectors] disk sectors from
//      [drive {0=A, 1=B, 2=C, ...}] drive
//      starting from [startinglogicalsector]
//
//To write 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.
//------------------------------------------------------------------
BOOL WriteSectors(int drive, DWORD startinglogicalsector, int numberofsectors, char*buffer)
{

// 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 ()

//char* buffer = (char*)malloc (512*numberofsectors);
HANDLE hDevice ;
DEVIOCTL_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 FALSE;

   // Setting the pointer to point to the start of the sector we want to read ..
   SetFilePointer (hDevice, (startinglogicalsector*512), NULL, FILE_BEGIN);

   if (!WriteFile (hDevice, buffer, 512*numberofsectors, &bytesread, NULL) )
      {
         CloseHandle(hDevice);
         return FALSE;
      }
}
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 = 0x6001 ;          // for write
//  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);

    reg.reg_EAX = drive;    // Int 26h drive numbers are 0-based.
    reg.reg_EBX = (DWORD)(&ControlBlock);
    reg.reg_ECX = 0xFFFF;        // use DISKIO struct
    fResult = DeviceIoControl(hDevice, VWIN32_DIOC_DOS_INT26,
       &reg, sizeof(reg),
       &reg, sizeof(reg),
       &cb, 0);

    if (!fResult || (reg.reg_Flags & 0x0001))
    {
       CloseHandle(hDevice);
       return FALSE;
    }
}

CloseHandle(hDevice);
return TRUE;
}
Technical Details
Message-ID: <40437604@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!cyclone.bc.net!news-hog.berkeley.edu!ucberkeley!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,5a57fca3b977b3f5,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-03-01 10:05:01 PST
Date: Mon, 01 Mar 2004 12:42:05 -0500
From: Walt Morrey <dadinc@bellsouth.net>
User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Problems writing boot record to removable media under WinME
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 68.214.68.223
Message-ID: <40437604@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1078162948 68.214.68.223 (1 Mar 2004 09:42:28 -0800)
Lines: 141
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!cyclone.bc.net!news-hog.berkeley.edu!ucberkeley!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail