🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Problems writing boot record to removable media under WinME

#11056
From: Walt Morrey
Date: Mon, 01 Mar 2004 19:52
143 lines
5316 bytes
I've made some changes and am now trying Int 21h Function 440Dh Minor Code 41h.  I don't know how to 
get the correct Head and Cylinder from startinglogicalsector, but right now, I'm only trying to 
write to the Boot sector, so I assume Head = Cylinder = 0 for this.

This also doesn't work --  the DeviceIoControl request for 440Dh at the end fails.

Any further suggestions?  Other than writing a 16-bit application -- I'd like the result to wirk 
under WinXP.




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

#pragma pack(1)
struct
{
    BYTE SpecFunc;
    WORD Head;
    WORD Cylinder;
    WORD FirstSector;
    WORD Sectors;
    DWORD Buffer;
} RWBlock;
#pragma pack()

//char* buffer = (char*)malloc (512*numberofsectors);
HANDLE hDevice ;
DEVIOCTL_REGISTERS reg ;
BOOL  fResult ;
DWORD cb ;

if( Win32Platform == VER_PLATFORM_WIN32_NT )
{
    // 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
// 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 )
       return FALSE ;

    ControlBlock.StartingSector = (DWORD)startinglogicalsector;
    ControlBlock.NumberOfSectors = (DWORD)numberofsectors ;
    ControlBlock.pBuffer = (DWORD)buffer ;

//------------------- Int 21h Function 7305h
    //-----------------------------------------------------------
    // 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);

//------------------- Int 26h
//   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);

//------------------- Int 21h Function 440Dh Minor Code 41h
    RWBlock.SpecFunc = 0;
    RWBlock.Head = 0;
    RWBlock.Cylinder = 0;
    RWBlock.FirstSector = (DWORD)startinglogicalsector;
    RWBlock.Sectors = (DWORD)numberofsectors ;
    RWBlock.Buffer = (DWORD)buffer ;
    reg.reg_EBX = drive + 1;     // 1 = A, 2 = B, etc
    reg.reg_ECX = 0x4841;        // FAT32, 16, or 12 & Write - 0x41
    reg.reg_EDX = (DWORD)(&ControlBlock);
    reg.reg_EAX = 0x440d;
    fResult = DeviceIoControl ( hDevice,
       6,
       &(reg), sizeof (reg),
       &(reg), sizeof (reg),
       &cb, 0);


    if (!fResult || (reg.reg_Flags & 0x0001))
    {
       CloseHandle(hDevice);
       return FALSE;
    }
Technical Details
Message-ID: <40439482$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <40437604@newsgroups.borland.com> <404380f3$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,5a57fca3b977b3f5
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-03-01 11:56:36 PST
Date: Mon, 01 Mar 2004 14:52:12 -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: Re: Problems writing boot record to removable media under WinME
References: <40437604@newsgroups.borland.com> <404380f3$1@newsgroups.borland.com>
In-Reply-To: <404380f3$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 68.214.68.223
Message-ID: <40439482$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1078170754 68.214.68.223 (1 Mar 2004 11:52:34 -0800)
Lines: 144
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail