-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsShareMemoryWrapper.cpp
140 lines (125 loc) · 2.74 KB
/
WindowsShareMemoryWrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "WindowsShareMemoryWrapper.h"
WindowsSharedMemoryWrapper::WindowsSharedMemoryWrapper(DWORD dwOffset, DWORD dwSize,const std::string& strName):m_hFileMap(NULL),
m_pMemory(NULL),
m_dwOffset(dwOffset),
m_dwMappedSize(dwSize),
m_strName(strName)
{
}
WindowsSharedMemoryWrapper::~WindowsSharedMemoryWrapper()
{
Close();
}
bool WindowsSharedMemoryWrapper::Create()
{
if (m_dwMappedSize <= 0 || m_strName.empty())
return false;
HANDLE handle = ::CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, m_dwMappedSize, m_strName.c_str());
if (!handle)
return false;
//// 已经存在了
//if (GetLastError() == ERROR_ALREADY_EXISTS)
//{
// Close();
// return false;
//}
m_hFileMap = handle;
return false;
}
bool WindowsSharedMemoryWrapper::Open()
{
if (m_strName.empty())
return false;
m_hFileMap = ::OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, m_strName.c_str());
if (!m_hFileMap)
return false;
return true;
}
bool WindowsSharedMemoryWrapper::MapAt()
{
if (!m_hFileMap || m_dwMappedSize > ULONG_MAX)
return false;
ULARGE_INTEGER ui;
ui.QuadPart = static_cast<ULONGLONG>(m_dwOffset);
m_pMemory = (LPTSTR)::MapViewOfFile(m_hFileMap,FILE_MAP_ALL_ACCESS, ui.HighPart, ui.LowPart, m_dwMappedSize);
return (m_pMemory != NULL);
}
void WindowsSharedMemoryWrapper::Unmap()
{
if (m_pMemory)
{
::UnmapViewOfFile(m_pMemory);
m_pMemory = NULL;
}
}
bool WindowsSharedMemoryWrapper::WriteMemory(std::string data)
{
bool ret = false;
if (data.empty())
{
Lock();
memset((void*)m_pMemory, 0, 1024);
ret = CopyMemory((void*)m_pMemory, data.c_str(), 1024) != NULL;
Unlock();
}
else
{
Lock();
memset((void*)m_pMemory, 0, 1024);
ret = CopyMemory((void*)m_pMemory, data.c_str(), data.length()) != NULL;
Unlock();
}
return ret;
}
std::string WindowsSharedMemoryWrapper::ReadMemory()
{
std::string msg = "";
if (m_pMemory != NULL)
{
Lock();
msg.assign((char*)m_pMemory);
Unlock();
}
return msg;
}
HANDLE WindowsSharedMemoryWrapper::GetHandle() const
{
return m_hFileMap;
}
bool WindowsSharedMemoryWrapper::Lock()
{
// 如果还没有创建锁就先创建一个
if (!m_hLock)
{
std::string strLockName = m_strName;
strLockName.append("_Lock");
// 初始化的时候不被任何线程占用
m_hLock = ::CreateMutexA(NULL, FALSE, strLockName.c_str());
if (!m_hLock)
return false;
}
// 哪个线程最先调用等待函数就最先占用这个互斥量
DWORD dwRet = ::WaitForSingleObject(m_hLock, INFINITE);
return (dwRet == WAIT_OBJECT_0 || dwRet == WAIT_ABANDONED);
}
void WindowsSharedMemoryWrapper::Unlock()
{
if (m_hLock)
{
::ReleaseMutex(m_hLock);
}
}
void WindowsSharedMemoryWrapper::Close()
{
Unmap();
if (m_hFileMap)
{
::CloseHandle(m_hFileMap);
m_hFileMap = NULL;
}
if (m_hLock != NULL)
{
CloseHandle(m_hLock);
m_hLock = NULL;
}
}