基于WINCE平台eVC编程要点之一 : http://blog.163.com/wu_congchao/blog/static/3542647200881842717836/
本文主要介绍基于WINCE平台下eVC进行应用程序开发时会常常用到的一些功能函数,各个函数的各项参数的具体定义可以根据函数名称在eVC的帮助文件直接获取,文中就不再一一详细说明。
一、文件和目录的管理
WINCE支持了很多有用的函数来进行文件和目录的管理。常用的有以下这些函数:
1) 复制文件:
BOOL CopyFile( LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFaillfExists );
2) 删除文件:
BOOL DeletFile( LPCTSTR lpFileName );
3)创建目录:
BOOL CreateDirectory( LPCTSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes);
4) 删除目录:
BOOL RemoveDirectory( LPCTSTR lpPathName );
二、获取存储设备的大小信息
在WINCE下已经没有了驱动器名的概念,文件存储设备都是在WINCE的根目录下中以目录的形式出现,可以调用以下函数来获取存储设备的总的大小和空闲空间的信息:
BOOL GetDiskFreeSpaceEx( LPCWSTR lpDirectoryName,
PULARGE_INTEGER lpFreeBytesAvailableToCaller,
PULARGE_INTEGER lpTotalNumberOfBytes,
PULARGE_INTEGER lpTotalNumberOfFreeBytes );
三、重启系统函数
调用函数KernelIoControl( … )可以实现WINCE系统重新启动, 定义的代码如下:
#include <winioctl.h>
#define IOCTL_HAL_REBOOT CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS)
extern 'C' __declspec(dllimport) BOOL KernelIoControl( DWORD dwIoControlCode,
LPVOID lpInBuf,
DWORD nInBufSize,
LPVOID lpOutBuf,
DWORD nOutBufSize,
LPDWORD Returned);
程序需要重启时,只需调用该函数即可,如:
KernelIoControl( IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL );
得到磁盘信息:http://blog.163.com/wu_congchao/blog/static/354264720088253476730/
void enumStore()
{
STOREINFO stinfo = {0};
HANDLE hPart = INVALID_HANDLE_VALUE;
HANDLE hstore;
TCHAR szInfo[100] = _T("");
stinfo.cbSize = sizeof(STOREINFO);
hstore = FindFirstStore(&stinfo);
if(hstore!=INVALID_HANDLE_VALUE)
{
do {
RETAILMSG(1,(TEXT("Partion number:%ld /n"),stinfo.dwPartitionCount ));
Size2String(stinfo.snNumSectors * stinfo.dwBytesPerSector,szInfo,100);
RETAILMSG(1,(TEXT("enumPartion:%s szInfo=%s/n"),stinfo.szDeviceName,szInfo));
Size2String(stinfo.dwBytesPerSector, szInfo, 100);
RETAILMSG(1,(TEXT("enumPartion:%s dwBytesPerSector=%s/n"),stinfo.szDeviceName,szInfo));
enumPartion(stinfo.szDeviceName,"//Hard Disk2//");
} while(FindNextStore(hstore, &stinfo));
}
FindCloseStore(hstore);
}
LONGLONG enumPartion(LPCTSTR deviceName,CString szPartionName )
{
PARTINFO partInfo = {0};
STOREINFO stinfo = {0};
HANDLE hPart= INVALID_HANDLE_VALUE;
HANDLE hstore=INVALID_HANDLE_VALUE;
TCHAR szInfo[100] = _T("");
stinfo.cbSize = sizeof(STOREINFO);
partInfo.cbSize = sizeof(PARTINFO);
hstore = OpenStore(deviceName);
if(INVALID_HANDLE_VALUE!=hstore)
{
if(GetStoreInfo(hstore,&stinfo)==FALSE)
return 0;
hPart=FindFirstPartition(hstore, &partInfo);
if(hPart==INVALID_HANDLE_VALUE)
RETAILMSG(1,(TEXT("no find Partion info")));
if(hPart!=INVALID_HANDLE_VALUE)
{
do {
RETAILMSG(1,(TEXT("enumPartion:%s /n"),partInfo.szVolumeName));
if(szPartionName.Right(1).Compare(_T("//"))==0)
szPartionName=szPartionName.Left(szPartionName.GetLength()-1);
if(szPartionName.Compare(partInfo.szVolumeName)==0)
{
CloseHandle(hstore);
FindClosePartition(hPart);
return partInfo.snNumSectors*stinfo.dwBytesPerSector;
}
} while(FindNextPartition(hPart, &partInfo));
}
}
CloseHandle(hstore);
FindClosePartition(hPart);
return 0L;
}
//
得到磁盘空间 : http://blog.163.com/wu_congchao/blog/static/354264720088253465799/
LONGLONG GetDiskSpace(LPCWSTR disk)
{
ULARGE_INTEGER lpuse;
ULARGE_INTEGER lptotal;
ULARGE_INTEGER lpfree;
LONGLONG llsize;
TCHAR szInfo[100] = _T("");
GetDiskFreeSpaceEx(disk,&lpuse,&lptotal,&lpfree);
//RETAILMSG(1,(TEXT("disk:%s used:%ld free:%ld total:%
ld/n"),disk,lpuse.QuadPart/1024/1024,lpfree.QuadPart/1024/1024,lptotal.QuadPart/1024/1024));
Size2String(lpuse.QuadPart, szInfo, 100);
//RETAILMSG(1,(TEXT("lpuse.QuadPart=%s/n"),szInfo));
llDiskFreeSize=lpuse.QuadPart;
szDiskFreeSize.Format(_T("%s"),szInfo);
//
llDiskTotalSize=lptotal.QuadPart;
Size2String(lptotal.QuadPart, szInfo, 100);
szDiskTotalSize.Format(_T("%s"),szInfo);
//RETAILMSG(1,(TEXT("szDiskTotalSize=%s szDiskFreeSize=%s /n"),szDiskTotalSize,szDiskFreeSize ));
return (LONGLONG)lptotal.QuadPart;
}
// --------------------------------------------------------------------
BOOL Size2String(LONGLONG llBytes, LPTSTR pszSize, DWORD cLen)
// --------------------------------------------------------------------
{
double bytes = (double)llBytes;
DWORD cIter = 0;
LPCTSTR pszUnits[] = { _T("B"), _T("K"), _T("M"), _T("G"), _T("T") };
DWORD cUnits = sizeof(pszUnits) / sizeof(pszUnits[0]);
// move from bytes to kb, to mb, to gb and so on diving by 1024
while(bytes >= 1024 && cIter < (cUnits-1))
{
bytes /= 1024;
cIter++;
}
_sntprintf(pszSize, cLen, _T("%.0f%s"), bytes, pszUnits[cIter]);
return TRUE;
}
格式化硬盘 :http://blog.163.com/wu_congchao/blog/static/35426472008104210588/
格式化硬盘,如果不知道各个磁盘DEVICE_NAME和PARTITION_NAME,可以用磁盘信息查询获取。
#define DEVICE_NAME _T("DSK1:")
#define PARTITION_NAME _T("Part02")
#define DLL_NAME _T("Fatutil.dll")
typedef DWORD (*PFN_FORMAT_VOLUME)(HANDLE hVolume,PDISK_INFO pdi, PFORMAT_OPTIONS pfo, PFN_PROGRESS pfnProgress,PFN_MESSAGE pfnMessage);
CFormatDlg *FormatProgress;
void DiskFomratProgress( DWORD dwNewPercent)
{
RETAILMSG(0, (TEXT("Source Disk Formatted %d /n"),dwNewPercent));
::PostMessage(FormatProgress->GetSafeHwnd(),FORMAT_PERCENT_MESSAGE,NULL,
(LPARAM)&(dwNewPercent));
}
BOOL FormatDisk()
{
HANDLE hStore;
hStore=OpenStore(DEVICE_NAME);
if(hStore==INVALID_HANDLE_VALUE)
{
RETAILMSG(1, (TEXT("FormatDisk OpenStore error!!/n")));
return FALSE;
}
HANDLE hPart;
hPart=OpenPartition(hStore,PARTITION_NAME);
if(hPart==INVALID_HANDLE_VALUE)
{
RETAILMSG(1, (TEXT("FormatDisk OpenPartition error!!/n")));
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 szDllPath=_T("//Hard Disk//");
szDllPath+=DLL_NAME;
HINSTANCE hUtilDll = NULL;
PFN_FORMAT_VOLUME pfnFormatVolume=NULL;
hUtilDll = LoadLibrary (szDllPath);
if(hUtilDll==NULL)
{
int err=GetLastError();
RETAILMSG(1, (TEXT("FormatDisk LoadLibrary error=%d!/n"),err));
return FALSE;
}
pfnFormatVolume = (PFN_FORMAT_VOLUME)GetProcAddress(hUtilDll, TEXT("FormatVolume"));
if(pfnFormatVolume==NULL)
{
RETAILMSG(1, (TEXT("FormatDisk GetProcAddress error!!/n")));
return FALSE;
}
//must dismount the partition,or the FormatVolumeUI() failed
DismountPartition(hPart);
RETAILMSG(1, (TEXT("start format the disk.....!/n")));
// if(pfnFormatVolume(hPart,NULL,&pfo,NULL,NULL)!=ERROR_SUCCESS)
if(pfnFormatVolume(hPart,NULL,&pfo,(PFN_PROGRESS)DiskFomratProgress,NULL)!=ERROR_SUCCESS)
{
RETAILMSG(1, (TEXT("FormatDisk pfnFormatVolume error!!/n")));
return FALSE;
}
//finished format, mount partition
MountPartition(hPart);
CloseHandle(hPart);
RETAILMSG(1, (TEXT("FormatDisk ok!!/n")));
return TRUE;
}