UnmapViewOfFile不会释放文件。执行功能后无法访问文件。

时间:2013-02-06 05:20:46

标签: c++ windows winhttp

我正在尝试使用winhttp将文件上传到云服务,但是在运行以下代码后,我无法再访问同一个文件了。似乎程序没有正确关闭其文件句柄:

HANDLE hFile = CreateFileW(MultiByteToWideChar(filePath), GENERIC_READ, FILE_SHARE_READ+FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwFileSize = GetFileSize(hFile, NULL);
HANDLE hFileMappingObject = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSize, NULL);
HANDLE file = MapViewOfFile(hFileMappingObject, FILE_MAP_READ, 0, 0, 0);

bool result = WinHttpSendRequest( hRequest, 
                                    WINHTTP_NO_ADDITIONAL_HEADERS,
                                    0, WINHTTP_NO_REQUEST_DATA, 0, 
                                    dwFileSize, 0);

result = WinHttpWriteData( hRequest, file, dwFileSize, &dwBytesWritten);

FlushViewOfFile(file, dwFileSize);
FlushFileBuffers(hFile);
UnmapViewOfFile(file);
CloseHandle(hFileMappingObject);
CloseHandle(hFile);

0 个答案:

没有答案