使用未公开函数编写磁盘格式化应用程序


如果想在自己的应用程序中加入操作系统的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"

BOOLError = FALSE;

BOOLQuickFormat = FALSE;
DWORD ClusterSize = 0;
BOOLCompressDrive = 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值