From: bluestar on
I used below function to partition one physical disk.
But I have some question to happen.

(1) I found the below function can't partition above 2 Logical Drives.
How to modify to do this function?

(2) I found it would happen partition wrong condition(In one physical
disk, it will show 3 Logical Drives and all sizes of 3 Logical
Drives
are over than one physical disk in Disk Management, for example
: 30G HDD, it shows 27.9G, 2G, 27.9G).
How to modify code to prevent this error condition?

Thanks for your great help.

//-------------------------------------------------------------------------------------//
llPhyDiskSize = PhyDiskGeometry(m_PhyDevHwnd);
if( llPhyDiskSize > MAX_SIZE_DISK ) // if over 32G, set to 32G
llPhyDiskSize = MAX_SIZE_DISK;
if( PartitionPhysicalDisk(m_PhyDevHwnd, llPhyDiskSize, &cDrive) )//
partition to FAT32
{
FormatPhyDisk(cDrive); //format to FAT32
}
//-------------------------------------------------------------------------------------//

LONGLONG PhyDiskGeometry(HANDLE hDevice)
{
BOOL bResult;
DWORD junk;
LONGLONG DiskSize;
DISK_GEOMETRY clsPdg;

bResult
= ::DeviceIoControl(hDevice ,IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,0, &clsPdg,sizeof(DISK_GEOMETRY),
&junk, (LPOVERLAPPED) NULL);
if (bResult)
{
DiskSize = clsPdg.Cylinders.QuadPart * (ULONG)
clsPdg.TracksPerCylinder *
(ULONG) clsPdg.SectorsPerTrack * (ULONG) clsPdg.BytesPerSector;
}
else
DiskSize = 0;

return DiskSize;
}

BOOL CCfgEepDlg::FormatPhyDisk(CHAR cDrive)
{
BOOL bResult;
INT nReturn;
TCHAR parameters[MAX_PATH];

DelayTime(500);

wsprintf(parameters, "%c: /FS:FAT32 /Q /Y", cDrive);
nReturn = (INT)ShellExecute(NULL, "open","format",parameters, NULL,
SW_SHOWNORMAL);

DelayTime(3000);
if( nReturn>32 )
{
do
{
if( GetProcessList("format.com") )
Sleep(1000);
else
break;
}
while(1);
bResult = TRUE;
}
else
bResult = FALSE;

return bResult;
}

BOOL GetProcessList(LPCTSTR szProcName)
{
HANDLE hProcessSnap;
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE ) {
return( false );
}

PROCESSENTRY32 pe32;
pe32.dwSize = sizeof( PROCESSENTRY32 );
if( !Process32First( hProcessSnap, &pe32 ) ) {
CloseHandle( hProcessSnap );
return( false );
}

do{
if(_stricmp(szProcName,pe32.szExeFile)==0){
return true;
}
}while(Process32Next( hProcessSnap, &pe32 ));

CloseHandle( hProcessSnap );

return false;
}

BOOL PartitionPhysicalDisk(HANDLE hDevice, LONGLONG llDiskSize, PCHAR
pDrive)
{
BOOL bResult;
DWORD junk, dwDrvOrg, dwDrv;
DRIVE_LAYOUT_INFORMATION_EX drv_layout_info_ex;
CREATE_DISK disk;
INT n;

dwDrvOrg = GetLogicalDrives();

if( dwDrvOrg!=m_dwDrv)
return FALSE;

ZeroMemory(&disk,sizeof(CREATE_DISK));
disk.PartitionStyle = PARTITION_STYLE_MBR;
disk.Mbr.Signature = 0x7B060725;
// Create primary partition MBR
bResult = ::DeviceIoControl(hDevice,IOCTL_DISK_CREATE_DISK,&disk,
sizeofCREATE_DISK),NULL,
0,&junk,NULL);
if(!bResult)
return FALSE;
::DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES,NULL,0,NULL,
0,&junk,NULL);

if (hDevice!=INVALID_HANDLE_VALUE)
{
drv_layout_info_ex.PartitionEntry[0].PartitionStyle =
PARTITION_STYLE_MBR;
drv_layout_info_ex.PartitionEntry[0].StartingOffset.QuadPart =
0;
drv_layout_info_ex.PartitionEntry[0].PartitionLength.QuadPart
= llDiskSize;
drv_layout_info_ex.PartitionEntry[0].PartitionNumber = 1;
drv_layout_info_ex.PartitionEntry[0].RewritePartition = TRUE;
drv_layout_info_ex.PartitionEntry[0].Mbr.PartitionType =
PARTITION_FAT32;
drv_layout_info_ex.PartitionEntry[0].Mbr.BootIndicator =
FALSE;
drv_layout_info_ex.PartitionEntry[0].Mbr.RecognizedPartition =
1;

drv_layout_info_ex.PartitionEntry[0].Mbr.HiddenSectors=(32256/512);

drv_layout_info_ex.PartitionStyle = PARTITION_STYLE_MBR;
drv_layout_info_ex.PartitionCount = 1;
drv_layout_info_ex.Mbr.Signature = 0x7B060725;


bResult=::DeviceIoControl(hDevice,IOCTL_DISK_SET_DRIVE_LAYOUT_EX,
&drv_layout_info_ex,sizeof(DRIVE_LAYOUT_INFORMATION_EX),
NULL,0,
&junk,
(LPOVERLAPPED) NULL);
if( bResult )
{
::DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES, NULL,
0,NULL,0,&junk,NULL);
bResult = TRUE;
}
else
bResult = FALSE;

DelayTime(500);

dwDrv = (GetLogicalDrives() - dwDrvOrg);
*pDrive = 0;

for(n=0;n<26;n++)
{
if( (dwDrv&(1<<n))!=0 )
{
*pDrive = 'A' + n;
}
}
}

if( *pDrive == 0 )
bResult = FALSE;
return bResult;
}