您的位置:首页 > 其它

利用windows未公开API,实现format函数

2010-12-31 12:50 771 查看
Windows的格式化是利用fmisf.dll实现的,下面代码描述了如何利用该dll实现格式话功能

typedef VOID (__stdcall *PFORMATEX)( PWCHAR DriveRoot,
DWORD MediaFlag,
PWCHAR Format,
PWCHAR Label,
BOOL QuickFormat,
DWORD ClusterSize,
PFMIFSCALLBACK Callback

);
PFORMATEX FormatEx;

//回调函数实现

BOOLEAN __stdcall FormatExCallback( CALLBACKCOMMAND Command, DWORD Modifier, PVOID Argument )
{
double percent;
PTEXTOUTPUT output;
// PBOOLEAN status;
static BOOL createStructures = FALSE;

//
// We get other types of commands, but we don't have to pay attention to them
//
switch( Command )
{

case PROGRESS:
percent = (double)*(PDWORD) Argument;
break;

case OUTPUT:
output = (PTEXTOUTPUT) Argument;

break;

case DONE:
FormatExResult = *(BOOLEAN *) Argument;
if ( FormatExResult == TRUE )
{
printf("here\n");
}
}
return TRUE;
}

HMODULE LoadFMIFSEntryPoints()
{
  HMODULE hModule;
   hModule = LoadLibrary( _T("fmifs.dll") );
   if(hModule == NULL)
     return NULL;
   if( !(FormatEx = (PFORMATEX) GetProcAddress( hModule,
       "FormatEx" )) ) {
     return NULL;
   }

   return hModule;
}

int FormatNtfs (int driveNo)
{

HMODULE hModule;
WCHAR dir[8] = { (WCHAR) driveNo + 'A', 0 };
// DWORD media;
// DWORD driveType;
BOOL Error = FALSE;

BOOL QuickFormat = TRUE;
DWORD ClusterSize = 16*512;
BOOL CompressDrive = FALSE;
BOOL GotALabel = FALSE;
PWCHAR Label = L"temp";

CHAR Drive = NULL;
// WCHAR LabelString[12];

//
// Get function pointers
//
hModule = LoadFMIFSEntryPoints();
if( !hModule) {
return LOADFORMATLIB_ERROR;
}
wcscat (dir, L":\\");

//////////////++++++++
//发现调用SHBrosweForFolder后没有LFN的问题,想到是否是Shell的状态,下面代码来至于
//Shell32/unicode/format.c
HRESULT hr = S_OK;
LPITEMIDLIST pidlFormat;
SHILCreateFromPath(dir, &pidlFormat, NULL);
if (pidlFormat) {
SHChangeNotify(SHCNE_MEDIAREMOVED, SHCNF_IDLIST | SHCNF_FLUSH, pidlFormat, 0);
//SHChangeNotifySuspendResume(TRUE, pidlFormat, TRUE, 0);
}
///////////////

FormatExResult = FALSE;
FormatBeExit = FALSE;
for (int i = 0; i < 15 && FormatExResult != TRUE; i++)
{
FormatEx(dir, FMIFS_HARDDISK, L"NTFS", Label, QuickFormat,
ClusterSize, FormatExCallback );
if(FormatExResult != TRUE){
Sleep (500);
}
}

/////////////+++++++++
if (pidlFormat) {
//SHChangeNotifySuspendResume(FALSE, pidlFormat, TRUE, 0);
ILFree(pidlFormat);
}
// See next comment,Shell32.65
//InvalidateDriveType(drive);
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATHW, (LPVOID) dir, NULL);
/////////////

if(FormatExResult != TRUE)
   return FORMAT_ERROR;
  return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: