CreateFileMapping_1
来源:互联网 发布:淘宝购物车营销 编辑:程序博客网 时间:2024/06/06 17:07
#include <Windows.h>#include <tchar.h>int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE, PTSTR pszCmdLine, int nCmdShow){ // Before executing the line below, C:\ does not have a file called "test.dat" // 失败后返回INVALID_HANDLE_VALUE(定义为-1), HANDLE hFile = CreateFile(_T("C:\\test.dat"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // Before executing the line below, the test.dat file does exist but has a file size of 0 bytes. // 失败后返回NULL HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 100, NULL); // After executing the line above, the test.dat file has a size of 100 bytes. // cleanup CloseHandle(hFileMap); CloseHandle(hFile); // code1 //HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); //DWORD dwFileSizeHigh; //__int64 qwFileSize = GetFileSize(hFile, &dwFileSizeHigh); //char addMsg[] = { "test" }; //__int64 myFileSize = qwFileSize + sinf.dwAllocationGranularity; //HANDLE hMyFileMap = CreateFileMapping(hmyFile, NULL, PAGE_READWRITE, //(DWORD)(myFileSize >> 32), (DWORD)(myFileSize & 0xffffffff), NULL); // When the process terminates, test.dat remians on the disk with a size of 100 bytes. return 0;}