我正在尝试使用 VS2010 在 Windows 上对文件进行内存映射。我在 DLL 中执行此操作。DLL 的第一个实例可以很好地映射文件。同一进程内的第二个实例导致
*ppvData = ::MapViewOfFile( *phMapping, FILE_MAP_READ, 0, 0, 0 );
失败并显示错误“没有足够的内存可用于此命令”。我不确定为什么会这样。
如果我映射 2 个不同的文件而不是两次相同的文件,一切正常,所以我不相信“内存不足”错误消息。
谢谢你。
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 */
这就是它的声明方式:
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;