C++ MapViewOfFile fails

谁说我不能喝 提交于 2019-12-02 22:15:32

问题


I am trying to memory-map a file on Windows using VS2010. I am doing this in a DLL. The first instance of the DLL maps the file just fine. The second instance within the same process causes

*ppvData = ::MapViewOfFile( *phMapping, FILE_MAP_READ, 0, 0, 0 );

to fail with the error "Not enough memory available for this command". I am not sure why this happens.

If I map 2 different files instead of twice the same file, all works fine, so I don't trust the "Not enough memory" error message.

Thank you.

hr = MapFile(sPath, &m_hVoiceData, &m_pVoiceData,wsErr );

HRESULT CTTSEngObj::MapFile( wstring uPath,  // Value that contains file path
                            HANDLE * phMapping,          // Pointer to file mapping handle
                            void ** ppvData,             // Pointer to the data
                            wstring &uError)
{
    HRESULT hr = S_OK;
    CSpDynamicString dstrFilePath(uPath.c_str());

    if ( SUCCEEDED( hr ) )
    {
        bool fWorked = false;
        *phMapping = NULL;
        *ppvData = NULL;
        HANDLE hFile;
#ifdef _WIN32_WCE
        hFile = CreateFileForMapping( dstrFilePath, GENERIC_READ,
                                      FILE_SHARE_READ, NULL, OPEN_EXISTING,
                                      FILE_ATTRIBUTE_NORMAL, NULL );
#else
        hFile = CreateFile(CW2T(dstrFilePath), GENERIC_READ,
                            FILE_SHARE_READ, NULL, OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL, NULL );
#endif
        if (hFile != INVALID_HANDLE_VALUE)
        {
            *phMapping = ::CreateFileMapping( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
            if (*phMapping)
            {
                *ppvData = ::MapViewOfFile( *phMapping, FILE_MAP_READ, 0, 0, 0 );
                if (*ppvData)
                {
                    fWorked = true;
                }
                else
                {
                    uError=GetLastErrorStdStrW();
                }
            }
            else
            {
                uError=L"mapfile: fm failed";
            }
            ::CloseHandle( hFile );
        }
        else
        {
            uError=L"mapfile: invalidhandle";
        }
        if (!fWorked)
        {
            hr = HRESULT_FROM_WIN32(::GetLastError());
            if (*phMapping)
            {
                ::CloseHandle(*phMapping);
                *phMapping = NULL;
            }
        }
    }
    else
    {
        uError=L"mapfile: dynstr";
    }
    return hr;
} /* CTTSEngObj::MapFile */

And this is how it is declared:

class ATL_NO_VTABLE CTTSEngObj : 
    public CComObjectRootEx<CComMultiThreadModel>,
    public CComCoClass<CTTSEngObj, &CLSID_SampleTTSEngine>,
    public ISpTTSEngine,
    public ISpObjectWithToken
{

  private:
    CComPtr<ISpObjectToken> m_cpToken;
    HANDLE                  m_hVoiceData;
    void*                   m_pVoiceData;

回答1:


You request to map the entire file into memory. How big is it? There may very well not be a large enough contiguous range of unallocated process's address space.



来源:https://stackoverflow.com/questions/19016512/c-mapviewoffile-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!