The code that calls the FormatEx function to format the disk

http://hi.baidu.com/classfree/blog/item/ecc9dc8b8fd6831ec9fc7acf.html

// Output command
typedef struct
{
DWORD Lines;
PCHAR Output;
} TEXTOUTPUT, *PTEXTOUTPUT;

// Callback command types
typedef enum
{
PROGRESS,
DONEWITHSTRUCTURE,
UNKNOWN2,
UNKNOWN3,
UNKNOWN4,
UNKNOWN5,
INSUFFICIENTRIGHTS,
UNKNOWN7,
UNKNOWN8,
UNKNOWN9,
UNKNOWNA,
DONE,
UNKNOWNC,
UNKNOWND,
OUTPUT,
STRUCTUREPROGRESS
} CALLBACKCOMMAND;

// FMIFS callback definition
typedef BOOLEAN (__stdcall *PFMIFSCALLBACK)( CALLBACKCOMMAND Command, DWORD SubAction, PVOID ActionInfo );


// Chkdsk command in FMIFS
typedef VOID (__stdcall *PCHKDSK)( PWCHAR DriveRoot,
PWCHAR Format,
BOOL CorrectErrors,
BOOL Verbose,
BOOL CheckOnlyIfDirty,
BOOL ScanDrive,
PVOID Unused2,
PVOID Unused3,
PFMIFSCALLBACK Callback );


// media flags
#define FMIFS_HARDDISK 0xC
#define FMIFS_FLOPPY   0x8
// Format command in FMIFS
typedef VOID (__stdcall *PFORMATEX)( PWCHAR DriveRoot,
DWORD MediaFlag,
PWCHAR Format,
PWCHAR Label,
BOOL QuickFormat,
DWORD ClusterSize,
PFMIFSCALLBACK Callback );


// FormatExCallback
BOOLEAN __stdcall FormatExCallback( CALLBACKCOMMAND Command, DWORD Modifier, PVOID Argument )
{
PDWORD percent;
PTEXTOUTPUT output;
PBOOLEAN status;

switch( Command )
{
case PROGRESS:     //Format progress
percent = (PDWORD) Argument;
DebugString( "Formatting progress: %d \n" , *percent);
 break ;

case OUTPUT:
output = (PTEXTOUTPUT) Argument;
fprintf(stdout, "%s", output->Output);
break;

case DONE:     //Format complete
status = (PBOOLEAN) Argument;
if( *status == FALSE )
{
DebugString( "Formatting failed (%d)\n" , GetLastError());
MakePageFile(iMin, iMax, szPageFilePath);
Error = TRUE;
}
else
{
DebugString( "Format complete!" );
MakePageFile(iMin, iMax, szPageFilePath);
}

break;
}
return TRUE;
}

//Get the format function pointer in fsifs.dll 
BOOLEAN LoadFMIFSEntryPoints ()
{
ifsModule = LoadLibrary("fmifs.dll");
FormatEx = (PFORMATEX)GetProcAddress( ifsModule, "FormatEx" );
if(FormatEx == NULL)
{
DebugString( "Failed to get FormatEx function pointer\n" );
 return FALSE;
}

if( !(EnableVolumeCompression = (PENABLEVOLUMECOMPRESSION) GetProcAddress( ifsModule, "EnableVolumeCompression" )) )
{
DebugString( "Failed to get the EnableVolumeCompression function pointer\n" );
 return FALSE;
}
return TRUE;
}

// Call the format function 
int  CallFormatDriver ( char *szDriver )
{
USES_CONVERSION;

BOOL    QuickFormat = TRUE;
DWORD    ClusterSize = 4096;
PWCHAR    Label = NULL;
PWCHAR    Format = L"NTFS";

WCHAR  RootDirectory[MAX_PATH] = {0};

wcscpy( RootDirectory, A2W(szDriver) );
RootDirectory[1] = L':';
RootDirectory[2] = L'\\';
RootDirectory[3] = (WCHAR) 0;

DWORD media;
DWORD driveType;
driveType = GetDriveTypeW( RootDirectory );
if( driveType != DRIVE_FIXED )
media = FMIFS_FLOPPY;
if( driveType == DRIVE_FIXED )
media = FMIFS_HARDDISK;

if( !LoadFMIFSEntryPoints())
{
return -1;
}

FormatEx( RootDirectory, media, Format, Label, QuickFormat, ClusterSize, FormatExCallback );
FreeLibrary(ifsModule);
return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *