I am trying to make a program to format a drive or partition. But the call
to FormatPartition() is always failed. Can someone take a look at my code
following and tell me what I did wrong? What I did here is to find a
partition matching "szDrive" and then format it. Thanks.
void FormatDisk(TCHAR* szDrive)
{
STOREINFO si;
si.cbSize = sizeof(STOREINFO);
HANDLE hss = FindFirstStore(&si);
if(hss == INVALID_HANDLE_VALUE)
{
printf("FindFirstStore failed.\n");
return;
}
do
{
wprintf(L"%s, %s, %s.\n", si.szDeviceName, si.szStoreName,
si.sdi.szProfile);
HANDLE hStore = OpenStore(si.szDeviceName);
if(hStore != INVALID_HANDLE_VALUE)
{
PARTINFO pi;
pi.cbSize = sizeof(PARTINFO);
HANDLE hsp = FindFirstPartition(hStore, &pi);
if(hsp != INVALID_HANDLE_VALUE)
{
do
{
wprintf(L"%s, %s.\n", pi.szPartitionName, pi.szVolumeName);
HANDLE hPar = OpenPartition(hStore, pi.szPartitionName);
if(hPar != INVALID_HANDLE_VALUE)
{
if(_wcsicmp(szDrive, pi.szVolumeName) == 0)
{
wprintf(L"DismountPartition: %d.\n", DismountPartition(hPar));
//Always return false
wprintf(L"FormatPartition: %d.\n", FormatPartition(hPar));
wprintf(L"MountPartion: %d.\n", MountPartition(hPar));
}
CloseHandle(hPar);
}
}
while(FindNextPartition(hsp, &pi));
FindClosePartition(hsp);
}
}
CloseHandle(hStore);
wprintf(L"-----------------------------\n");
}
while(FindNextStore(hss, &si));
FindCloseStore(hss);
}
Best regards,