格式化硬盘

格式化硬盘,如果不知道各个磁盘DEVICE_NAME和PARTITION_NAME,可以用磁盘信息查询获取。

 #define DEVICE_NAME _T("DSK1:") #define PARTITION_NAME _T("Part02") #define DLL_NAME _T("Fatutil.dll") typedef DWORD (*PFN_FORMAT_VOLUME)(HANDLE hVolume,PDISK_INFO pdi, PFORMAT_OPTIONS pfo, PFN_PROGRESS pfnProgress,PFN_MESSAGE pfnMessage); CFormatDlg *FormatProgress; void DiskFomratProgress( DWORD dwNewPercent) { RETAILMSG(0, (TEXT("Source Disk Formatted %d /n"),dwNewPercent)); ::PostMessage(FormatProgress->GetSafeHwnd(),FORMAT_PERCENT_MESSAGE,NULL,(LPARAM)&(dwNewPercent)); } BOOL FormatDisk() { HANDLE hStore; hStore=OpenStore(DEVICE_NAME); if(hStore==INVALID_HANDLE_VALUE) { RETAILMSG(1, (TEXT("FormatDisk OpenStore error!!/n"))); return FALSE; } HANDLE hPart; hPart=OpenPartition(hStore,PARTITION_NAME); if(hPart==INVALID_HANDLE_VALUE) { RETAILMSG(1, (TEXT("FormatDisk OpenPartition error!!/n"))); return FALSE; } CloseHandle(hStore); FORMAT_OPTIONS pfo; //if dwClusSize set to 0 ,the FormatValume() in the dll file will caculate; //also you could set to 128*512; pfo.dwClusSize=0; pfo.dwRootEntries=512; pfo.dwFatVersion=16; pfo.dwNumFats=2; //Careful: Don't set to FATUTIL_FORMAT_TFAT,or it will creat a hidden folder, //and all of the files, which be copied to harddisk by ActiveSync, are there, //so you could not lauch the OS! pfo.dwFlags=FATUTIL_FULL_FORMAT; CString szDllPath=_T("//Hard Disk//"); szDllPath+=DLL_NAME; HINSTANCE hUtilDll = NULL; PFN_FORMAT_VOLUME pfnFormatVolume=NULL; hUtilDll = LoadLibrary (szDllPath); if(hUtilDll==NULL) { int err=GetLastError(); RETAILMSG(1, (TEXT("FormatDisk LoadLibrary error=%d!/n"),err)); return FALSE; } pfnFormatVolume = (PFN_FORMAT_VOLUME)GetProcAddress(hUtilDll, TEXT("FormatVolume")); if(pfnFormatVolume==NULL) { RETAILMSG(1, (TEXT("FormatDisk GetProcAddress error!!/n"))); return FALSE; } //must dismount the partition,or the FormatVolumeUI() failed DismountPartition(hPart); RETAILMSG(1, (TEXT("start format the disk.....!/n"))); // if(pfnFormatVolume(hPart,NULL,&pfo,NULL,NULL)!=ERROR_SUCCESS) if(pfnFormatVolume(hPart,NULL,&pfo,(PFN_PROGRESS)DiskFomratProgress,NULL)!=ERROR_SUCCESS) { RETAILMSG(1, (TEXT("FormatDisk pfnFormatVolume error!!/n"))); return FALSE; } //finished format, mount partition MountPartition(hPart); CloseHandle(hPart); RETAILMSG(1, (TEXT("FormatDisk ok!!/n"))); return TRUE; }

你可能感兴趣的:(File,OS,null,dll,disk,磁盘)