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,

Re: Format a partition by Timo

Timo
Thu Aug 24 01:38:36 CDT 2006

I have a similar code but I'm using FormatVolume() instead of
FormatPartition() (pass the same handle hPar and the other arguments
required).
Platform Builder help says: "FormatPartition erases sector zero of a
specified partition; it does not actually write a file system format
such as FATFS. To write a file system format, call FormatVolume."

Regards

Long schrieb:
> 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,

Re: Format a partition by Long

Long
Fri Aug 25 02:03:01 CDT 2006

Thanks Timo. It does work that way. That really confuses me. Those calls
come in the same group, i.e. DismountPartition and FormatPartition. People
will think they are paired up. What is FormatPartition() used for then?

"Timo Kuehn" wrote:

> I have a similar code but I'm using FormatVolume() instead of
> FormatPartition() (pass the same handle hPar and the other arguments
> required).
> Platform Builder help says: "FormatPartition erases sector zero of a
> specified partition; it does not actually write a file system format
> such as FATFS. To write a file system format, call FormatVolume."
>
> Regards
>
> Long schrieb:
> > 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,
>