如果想在自己的應用程式中加入作業系統的Format(格式化)功能,就象系統的那個format utility,應該怎麼做呢?用Google搜尋的話,中文資料最多的一篇是《磁碟格式化的編程實現》,講的是借用SHFormatDrive函數來調用系統的格式化工具完成格式化功能。有沒有辦法使用自己的代碼實現呢?參考sysinternals裡的一篇文章,嘗試用FMIFS.dll裡的一個未公開函數實現了Format功能,所有的參數都由自己給定,比如每簇大小、檔案系統等待。這裡我只示範了最基本的功能,如果要想程式更健壯些,則還需要做更多的工作。
FormatEx函數原型:
VOID FormatEx ( PWCHAR DriveRoot,
DWORD MediaFlag ,
PWCHAR Format,
PWCHAR Label,
BOOL QuickFormat,
DWORD ClusterSize,
PFMIFSCALLBACK Callback);
其中FMIFSCALLBACK是:
typedef BOOLEAN (__stdcall *PFMIFSCALLBACK)(
CALLBACKCOMMAND Command,
DWORD SubAction,
PVOID ActionInfo );
下面是具體工作:
先定義一個標頭檔Fmifs.h:
//======================================================================
//
// Fmifs.h
//
//======================================================================
//
// 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 );
//
// Format command in FMIFS
//
// media flags
#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 );
//////////////////////////////////////////////////////////////////////////
下面是主檔案:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "fmifs.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"";
PWCHAR Drive = NULL;
PWCHAR Format = L"FAT32";
WCHAR RootDirectory[MAX_PATH];
WCHAR LabelString[12];
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 },
};
//----------------------------------------------------------------------
//
// FormatExCallback
//
// The file system library will call us back with commands that we
// can interpret. If we wanted to halt the chkdsk we could return FALSE.
//
//----------------------------------------------------------------------
BOOLEAN __stdcall FormatExCallback( CALLBACKCOMMAND Command, DWORD Modifier, PVOID Argument )
{
PDWORD percent;
PTEXTOUTPUT output;
PBOOLEAN status;
static createStructures = FALSE;
//
// We get other types of commands, but we don't have to pay attention to them
//
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;
}
//----------------------------------------------------------------------
//
// LoadFMIFSEntryPoints
//
// Loads FMIFS.DLL and locates the entry point(s) we are going to use
//
//----------------------------------------------------------------------
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;
}
int main( int argc, WCHAR *argv[] )
{
if( argv[1][1] != L':' ) return 0;
Drive = argv[1];
wcscpy( RootDirectory, Drive );
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;
//
// Get function pointers
//
if( !LoadFMIFSEntryPoints()) {
_tprintf(L"Could not located FMIFS entry points./n/n");
return -1;
}
FormatEx( RootDirectory, media, Format, Label, QuickFormat,
ClusterSize, FormatExCallback );
return 0;
}