共享内存

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;
}

 

转载于:https://my.oschina.net/ucliaohh/blog/536545

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值