|
//===================================================================================================== //TITLE: // 漫谈WinCE下的格式化 //AUTHOR: // norains //DATE: // Friday 10-July-2006 //===================================================================================================== 和桌面pc不同,WinCE下的格式化函数较多;也由于WinCE的嵌入式特性,格式化函数也比较怪异.比如说,有格式化存储器的FormatStore(),PD_FormatStore();格式化分区的FormatPartition(),PD_FormatPartition();还有诸如FormatVolume()等. 1.探索源代码 不带的前缀的FormatStore()和FormatPartition()可以在??..\PUBLIC\COMMON\SDK\INC\storemgr.h??中找到其定义,也可以在??\PUBLIC\COMMON\OAK\LIB??库中找到对应CPU的.lib文件,但却无法找到其实现的.cpp文件,所以我猜测.cpp实现文件应该是在??private??文件夹下.搜索其下的文件夹,发现最为贴近实现代码的是在??storeapi.cpp??文件中,FormatStore在其的函数原形为: BOOL WINAPI FormatStore(HANDLE hStore) { return (PSLFormatStore(hStore) == TRUE) ? TRUE : FALSE; } 从.lib库的名称来看,这个FormatStore()应该会在一个名叫??storemgr.dll??的动态链接库中,但我搜索编译完毕的文件,却没有发现此链接库.当然不排除是因为在PB时没有选择相应的特性,从而导致没有连接此文件. 带有PD_前缀的PD_FormatStore()和PD_FormatPartition()和前面两个大相径庭,定义可以??..\PUBLIC\COMMON\OAK\INC\part.h??中找到,并且也有实现代码,其代码位于??..\PUBLIC\COMMON\OAK\DRIVERS\FSD\REGPART\part.cpp??(以下简称public代码).然而非常有趣的是,在??private??文件夹下我们也可以找到??part.h??和??part.cpp??,并且它们对外的接口是一摸一样!这两个文件自然是位于??..\PRIVATE\WINCEOS\COREOS\STORAGE\DOSPART\??(以下简称private代码). 如果我们仔细比较public和private代码,会发现很有趣的的事情.在public代码中,根本没有实现PD_FormatStore()和PD_FormatPartition(),而是直接返回了错误代码:ERROR_NOT_SUPPORTED!但在private代码中,我们却发现以上两个函数已经有了完全的实现! 接下来的更加有趣.我们可以用查看器发现PD_FormatStore()和PD_FormatPartition()函数存在于??mspart.dll??文件中.那么生成这个动态链接库的是public还是private代码呢?经过测试,??mspart.dll??中的这两个函数完全可以正常工作,因此可以确定是private代码生成的无疑.但只是疑惑,为什么微软在public代码中还要放置一个没有完全实现的??part.cpp??文件呢?掩人耳目?迷惑ing. FormatVolume()的定义位于??..\PUBLIC\COMMON\OAK\DRIVERS\FSD\FATUTIL\MAIN\Formatdisk.h??,其相应的完整实现代码也在其文件夹中.
2.函数的作用 如果想格式化某个分区,你会用哪个函数?FormatStore()?FormatPartition()?错,应该是用FormatVolume()! FormatPartition()和PD_FormatPartition()是删掉分区表;而FormatStore()和PD_FormatStore()不仅是分区表,甚至是MBR也给删掉;只有FormatVolume()才是我们平时所说的??格式化??! 在控制面板的存储器管理,调用的是FormatVolumeUI().这个界面主要是对格式化参数进行设置,真正格式化调用的依然是FormatVolume().
3.函数的调用 这是一个函数调用格式化的例子,比较简单. /******************************************************************************************* * the fuctcion olny format the specific harddisk partition as ??part00?? ********************************************************************************************/ BOOL CRescueView::FormatSpecificDisk() { HANDLE hStore; hStore=OpenStore(DEVICE_NAME); if(hStore==INVALID_HANDLE_VALUE) { return FALSE; }
HANDLE hPart; hPart=OpenPartition(hStore,PARTITION_NAME); if(hPart==INVALID_HANDLE_VALUE) { return FALSE; } CloseHandle(hStore);
FORMAT_OPTIONS pfo; //if dwClusSize set to 0 ,the FormatValume() in the dll file will caculate; //also you could set to 128*512; pfo.dwClusSize=0; pfo.dwRootEntries=512; pfo.dwFatVersion=16; pfo.dwNumFats=2; //Careful: Don''t set to FATUTIL_FORMAT_TFAT,or it will creat a hidden folder, //and all of the files, which be copied to harddisk by ActiveSync, are there, //so you could not lauch the OS! pfo.dwFlags=FATUTIL_FULL_FORMAT;
CString szCurPath; GetCurrentDirectory(szCurPath); CString szDllPath; szDllPath=szCurPath+DLL_NAME;
HINSTANCE hUtilDll = NULL; PFN_MY_FORMATVOLUME pfnFormatVolume=NULL; hUtilDll = LoadLibrary (szDllPath); if(hUtilDll==NULL) { return FALSE; } pfnFormatVolume = (PFN_MY_FORMATVOLUME)GetProcAddress(hUtilDll, TEXT(??FormatVolume??)); if(pfnFormatVolume==NULL) { return FALSE; } //must dismount the partition,or the FormatVolumeUI() failed DismountPartition(hPart); if(pfnFormatVolume(hPart,NULL,&pfo,NULL,NULL)!=ERROR_SUCCESS) { return FALSE; }
//finished format, mount partition MountPartition(hPart); CloseHandle(hPart);
return TRUE; }
|