我有一个主VC ++应用程序正在创建共享内存,如下面的代码所示:
if (m_hMapFile == NULL)
{
SECURITY_ATTRIBUTES structSecurityAttr = { 0 };
m_hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, &structSecurityAttr, PAGE_READWRITE | SEC_COMMIT, 0, BUF_SIZE, (LPCSTR)(LPCTSTR)szName);
if (m_hMapFile == NULL)
{
DWORD dwError = GetLastError();
CString strError;
strError.Format(_T("%lu"), dwError);
return FALSE;
}
pSharedMemBuf = (LPTSTR)MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
if (pSharedMemBuf == NULL)
{
return FALSE;
}
//Initialize the Shared Memory.
ZeroMemory((void *)pSharedMemBuf, BUF_SIZE);
}
从这个主进程的一个线程我将映射到共享内存,如下所示:
if (m_hMapFile == NULL)
{
//Open the Named Shared Memory
m_hMapFile = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, TRUE, (LPCSTR)(LPCTSTR)szName); //FILE_MAP_ALL_ACCESS
if (m_hMapFile == NULL)
{
return FALSE;
}
//Get the Shared Memory Buffer Pointer.
pSharedMemBuf = (LPTSTR)MapViewOfFile(m_hMapFile, FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY/*FILE_MAP_ALL_ACCESS*/, 0, 0, BUF_SIZE);
DWORD error = GetLastError();
//If Shared Memory Buffer is NULL then return the control
if (pSharedMemBuf == NULL)
{
CString strSize;
strSize.Format(_T("Error is %d"), error);
return FALSE;
}
}
现在,我想将CreateProcess
调用的输出重定向到此共享内存。我正在提供FileMapping STARTUPINFO
,如下所示:
si.hStdOutput = m_hMapFile;
si.hStdError = m_hMapFile;
然后按如下方式创建流程:
CreateProcess( NULL, szCombinedCmd, &saAttr, NULL, TRUE,
dwCreateFlags, NULL, pszCurrentDirectory, &si, &pi );
但是,当我尝试读取共享内存时,我没有将输出输入缓冲区。请帮我解决这个问题。