Jump to content
  • Advertisement
Sign in to follow this  
Ace826

Memory Mapped Files. Need some help.

This topic is 5461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I have a problem. I am trying to create a memory mapped file and load the info from it. Well, I step through using debugger, and hFileMap is being set to 0x0000000h.
{
	HANDLE hFile;
	DWORD dwFileSize;

	hFile = CreateFile("Heimp.hmp",						GENERIC_READ,							0,						
		NULL,								OPEN_EXISTING,					
		FILE_ATTRIBUTE_NORMAL,						NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		MessageBox(NULL, "hFile invalid.", "ERROR", MB_OK);
		return FALSE;
	}
	dwFileSize = GetFileSize(hFile, NULL);
    
	HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);	
	if(hFileMap == NULL)
	{
        MessageBox(NULL, "Couldn't create file map.", "File I/O Error", MB_OK | MB_ICONSTOP);
		return FALSE;
	}

	/*Map a view of the file*/
	BYTE *pbFile = (BYTE *) MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
	
	CloseHandle(hFile);
	CloseHandle(hFileMap);
	

	for(unsigned int ind = 0; ind < dwFileSize; ind++)
	{
		HeightMap[ind] = *(pbFile + ind);
	}


	UnmapViewOfFile((PVOID) pbFile);

	return TRUE;
}

Darn memory mapped files. Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
What does GetLastError() return when CreateFileMapping() fails?

EDIT: Also, try setting the third parameter to FILE_SHARE_READ

Share this post


Link to post
Share on other sites
Last error returns 1006. I would like to mention. When I get the file size. File size is being set to 0. File does exist and is filled with all my heightmap stuff.

Share this post


Link to post
Share on other sites
One time, I was absolutely convinced that a file I was trying to read from was there when in reality I had actually moved it. When I forced the program to read from the file pointer, it shut the computer down hard, like someone had yanked the cord out of the wall. So, the moral of the story is, check your file path.

Share this post


Link to post
Share on other sites
Solved the file error. Problem is:


HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);



Well, this set my file to be 0 bytes. Changed the 5th param to dwFileSize. Now it opens the file, new problems, but I am hoping i can figure them out. THX guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!