Doubt in FileMapping

Posted on 2002-07-07
Medium Priority
Last Modified: 2013-11-20
for a certain project of mine, where i display audio(wave) data, i use Create a Mapping for my file and Map its view, using CreateFileMapping and MapViewOfFile, in this case i use  an already recorded wave file,using this i create a bitmap which i blit it.
but in another part of the same project i have to display live recorded file wherein i create the file with share deny none attribute in the following manner
CFile* pFile = GetFile(lpszPathName,
          CFile::modeRead|CFile::shareDenyNone, &fe);
after this i create a filemapping and mapview of the file,
HANDLE m_hMap=::CreateFileMapping((HANDLE)pFile->m_hFile,NULL,PAGE_READONLY,0,0,NULL);

now in this case i get a NULL handle for CreateFileMapping,
i even changed the attributes of 3rd parameter of CreateFileMapping, to PAGE_READWRITE and FILE_MAP_ALL_ACCESS but still i get NULL handle.

how can i correct this problem?

Question by:Ashwin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 30

Assisted Solution

Axter earned 300 total points
ID: 7135065
Did you make sure the file is greater then zero length?
Did you verify that pFile is valid before using CreateFileMapping?
LVL 30

Assisted Solution

Axter earned 300 total points
ID: 7135073
After getting a NULL value, what do you get from GetLastError() function?

If the CreateFileMapping function fails, the return value is NULL. To get extended error information, call GetLastError.

Author Comment

ID: 7135977
the problem that i am facing as u pointed out is the size of the file changes at runtime(this file save the live audio data), i want to map this file so that i can display the contents of the growing file. i have a separate worker thread which finds out the size of the file and if on a size change i take the appropriate action.
will filemapping work in my case or should i have to take care of allocation/deallocation myself..
LVL 12

Accepted Solution

migel earned 300 total points
ID: 7137053
It seems like you need to use Growable Memory mapped file.
look at MSDN article Periodicals 1999\MSJ\April\Q&A Win32
there is example how to create those files by Jeffrey Richter
here is code snippets from article:

Module name: GMMF.cpp
Written by: Jeffrey Richter
Purpose:    Function prototypes for using growable memory-mapped files.
#define _WIN32_WINNT 0x0500 // Sparse files require Windows 2000
#include <windows.h>
#include <WinIoCtl.h>
// This class makes it easy to work with memory-mapped sparse files
class CGMMF {
   HANDLE m_hfilemap;      // File-mapping object
   PVOID  m_pvFile;        // Address to start of mapped filepublic:
   // Creates a GMMF and maps it in the process's address space.
   CGMMF(HANDLE hFile, UINT_PTR cbFileSizeMax);   // Closes a GMMF
   ~CGMMF() { ForceClose(); }   // GMMF to BYTE cast operator returns address of first byte
   // in the memory-mapped sparse file.
   operator PBYTE() const { return((PBYTE) m_pvFile); }   // Allows you to explicitly close the GMMF without having
   // to wait for the destructor to be called.
   VOID ForceClose();public:
    // Static method that resets a portion of a file back to
    // zeroes (frees disk clusters)
    static BOOL SetToZero(HANDLE hfile,
        UINT_PTR cbOffsetStart, UINT_PTR cbOffsetEnd);


CGMMF::CGMMF(HANDLE hfile, UINT_PTR cbFileSizeMax) {
    // Initialize to NULL in case something goes wrong
    m_hfilemap = m_pvFile = NULL;    // Make the file sparse
    DWORD dw;
    BOOL fOk = ::DeviceIoControl(hfile, FSCTL_SET_SPARSE,
        NULL, 0, NULL, 0, &dw, NULL);    if (fOk) {
        // Create a file-mapping object
        m_hfilemap = ::CreateFileMapping(hfile, NULL,
            PAGE_READWRITE, 0, cbFileSizeMax, NULL);        if (m_hfilemap != NULL) {
            // Map the file into the process's address space
            m_pvFile = ::MapViewOfFile(m_hfilemap,
                FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0);
        } else {
            // Failed to map the file, cleanup
VOID CGMMF::ForceClose() {
    // Cleanup everything that was done sucessfully
    if (m_pvFile   != NULL) {
        m_pvFile = NULL;
    if (m_hfilemap != NULL) {
        m_hfilemap = NULL;
    UINT_PTR cbOffsetStart, UINT_PTR cbOffsetEnd) {    // NOTE: This function does not work if this file is memory-mapped.
    DWORD dw;
    fzdi.FileOffset.QuadPart = cbOffsetStart;
    fzdi.BeyondFinalZero.QuadPart = cbOffsetEnd + 1;
    return(::DeviceIoControl(hfile, FSCTL_SET_ZERO_DATA,
        (LPVOID) &fzdi, sizeof(fzdi), NULL, 0, &dw, NULL));

int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int ) {    char szPathname[] = "C:\\GMMF.";    // Create the file
    HANDLE hfile = CreateFile(szPathname, GENERIC_READ | GENERIC_WRITE,
        0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);    // Create a GMMF using the file (set the maximum size here too)
    CGMMF gmmf(hfile, 10 * 1024 * 1024);    // Read bytes from the file (0s are returned)
    for (int x = 0; x < 10 * 1024 * 1024; x += 1024) {
        if (gmmf[x] != 0) DebugBreak();
    }    // Write bytes to the file (clusters are allocated as necessary).
    for (x = 0; x < 100; x++) {
        gmmf[8 * 1024 * 1024 + x] = x;
    }    // These lines just prove to us what's going on
    DWORD dw = GetFileSize(hfile, NULL);
    // This returns the logical size of the file.    // Get the actual number of bytes allocated in the file
    dw = GetCompressedFileSize(szPathname, NULL);
    // This returns 0 because the data has not been written to the file yet.
    // Force the data to be written to the file
    FlushViewOfFile(gmmf, 10 * 1024 * 1024);    // Get the actual number of bytes allocated in the file
    dw = GetCompressedFileSize(szPathname, NULL);
    // This returns the size of a cluster now    // Normally the destructor causes the file-mapping to close.
    // But, in this case, we wish to force it so that we can reset
    // a portion of the file back to all zeroes.
    gmmf.ForceClose();    // We call ForceClose above because attempting to zero a portion of the
    // file while it is mapped, causes DeviceIoControl to fail with error
    // code 0x4C8 (ERROR_USER_MAPPED_FILE: "The requested operation cannot
    // be performed on a file with a user-mapped section open.")
    CGMMF::SetToZero(hfile, 0, 2 * 1024 * 1024);    // We no longer need access to the file, close it.
    CloseHandle(hfile);    return(0);
}//////////////////////////////// End of File /////////////////////////////////

Author Comment

ID: 12040890
Thanks a lot guys.. sorry for not closing this doubt earlier.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question