I use below steps to create new logical disk, but something
wrong.
(1)use DeviceIoControl:IOCTL_DISK_GET_DRIVE_GEOMETRY to
get physical disk size and Bytes per Sector in disk.
(2)use DeviceIoControl:IOCTL_DISK_CREATE_DISK、
DeviceIoControl:IOCTL_DISK_UPDATE_PROPERTIES、
DeviceIoControl:IOCTL_DISK_SET_DRIVE_LAYOUT_EX to
partition disk.( below code )
(3) after partitioning, use "format.com" in WinXP to
format disk.
But I found the step for partition can fail. I can't
use NTFS format to parition and if I use FAT32 format
to do, it would happen wrong partition disk to reboot
pc. For example, in one physical disk:30G, after
partitioning, it will shows 3 parts(27.5G,2G,27.G)
and no logical drivers created.
Could anyone tell me what's wrong with my code and
how to modify it? And How to improve the speed of
format?
//-----------------------------------------------------------------------------------------//
BOOL bResult;
DWORD junk;
DRIVE_LAYOUT_INFORMATION_EX drv_layout_info_ex;
CREATE_DISK disk;
INT n;
ZeroMemory(&disk,sizeof(CREATE_DISK));
disk.PartitionStyle = PARTITION_STYLE_MBR;
disk.Mbr.Signature = 0x7B060725;
bResult = ::DeviceIoControl(hDevice,IOCTL_DISK_CREATE_DISK,&disk,
sizeof(CREATE_DISK),NULL,
0,&junk,NULL);
if(!bResult)
return FALSE;
bResult
= ::DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES,NULL,0,NULL,
0,&junk,NULL);
if( bResult )
{
if (hDevice!=INVALID_HANDLE_VALUE)
{
ZeroMemory(&drv_layout_info_ex,sizeof(DRIVE_LAYOUT_INFORMATION_EX));
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 =
llPartitionDiskSize;
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/
ulBytesPerSector);
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 )
{
bResult
= ::DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES,
NULL,0,NULL,
0,&junk,NULL);
}
if( bResult )
{
dwDrv = (GetLogicalDrives() - dwDrvOrg);
*pDrive = 0;
for(n=0;n<26;n++)
{
if( (dwDrv&(1<<n))!=0 )
{
*pDrive = 'A' + n;
}
}
}//if( bResult )
}
if( (*pDrive == 0) || (*pDrive <= 'C') )
bResult = FALSE;
}
//-----------------------------------------------------------------------------------------//
Boris
"bluestar" <bluest...@gmail.com> wrote in message
news:0cb7a59f-ded6-4de6...@t1g2000pra.googlegroups.com...
I can't use other tools to create partition and format.
I need to both procedures in my code.
I also try to use below code to format, and it failed when
running command FormatEx.
What's wrong with this code?
//---------------------------------------------------------------------------------------//
TCHAR strWinSysDir[MAX_PATH],szDest[MAX_PATH];
HMODULE hK32lib;
PWCHAR Label = L"";
PWCHAR Format = L"FAT32";
BOOL bResult = TRUE;
GetSystemDirectory(strWinSysDir,MAX_PATH);//get c:\Windows\System32
_tcscpy_s (szDest,MAX_PATH,strWinSysDir);
_tcscat_s(szDest, MAX_PATH, _T("\\fmifs.dll"));
hK32lib = LoadLibrary( szDest );
if( !(FormatEx = (PFORMATEX)
GetProcAddress( GetModuleHandle( "fmifs.dll"),"FormatEx" )) ) {
return FALSE;
}
FormatEx((PWCHAR)cDrive, FMIFS_HARDDISK, Format, Label, TRUE,
512, FormatExCallback);
FreeLibrary(hK32lib);
BOOLEAN __stdcall FormatExCallback( CALLBACKCOMMAND Command, DWORD
Modifier, PVOID Argument )
{
PDWORD percent;
PTEXTOUTPUT output;
PBOOLEAN status;
switch( Command ) {
case PROGRESS:
percent = (PDWORD) Argument;
//_tprintf(L"%d percent completed.\r", *percent);
break;
case OUTPUT:
output = (PTEXTOUTPUT) Argument;
//fprintf(stdout, "%s", output->Output);
break;
case DONE:
status = (PBOOLEAN) Argument;
if( *status == FALSE ) {
//_tprintf(L"FormatEx was unable to complete successfully.\n\n");
}
break;
}
return TRUE;
}
//---------------------------------------------------------------------------------------//
I tried to modify code to do(below code),
but I don't know where is wrong.
Does anyone help me????
//----------------------------------------------------------------------------------------------------------//
ZeroMemory(&disk,sizeof(CREATE_DISK));
disk.PartitionStyle = PARTITION_STYLE_MBR;
// Create primary partition MBR
bResult
= ::DeviceIoControl(hDevice,IOCTL_DISK_CREATE_DISK,&disk,sizeof(CREATE_DISK),NULL,
0,&junk,NULL);
if(!bResult)
return FALSE;
bResult = ::DeviceIoControl((HANDLE) hDevice,
IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
NULL, 0, (LPVOID) temp, (DWORD) 4096, (LPDWORD) &junk, NULL);
if(!bResult)
return FALSE;
DRIVE_LAYOUT_INFORMATION_EX *ptr = (DRIVE_LAYOUT_INFORMATION_EX
*)temp;
if (hDevice!=INVALID_HANDLE_VALUE)
{
ZeroMemory(&drv_layout_info_ex,sizeof(DRIVE_LAYOUT_INFORMATION_EX));
ZeroMemory(&arraypartinfn_x[0],sizeof(PARTITION_INFORMATION_EX));
arraypartinfn_x[0].PartitionNumber = 1;
arraypartinfn_x[0].RewritePartition = true;
arraypartinfn_x[0].StartingOffset.QuadPart = 0;
arraypartinfn_x[0].PartitionLength.QuadPart = llPartitionDiskSize;
arraypartinfn_x[0].Mbr.BootIndicator = false;
arraypartinfn_x[0].Mbr.RecognizedPartition = true;
arraypartinfn_x[0].Mbr.HiddenSectors = 1;//(32256/ulBytesPerSector);
arraypartinfn_x[0].Mbr.PartitionType = PARTITION_FAT32;
arraypartinfn_x[0].RewritePartition =1;
arraypartinfn_x[0].PartitionStyle = PARTITION_STYLE_MBR;
drv_layout_info_ex.PartitionCount = 1;
drv_layout_info_ex.PartitionEntry[0] = arraypartinfn_x[0];
drv_layout_info_ex.PartitionStyle = PARTITION_STYLE_MBR;
DRIVE_LAYOUT_INFORMATION_MBR mbrlayout;
mbrlayout.Signature = ptr->Mbr.Signature;
drv_layout_info_ex.Mbr = mbrlayout;
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)
return FALSE;
VERIFY_INFORMATION verify;
verify.Length = (DWORD)llPartitionDiskSize;
verify.StartingOffset.QuadPart = 0;
bResult = ::DeviceIoControl((HANDLE) hDevice, IOCTL_DISK_VERIFY,
(LPVOID) &verify,
(DWORD) sizeof(VERIFY_INFORMATION), NULL, 0, (LPDWORD) &junk,
(LPOVERLAPPED) NULL);
if(!bResult)
return FALSE;
bResult = ::DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES,
NULL,0,NULL,0,&junk,NULL);
if( !bResult )
{
bResult = ::DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES,
NULL,0,NULL,0,&junk,NULL);
}
}