|
HANDLE hFile=CreateFile(L"BC.txt",GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);//EVC下这里用CreateFileForMapping
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(L"Create file failed!");
return;
}
HANDLE hFileMap = CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,
/*dwFileSize+sizeof(char)*/256,L"write");
if(hFileMap == NULL){
MessageBox(L"File mapping failed!");
CloseHandle(hFile);
return;
}
PVOID pvFile = MapViewOfFile(hFileMap,FILE_MAP_WRITE,0,0,0);
if(pvFile == NULL){
MessageBox(L"Access to File map failed!");
CloseHandle(hFileMap);
CloseHandle(hFile);
return;
}
PSTR pchAnsi=(PSTR)pvFile;
memcpy(pchAnsi,"abcdefhgijklml",14);
//CFile m_file((int)hFile);
//m_file.Open();
//m_file.Write("abefghijkl",10);
//m_file.Close();
if(UnmapViewOfFile(pvFile)==0) MessageBox(L"Unmap View failed!");
CloseHandle(hFileMap);
if(SetFilePointer(hFile,8,0,FILE_BEGIN)==0) MessageBox(L"Set File Pointer failed!");
if(SetEndOfFile(hFile)==0) MessageBox(L"Resize file failed!");
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
MessageBox((LPCTSTR)lpMsgBuf);
CloseHandle(hFile);
上面的代码在VS2005下运行正常,文件被正确设置为8Byte,但在EVC下,最后的SetEndOfFile这句就是不能成功,而直接创建文件而不创建内存映像的话就正常,但是我已经关闭了内存映像啦,句柄也已经关了,不知道为什么就是不行呢?请高手指教啊!
|
|