🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Problems getting DISK_GEOMETRY under WinME

#11268
From: Walt Morrey
Date: Sat, 21 Feb 2004 19:53
117 lines
4992 bytes
I'm using the following code to try to get DISK_GEOMETRY on all the drives on my computer under 
WinME.  I first tried calling CreateFile within the function, GetDriveGeom, using DeviceName. 
CreateFile failed, so I put in "\\\\.\\vwin32" instead.  That works, and then the call to 
DeviceIoControl in the line below fails.

         fResult = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY,
             NULL, 0, &disk, sizeof(disk), &cb, 0);

I have only a Button Press to trigger THPI_Transfer_Form1::GetMedia() and a Memo in the form.

How can I get DISK_GEOMETRY information AND, in the future, use that information to read and write 
to a removeable drive sector-by-sector (NO FAT, etc, just the minimum info on Sector 0)?


//---------------------------------------------------------------------------
void __fastcall THPI_Transfer_Form1::GetMedia(TObject *Sender)
{
    MID mid;
    UINT nDrive;
    AnsiString IString;
    BYTE DeviceName[20];
    DISK_GEOMETRY DiskGeom;
    char * str1;

    nDrive = 0;
    strcpy(DeviceName, "\\.\PhysicalDrive0");
    HPI_Transfer_Form1->Memo1->Lines->Clear();
    while (nDrive < (DriveStringBufferSize/4))      //  DriveStringBufferSize is 24 for 6 drives
    {
       IString = "Drive "+IntToStr(nDrive)+"  ";
       DeviceName[15] = '0'+nDrive;
       if( !GetMediaID(&mid, nDrive) )
       {
          IString += "error on nDrive";
       }
       else
       {
          if( !GetDriveGeom( DiskGeom, DeviceName ) )
          {
             IString += "Error getting Geometry";
          }
          else
          {
             str1 = mid.midFileSysType;
             *(str1+7) = '\0';
             IString += str1;
             sprintf( str1, "0x%08x 0x%01x 0x%08x 0x%08x 0x%08x, DiskGeom.Cylinders, 
DiskGeom.MediaType, DiskGeom.TracksPerCylinder, DiskGeom.SectorsPerTrack, DiskGeom.BytesPerSector" );
             IString += str1;
          }
       }
       HPI_Transfer_Form1->Memo1->Lines->Append(IString);
       nDrive++;
    }
}
//---------------------------------------------------------------------------

BOOL GetDriveGeom(DISK_GEOMETRY disk, char * lpDeviceName)
{
     HANDLE hDevice;
     BOOL fResult;
     DWORD cb;
     //lpDeviceName = "\\\\.\\vwin32"

     hDevice = CreateFile("\\\\.\\vwin32",
         GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
         (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
         FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
     if (hDevice == (HANDLE) INVALID_HANDLE_VALUE)
         return FALSE;
     else {
         fResult = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY,
             NULL, 0, &disk, sizeof(disk), &cb, 0);
         if (!fResult)
             return FALSE;
     }
     CloseHandle(hDevice);
     return TRUE;
}

BOOL GetMediaID(PMID pmid, UINT nDrive)
{
     DEVIOCTL_REGISTERS reg;

     reg.reg_EAX = 0x440D;       /* IOCTL for block devices */
     reg.reg_EBX = nDrive;       /* zero-based drive ID     */
     reg.reg_ECX = 0x0866;       /* Get Media ID command    */
     reg.reg_EDX = (DWORD) pmid; /* receives media ID info  */
     if (!DoIOCTL(&reg))
         return FALSE;
     if (reg.reg_Flags & 0x8000) /* error if carry flag set */
         return FALSE;
     return TRUE;
}

BOOL DoIOCTL(PDEVIOCTL_REGISTERS preg)
{
     HANDLE hDevice;
     BOOL fResult;
     DWORD cb;

     preg->reg_Flags = 0x8000; /* assume error (carry flag set) */
     hDevice = CreateFile("\\\\.\\vwin32",
         GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
         (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
         FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
     if (hDevice == (HANDLE) INVALID_HANDLE_VALUE)
         return FALSE;
     else {
         fResult = DeviceIoControl(hDevice, VWIN32_DIOC_DOS_IOCTL,
              preg, sizeof(*preg), preg, sizeof(*preg), &cb, 0);
         if (!fResult)
              return FALSE;
     }
     CloseHandle(hDevice);
     return TRUE;
}
Technical Details
Message-ID: <4037b73a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,8ef29fec1dea7408,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-02-21 12:09:58 PST
Date: Sat, 21 Feb 2004 14:53:11 -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 getting DISK_GEOMETRY under WinME
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 68.214.66.165
Message-ID: <4037b73a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1077393210 68.214.66.165 (21 Feb 2004 11:53:30 -0800)
Lines: 118
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail