Write Memory 启动 excel 进程,并通过 shared Memory 传递文件名
void CRtcaDataAnalysisApp::OnOpenExcel()
{
HANDLE hMapFile = NULL;
PVOID pView = NULL;
// Create the file mapping object
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // Use paging file - shared memory
NULL, // Default security attributes
PAGE_READWRITE, // Allow read and write access
0, // High-order DWORD of file mapping max size
65536, // Low-order DWORD of file mapping max size
_T("RTCA_CardioECR") // Name of the file mapping object
);
if (!hMapFile)
return;
// Map a view of the file mapping into the address space of the current
// process.
pView = MapViewOfFile(
hMapFile, // Handle of the map object
FILE_MAP_ALL_ACCESS, // Read and write access
0, // High-order DWORD of the file offset
0, // Low-order DWORD of the file offset
1024 // The number of bytes to map to view
);
if (!pView)
{
CloseHandle(hMapFile);
hMapFile = NULL;
}
USES_CONVERSION;
LPWSTR pszMessage = A2W(m_strPltFile);
DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);
memcpy_s(pView, 1024, pszMessage, cbMessage);
HKEY hKEY;
HKEY hKeyRoot = HKEY_LOCAL_MACHINE;
long ret0 = (::RegOpenKeyEx(hKeyRoot, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\excel.exe", 0, KEY_READ, &hKEY));
if (ret0 != ERROR_SUCCESS)
return;
LPBYTE getValue = new BYTE[80];
DWORD keyType = REG_SZ;
DWORD DataLen = 80;
CString strUser = _T("Path");
long ret1 = ::RegQueryValueEx(hKEY, strUser, NULL, &keyType, getValue, &DataLen);
if (ret1 != ERROR_SUCCESS)
return;
CString strExcelPath = (LPTSTR)getValue;
WinExec(strExcelPath + "\\EXCEL.EXE", SW_SHOW);
}
Read & Clear Memory
CString CExcelAddInCtrl::GetPltFileFromSharedMemory()
{
HANDLE hMapFile = NULL;
PVOID pView = NULL;
// Try to open the named file mapping identified by the map name
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // Read access
FALSE, // Do not inherit the name
_T("RTCA_CardioECR") // File mapping name
);
if (!hMapFile)
return NULLSTR;
// Map a view of the file mapping into the address space of the current
// process.
pView = MapViewOfFile(
hMapFile, // Handle of the map object
FILE_MAP_ALL_ACCESS, // Read access
0, // High-order DWORD of the file offset
0, // Low-order DWORD of the file offset
1024 // The number of bytes to map to view
);
if (!pView)
{
CloseHandle(hMapFile);
hMapFile = NULL;
return NULLSTR;
}
USES_CONVERSION;
CString str = W2A(LPWSTR(pView));
if (hMapFile)
{
if (pView)
{
LPWSTR pszMessage = L"";
DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);
memcpy_s(pView, 1024, pszMessage, cbMessage);
UnmapViewOfFile(pView); //Unmap the file view
pView = NULL;
}
CloseHandle(hMapFile); //Close the file mapping object
hMapFile = NULL;
}
return str;
}