Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Memory Mapping Large Files Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

January 28, 2005, 05:08 PM

I'm working with a file of approximately 4 gb. I've been successful in mapping the first X bytes, accessing them and doing my processing.

My problem is that when I try to release and reset the map to a different portion of the file, I'm getting Error 5, ERROR_ACCESS_DENIED.

Here's how I've been doing this:

  2. for(int j=0; j<num_chunks; j++) {
  3.    // each of these values are initialized and tested to be correct in my implementation
  4.    // LARGE_INTEGER currPointer - points to the aligned entry to my current map area in the File
  5.    // __int32 span - holds the number of bytes to map for this area
  6.    // __int32 offset_align - holds the number of bytes of front-padding to use to align the spanned block
  10.    hMapFile = CreateFileMapping( hTheFile, NULL, PAGE_READONLY, 0, span + offset_align, tMapName);
  12.    pBuf = (LPTSTR) MapViewOfFile( hMapFile, FILE_MAP_READ, currPointer.HighPart, currPointer.LowPart, span + offset_align);
  14.    // Do all my processing using pBuf here
  16.    UnmapViewOfFile(pBuf);
  17.    CloseHandle(hMapFile);
  18.    CloseHandle(hThefile);
  19. }

Now I don't believe I should have to Close the file handle and re-open it each iteration, I just through it in like this to garauntee a fresh control structure.

The first time through this code works great, but the second time through, when I give it an index for instance 48 MB into the file, I get this ERROR_ACCESS_DENIED back from MapViewOfFile (NOTE: hTheFile and hMapFile both return properly). I know this is a pretty generic error code so you'll have to forgive me if I'm not giving you enough information, but I'm really at a loss right now.



January 28, 2005, 05:50 PM

I had the same problem a few days ago...

  2. CreateFileMapping( hTheFile, NULL, PAGE_READONLY, 0, span + offset_align, tMapName);

Try to map entire file instead of "span + offset_align"

Are currPointer.HighPart and currPointer.LowPart with valid values ? Maybe you have swapped them...

And really don't need to close those handles in each iteration :)


February 08, 2005, 01:24 PM

mentalcalculator wrote: Try to map entire file instead of "span + offset_align"

os3omaha wrote: I'm working with a file of approximately 4 gb.



February 09, 2005, 06:47 AM

- I _guess_ you need to pass some file sharing flags to CreateFile, like FILE_SHARE_READ and/or FILE_SHARE_WRITE.

- And yes, you _need_ to close the handles in each iteration, since you create a new one in each iteration.

- Also, not only the size but also the start pointer must be aligned.

- Also, you currently assume the size fits into 32 bits in your call to CreateFileMapping.

But why do you do this ? Why not create the the file once, create the mapping once, and then viewing and unviewing only parts of it ? It's no problem to create a mapping on a 4GB file, it's only that you cannot _view_ the 4GB at once.

So, why not try

  2. /* note FILE_SHARE_READ */
  3. hTheFile = CreateFile( lFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
  4.                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  6. /* DETERMINE total_size HERE */
  7. hMapFile = CreateFileMapping( hTheFile, NULL, PAGE_READONLY,
  8.                               total_size_high_part, total_size_low_part,
  9.                               tMapName);
  11. for(int j=0; j<num_chunks; j++) {
  12.    /* ALIGN currPointer HERE */
  13.    pBuf = (LPTSTR) MapViewOfFile( hMapFile, FILE_MAP_READ,
  14.                                   currPointer.HighPart, currPointer.LowPart,
  15.                                   span + offset_align);
  17.    // Do all my processing using pBuf here
  19.    UnmapViewOfFile(pBuf);
  20. }
  22. CloseHandle(hMapFile);
  23. CloseHandle(hTheFile);

This thread contains 4 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.