全部博文(346)
分类: WINDOWS
2008-08-21 13:06:25
HANDLE CreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile); |
HANDLE CreateFileMapping(HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpName); |
LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, DWORD dwNumberOfBytesToMap); |
SYSTEM_INFO sinf; GetSystemInfo(&sinf); DWORD dwAllocationGranularity = sinf.dwAllocationGranularity; |
BOOL UnmapViewOfFile(LPCVOID lpBaseAddress); |
…… // 创建文件内核对象,其句柄保存于hFile HANDLE hFile = CreateFile("Recv1.zip", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL); // 创建文件映射内核对象,句柄保存于hFileMapping HANDLE hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READWRITE, 0, 0x4000000, NULL); // 释放文件内核对象 CloseHandle(hFile); // 设定大小、偏移量等参数 __int64 qwFileSize = 0x4000000; __int64 qwFileOffset = 0; __int64 T = 600 * sinf.dwAllocationGranularity; DWORD dwBytesInBlock = 1000 * sinf.dwAllocationGranularity; // 将文件数据映射到进程的地址空间 PBYTE pbFile = (PBYTE)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, (DWORD)(qwFileOffset>>32), (DWORD)(qwFileOffset&0xFFFFFFFF), dwBytesInBlock); while(bLoop) { // 捕获事件hEvent[0]和事件hEvent[1] DWORD ret = WaitForMultipleObjects(2, hEvent, FALSE, INFINITE); ret -= WAIT_OBJECT_0; switch (ret) { // 接收数据事件触发 case 0: // 从端口接收数据并保存到内存映射文件 nReadLen=syio_Read(port[1], pbFile + qwFileOffset, QueueLen); qwFileOffset += nReadLen; // 当数据写满60%时,为防数据溢出,需要在其后开辟一新的映射视图 if (qwFileOffset > T) { T = qwFileOffset + 600 * sinf.dwAllocationGranularity; UnmapViewOfFile(pbFile); pbFile = (PBYTE)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, (DWORD)(qwFileOffset>>32), (DWORD)(qwFileOffset&0xFFFFFFFF), dwBytesInBlock); } break; // 终止事件触发 case 1: bLoop = FALSE; // 从进程的地址空间撤消文件数据映像 UnmapViewOfFile(pbFile); // 关闭文件映射对象 CloseHandle(hFileMapping); break; } } … |
// 创建另外一个文件内核对象 hFile2 = CreateFile("Recv.zip", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL); // 以实际数据长度创建另外一个文件映射内核对象 hFileMapping2 = CreateFileMapping(hFile2, NULL, PAGE_READWRITE, 0, (DWORD)(qwFileOffset&0xFFFFFFFF), NULL); // 关闭文件内核对象 CloseHandle(hFile2); // 将文件数据映射到进程的地址空间 pbFile2 = (PBYTE)MapViewOfFile(hFileMapping2, FILE_MAP_ALL_ACCESS, 0, 0, qwFileOffset); // 将数据从原来的内存映射文件复制到此内存映射文件 memcpy(pbFile2, pbFile, qwFileOffset); file://从进程的地址空间撤消文件数据映像 UnmapViewOfFile(pbFile); UnmapViewOfFile(pbFile2); // 关闭文件映射对象 CloseHandle(hFileMapping); CloseHandle(hFileMapping2); // 删除临时文件 DeleteFile("Recv1.zip"); |