Re: [問題] Shared Memory

看板C_and_CPP (C/C++)作者 (天亮damody)時間12年前 (2013/07/04 01:09), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《jeep168917 (幫幫)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : VC++ : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 問題(Question): : 我讓windows一開機就先建立一塊shared memory,我可以控制這塊shard memory的大小 : 也知道這塊memory的記憶體位置。 : 目前我在project都是已"new"去配置記憶體,但是我想要用將我所用到的資料配置到 : 我已知的記憶體位置,也就是說將資料改成用到shared memory,而不是隨便配置記憶體 : 餵入的資料(Input): : 預期的正確結果(Expected Output): : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : 舉例 : m_Tab0 = new CMonitorTab0; : 補充說明(Supplement): : 第一次發文,若有不清楚或者違反規則,我可以補充或者刪除文章 : 謝謝 我最近因為 x86 跟 x64 溝通的問題, 也使用 share memory 來解決, 定義兩個 class 一個 sender 一個 reader 去溝通= = 沒有用同步機制,實際運用上需要修改, 不過可以跑,算簡單的 example 程鏑。 class FileMappingSender { public: int m_BufferSize; HANDLE m_hMapFile; char* m_pBuf; FileMappingSender():m_BufferSize(1024){} ~FileMappingSender() { UnmapViewOfFile(m_pBuf); CloseHandle(m_hMapFile); } bool OpenShareMemory(std::wstring name) { m_hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write access FALSE, // do not inherit the name name.c_str()); // name of mapping object if (m_hMapFile == NULL) { _tprintf(TEXT("Could not open file mapping object (%d).\n"), GetLastError()); return false; } m_pBuf = (char*) MapViewOfFile(m_hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, m_BufferSize); if (m_pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); CloseHandle(m_hMapFile); return false; } return true; } void CallPrint(std::string s) { s = "pr" + s; memcpy((void*)m_pBuf, s.c_str(), s.size()); m_pBuf[0] = 'p'; m_pBuf[1] = 'r'; } }; class FileMappingReader { public: int m_BufferSize; HANDLE m_hMapFile; char* m_pBuf; FileMappingReader():m_BufferSize(1024){} ~FileMappingReader() { UnmapViewOfFile(m_pBuf); CloseHandle(m_hMapFile); } bool OpenShareMemory(std::wstring name) { m_hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) name.c_str()); // name of mapping object if (m_hMapFile == NULL) { _tprintf(TEXT("Could not open file mapping object (%d).\n"), GetLastError()); return false; } m_pBuf = (char*) MapViewOfFile(m_hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, m_BufferSize); if (m_pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); CloseHandle(m_hMapFile); return 0; } } bool CheckPrint(std::string& ostr) { if ('p' == m_pBuf[0] && 'r' == m_pBuf[1]) { std::string ss((char*)m_pBuf+2); ostr = ss; memset(m_pBuf, 0, m_BufferSize); return true; } return false; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.37.88.25

07/04 01:23, , 1F
沒寫過 x64, msg 不能解?
07/04 01:23, 1F

07/04 12:25, , 2F
可以吧。
07/04 12:25, 2F

07/04 15:00, , 3F
了解 所以是習慣性了
07/04 15:00, 3F
文章代碼(AID): #1Hr5g_wG (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1Hr5g_wG (C_and_CPP)