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

来源:互联网 发布:华为网络高级工程师 编辑:程序博客网 时间:2024/04/29 02:52
如果想在自己的应用程序中加入操作系统的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;
}

原创粉丝点击