Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am receiving buffer from some module. example i received buffer which is produced by below peace of code
C++
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 :
C++
LPVOID  pViewMMFFile;
DWORD   granularity, dwSizeContent;
DWORD64 dwOffSetStart;
HANDLE  hFile, hFileMMF, hFileMMFTemp;
char    szName[256] = "PROCESS1";
char    szTmpFile[256];
char    szMMFile[256];
SYSTEM_INFO sysInfo;

////////////////////////////////////////
/* Create temporary file for mapping. */
////////////////////////////////////////
GetTempPath (256, szTmpFile);

strcat( szTmpFile, "temptestbuf.txt");
strcpy( szMMFile, szTmpFile);

SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(sa);

////////////////////////////////////////////
/* If file created, continue to map file. */
////////////////////////////////////////////
hFile = CreateFile (szMMFile,
	GENERIC_WRITE | GENERIC_READ,
	FILE_SHARE_READ|FILE_SHARE_WRITE,
	&sa,
	OPEN_ALWAYS,//CREATE_ALWAYS,
	FILE_ATTRIBUTE_READONLY | FILE_FLAG_SEQUENTIAL_SCAN,
	NULL);
CString str;
str = szMMFile;
if( hFile == NULL )
{
	DWORD dwErr1 = GetLastError();
	ASSERT(FALSE);
	return;
}

/*----------------------------
CREATING FILE DONE.
----------------------------*/

hFileMMFTemp = CreateFileMapping(hFile, &sa, PAGE_READWRITE, 0, 0, szName);

if(hFileMMFTemp == NULL)
{
	DWORD dwErr2 = GetLastError();
	ASSERT(FALSE);
	return;
}
CloseHandle(hFile);
/*--------------------------------------------------------
	----------- First module completed -----------.....
--------------------------------------------------------*/

dwSizeContent = m_strMemData.GetLength(); //GetFileSize(hFile, NULL);
dwOffSetStart = 0;
/*----------------------------
CREATING FILE MAPPING IS DONE.
----------------------------*/
//Open named file mapping object.
hFileMMF = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,szName);

if(hFileMMF == NULL)
{
	DWORD dwErr3 = GetLastError();
	ASSERT(FALSE);
	return;
}
GetSystemInfo(&sysInfo);
granularity = sysInfo.dwAllocationGranularity;
//SetFilePointer( hFileMMF, granularity, NULL, FILE_BEGIN);
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;

	//CopyMemory((LPVOID)pViewMMFFile, pTempBinary, dwLength );
	UnmapViewOfFile(pViewMMFFile);
	

//Is this the right way to ....??
	//SetFilePointer( pViewMMFFile, granularity, NULL, FILE_BEGIN);
}
while( dwOffSetStart < dwSizeContent );

SetEndOfFile(pViewMMFFile);
CloseHandle(hFileMMF);

CloseHandle(hFileMMFTemp);	
visual studio is already running as administrator. thanks for your message
Posted
Updated 17-Nov-11 23:34pm
v2

1 solution

If using Windows7 - try to run Visual Studio "as Administrator" and then run your code again to see if you still get error 5 (Access Denied).
 
Share this answer
 
Comments
G Haranadh 18-Nov-11 5:33am    
Hi, visual studio is already running as administrator. thanks for your message

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900