Memory Mapped Files. Need some help.

Started by
4 comments, last by Ace826 19 years, 6 months ago
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.
Advertisement
Yeah...use source tags.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

What does GetLastError() return when CreateFileMapping() fails?

EDIT: Also, try setting the third parameter to FILE_SHARE_READ
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.
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.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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!

This topic is closed to new replies.

Advertisement