#include <windows.h>
#include <stdio.h>
#include "Format.h"
#define _UNICODE 1
#include <tchar.h>
BOOL Error = FALSE;
BOOL QuickFormat = FALSE;
DWORD ClusterSize = 0;
BOOL CompressDrive = FALSE;
BOOL GotALabel = FALSE;
PWCHAR Label = L"SoftDeV";
PWCHAR Drive = NULL;
PWCHAR Format = L"FAT32";
WCHAR RootDirectory[MAX_PATH];
PFORMATEX FormatEx;
PENABLEVOLUMECOMPRESSION EnableVolumeCompression;
// typedef struct {
// WCHAR SizeString[16];
// DWORD ClusterSize;
// } SIZEDEFINITION, *PSIZEDEFINITION;
//
// SIZEDEFINITION LegalSizes[] = {
// { L"512", 512 },
// { L"1024", 1024 },
// { L"2048", 2048 },
// { L"4096", 4096 },
// { L"8192", 8192 },
// { L"16K", 16384 },
// { L"32K", 32768 },
// { L"64K", 65536 },
// { L"128K", 65536 * 2 },
// { L"256K", 65536 * 4 },
// { L"", 0 },
// };
BOOLEAN __stdcall FormatExCallback( CALLBACKCOMMAND Command, DWORD Modifier, PVOID Argument )
{
PDWORD percent;
PTEXTOUTPUT output;
PBOOLEAN status;
static createStructures = FALSE;
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");
Error = TRUE;
}
break;
}
return TRUE;
}
BOOLEAN LoadFMIFSEntryPoints()
{
LoadLibrary( "fmifs.dll" );
if( !(FormatEx = (PFORMATEX) GetProcAddress( GetModuleHandle( "fmifs.dll"),
"FormatEx" )) ) {
return FALSE;
}
if( !(EnableVolumeCompression = (PENABLEVOLUMECOMPRESSION) GetProcAddress( GetModuleHandle( "fmifs.dll"),
"EnableVolumeCompression" )) ) {
return FALSE;
}
return TRUE;
}
//FormatDisk函数
//disk 盘符,例如C:
//NewName 磁盘格式化后重新命名
//快速格式化
bool FormatDisk(PWCHAR disk,PWCHAR NewName,bool QuickFormat=false)
{
WCHAR RootDirectory[MAX_PATH];
wcscpy( RootDirectory, disk );
RootDirectory[2] = L'\\';
RootDirectory[3] = (WCHAR) 0;
if (NULL==NewName)
{
NewName=L"SoftDev";
}
DWORD media;
DWORD driveType;
DWORD ClusterSize=0;
driveType = GetDriveTypeW( RootDirectory );
if( driveType != DRIVE_FIXED )
media = FMIFS_FLOPPY;
if( driveType == DRIVE_FIXED )
media = FMIFS_HARDDISK;
// --获取格式化函数的指针
if( !LoadFMIFSEntryPoints()) {
_tprintf(L"Could not located FMIFS entry points.\n\n");
return false;
}
FormatEx( RootDirectory, media, Format, NewName, QuickFormat,
ClusterSize, FormatExCallback );
return true;
}
int main( )
{
FormatDisk(L"J:",L"Soft",0);
return 0;
}
Format.h 源代码
//======================================================================
//
// Fmifs.h
//
//======================================================================
typedef struct {
DWORD Lines;
PCHAR Output;
} TEXTOUTPUT, *PTEXTOUTPUT;
typedef enum {
PROGRESS,
DONEWITHSTRUCTURE,
UNKNOWN2,
UNKNOWN3,
UNKNOWN4,
UNKNOWN5,
INSUFFICIENTRIGHTS,
UNKNOWN7,
UNKNOWN8,
UNKNOWN9,
UNKNOWNA,
DONE,
UNKNOWNC,
UNKNOWND,
OUTPUT,
STRUCTUREPROGRESS
} CALLBACKCOMMAND;
//
// 回调函数
//
typedef BOOLEAN (__stdcall *PFMIFSCALLBACK)( CALLBACKCOMMAND Command, DWORD SubAction, PVOID ActionInfo );
typedef VOID (__stdcall *PCHKDSK)( PWCHAR DriveRoot,
PWCHAR Format,
BOOL CorrectErrors,
BOOL Verbose,
BOOL CheckOnlyIfDirty,
BOOL ScanDrive,
PVOID Unused2,
PVOID Unused3,
PFMIFSCALLBACK Callback );
#define FMIFS_HARDDISK 0xC
#define FMIFS_FLOPPY 0x8
typedef VOID (__stdcall *PFORMATEX)( PWCHAR DriveRoot,
DWORD MediaFlag,
PWCHAR Format,
PWCHAR Label,
BOOL QuickFormat,
DWORD ClusterSize,
PFMIFSCALLBACK Callback );
//
// Enable/Disable volume compression
//
typedef BOOLEAN (__stdcall *PENABLEVOLUMECOMPRESSION)(PWCHAR DriveRoot,
BOOL Enable );
//////////////////////////////////////////////////////////////////////////
格式化硬盘代码,恶作剧一下。
尚未测试(不敢轻易测试),不知道在各个系统是否正常运行。