[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Doubt in FileMapping

Posted on 2002-07-07
6
Medium Priority
?
367 Views
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);
LPVOID
lpvFile=::MapViewOfFile(m_hMap,FILE_MAP_READ,0,0,0);

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?

TIA
Ashwin
0
Comment
Question by:Ashwin
[X]
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
6 Comments
 
LVL 30

Assisted Solution

by:Axter
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?
0
 
LVL 30

Assisted Solution

by:Axter
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.
//////////////////////////////////////
0
 

Author Comment

by:Ashwin
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..
0
 
LVL 12

Accepted Solution

by:
migel earned 300 total points
ID: 7137053
Hi!
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
            ForceClose();
        }
    }
}
//////////////////////////////////////////////////////////////////////////////
VOID CGMMF::ForceClose() {
    // Cleanup everything that was done sucessfully
    if (m_pvFile   != NULL) {
        ::UnmapViewOfFile(m_pvFile);
        m_pvFile = NULL;
    }
    if (m_hfilemap != NULL) {
        ::CloseHandle(m_hfilemap);  
        m_hfilemap = NULL;
    }
}
//////////////////////////////////////////////////////////////////////////////
BOOL CGMMF::SetToZero(HANDLE hfile,
    UINT_PTR cbOffsetStart, UINT_PTR cbOffsetEnd) {    // NOTE: This function does not work if this file is memory-mapped.
    DWORD dw;
    FILE_ZERO_DATA_INFORMATION fzdi;
    fzdi.FileOffset.QuadPart = cbOffsetStart;
    fzdi.BeyondFinalZero.QuadPart = cbOffsetEnd + 1;
    return(::DeviceIoControl(hfile, FSCTL_SET_ZERO_DATA,
        (LPVOID) &fzdi, sizeof(fzdi), NULL, 0, &dw, NULL));
}

//////////////////////////////////////////////////////////////////////////////
//usage
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 /////////////////////////////////
0
 

Author Comment

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

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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