對一個文件使用內存映射文件
發表于:2007-07-14來源:作者:點擊數:
標簽:
Demo: 1:創建或打開一個文件內核對象: // Open the file for reading and writing. HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); // 由于hFile即使為INVALID_HANDLE_VALUE
Demo:
1:創建或打開一個文件內核對象:
// Open the file for reading and writing.
HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// 由于hFile即使為INVALID_HANDLE_VALUE,下面的CreateFileMapping仍然可以正常運行,
// 所以這里一定要對hFile進行檢查!
if (hFile == INVALID_HANDLE_VALUE) {
chMB("File could not be opened.");
return(FALSE);
}
2:創建一個文件映射內核對象:
// Get the size of the file (I assume the whole file can be mapped).
DWORD dwFileSize = GetFileSize(hFile, NULL);
// Create the file-mapping object. The file-mapping object is 1 character
// bigger than the file size so that a zero character can be placed at the
// end of the file to terminate the string (file). Because I don't yet know
// if the file contains ANSI or Unicode characters, I assume worst case
// and add the size of a WCHAR instead of CHAR.
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE,
0,
dwFileSize + sizeof(WCHAR), // 如果該文件小于設定的大小,本函數將擴展該文件的大小,
// 使磁盤上的文件變大。這樣當以后將該文件作為內存映射
// 文件使用時,物理存儲器就已經存在了。
NULL f// 這個文件映射對象的名字用于與其他進程共享該對象,這里我們還用不到。
);
if (hFileMap == NULL) {
chMB("File map could not be opened.");
CloseHandle(hFile);
return(FALSE);
}
3:將文件數據映射到進程的地址空間:
當創建了一個文件映射對象之后,仍然必須讓系統為文件的數據保留一個地址空間區域,
并將文件的數據作為映射到該區域的物理存儲器進行提交。
// Get the address where the first byte of the file is mapped into memory.
// the return value is the starting address of the mapped view:
PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
if (pvFile == NULL) {
chMB("Could not map view of file.");
CloseHandle(hFileMap);
CloseHandle(hFile);
return(FALSE);
}
4:既然我們通過pvFile得到了映象視圖的起始地址,那么可以對視圖做一些操作了:
ANSI版本:
PSTR pchANSI = (PSTR) pvFile;
UNICODE版本:
PWSTR pchUnicode = (PWSTR) pvFile;
5:從進程的地址空間中撤銷文件數據的映象:
// Clean up everything before exiting.
UnmapViewOfFile(pvFile);
6:關閉文件映射對象和文件對象:
CloseHandle(hFileMap);
CloseHandle(hFile);
Definition:
HANDLE CreateFileMapping(
HANDLE hFile, // handle to file
LPSECURITY_ATTRIBUTES lpAttributes, // security
DWORD flProtect, // protection
DWORD dwMaximumSizeHigh, // high-order DWORD of size
DWORD dwMaximumSizeLow, // low-order DWORD of size
LPCTSTR lpName // object name
);
LPVOID MapViewOfFile(
HANDLE hFileMappingObject, // handle to file-mapping object
DWORD dwDesiredA
clearcase/" target="_blank" >ccess, // access mode
DWORD dwFileOffsetHigh, // high-order DWORD of offset
DWORD dwFileOffsetLow, // low-order DWORD of offset
SIZE_T dwNumberOfBytesToMap // number of bytes to map
);
BOOL UnmapViewOfFile(
LPCVOID lpBaseAddress // starting address
);
Tips:
也可以盡量早地把對象關閉,以消除資源泄漏的可能性,如:
HANDLE hFile = CreateFile(...);
HANDLE hFileMapping = CreateFileMapping(hFile,...);
CloseHandle(hFile);
PVOID pvFile = MapViewOfFile(hFileMapping,...);
CloseHandle(hFileMapping);
// use the memory-mapped file.
UnmapViewOfFile(pvFile);
原文轉自:http://www.anti-gravitydesign.com