Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Doubt in FileMapping

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?

  • 2
  • 2
3 Solutions
Did you make sure the file is greater then zero length?
Did you verify that pFile is valid before using CreateFileMapping?
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.
AshwinAuthor Commented:
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..
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 /////////////////////////////////
AshwinAuthor Commented:
Thanks a lot guys.. sorry for not closing this doubt earlier.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now