Hi,
I am receiving buffer from some module. example i received buffer which is produced by below peace of code
CString strTemp = "";
for(int i =0; i < 10000; ++i)
{
strTemp.Format("Test1-Test2-Test3-Test4-Test5-Test6-Test7===%d;",i);
m_strMemData = m_strMemData + strTemp;
}
Then i am trying to insert the buffer into file using memory mapped files.
Steps i am doing as part of below peace of code is:
Module#1
1) Creating a file in temp folder
2) Creating memory map file, using CreateFileMapping
these first two parts will be done as part of seperate module.
Module#2:
3) Opening mapped file, using OpenFileMapping
4) Finding granularity using GetSystemInfo
5) In loop mapping the file which is in temp folder, and trying to write by chunk by chunk, using MapViewOfFile and copymemory.
in First iteration mapviewoffile is success, second time if i change the dwFileOffsetHigh, and dwFileOffsetLow then the MapViewOfFile function is failing with error code#5.
please help if any one have solution.
some one suggested SetFilePointer, i tried that also, but not worked. whether i need to try this setfilepointer with mapped file pointer / opened file pointer or which one. ?
Please find code :
LPVOID pViewMMFFile;
DWORD granularity, dwSizeContent;
DWORD64 dwOffSetStart;
HANDLE hFile, hFileMMF, hFileMMFTemp;
char szName[256] = "PROCESS1";
char szTmpFile[256];
char szMMFile[256];
SYSTEM_INFO sysInfo;
GetTempPath (256, szTmpFile);
strcat( szTmpFile, "temptestbuf.txt");
strcpy( szMMFile, szTmpFile);
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(sa);
hFile = CreateFile (szMMFile,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa,
OPEN_ALWAYS, FILE_ATTRIBUTE_READONLY | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
CString str;
str = szMMFile;
if( hFile == NULL )
{
DWORD dwErr1 = GetLastError();
ASSERT(FALSE);
return;
}
hFileMMFTemp = CreateFileMapping(hFile, &sa, PAGE_READWRITE, 0, 0, szName);
if(hFileMMFTemp == NULL)
{
DWORD dwErr2 = GetLastError();
ASSERT(FALSE);
return;
}
CloseHandle(hFile);
dwSizeContent = m_strMemData.GetLength(); dwOffSetStart = 0;
hFileMMF = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,szName);
if(hFileMMF == NULL)
{
DWORD dwErr3 = GetLastError();
ASSERT(FALSE);
return;
}
GetSystemInfo(&sysInfo);
granularity = sysInfo.dwAllocationGranularity;
do
{
pViewMMFFile = (unsigned short*)MapViewOfFile(hFileMMF, FILE_MAP_ALL_ACCESS, (DWORD)dwOffSetStart >> 32,
(DWORD)dwOffSetStart & 0xFFFFFFFF, granularity);
if(pViewMMFFile == NULL)
{
DWORD dwErr4 = GetLastError();
ASSERT(FALSE);
return;
}
if (dwOffSetStart+granularity < dwSizeContent)
dwOffSetStart+=granularity;
else
dwOffSetStart = dwSizeContent;
UnmapViewOfFile(pViewMMFFile);
}
while( dwOffSetStart < dwSizeContent );
SetEndOfFile(pViewMMFFile);
CloseHandle(hFileMMF);
CloseHandle(hFileMMFTemp);
visual studio is already running as administrator. thanks for your message