基于CS模式的共享内存与事件
#include
#include
using namespace std;
#define BUF_SIZE 4096
#define FM_RECV_NAME "Global\\Send"
#define FM_SEND_NAME "Global\\Recv"
#define MsgEventRecv "Global\\MessageFromEv"
#define MsgEventSend "Global\\Message2Ev"
#define RECVMSG WM_USER+1
#define SENDMSG WM_USER+2
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
);
HANDLE m_hdlUkeyEventRecv;
HANDLE m_hdlFileMapRecv;
int main()
{
m_hdlUkeyEventRecv = OpenEvent(EVENT_ALL_ACCESS, FALSE, MsgEventSend);
if (m_hdlUkeyEventRecv == NULL)
{
m_hdlUkeyEventRecv = CreateEvent(NULL, FALSE, FALSE, MsgEventSend);
}
auto m_hdlUkeyEventSend = CreateEvent(NULL, FALSE, FALSE, MsgEventRecv);
m_hdlFileMapRecv = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
false,
FM_SEND_NAME);
if (m_hdlFileMapRecv == NULL)
{
m_hdlFileMapRecv = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE,
FM_SEND_NAME);
}
auto m_hdlFileMapSend = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE,
FM_RECV_NAME);
if (m_hdlFileMapRecv == NULL || m_hdlFileMapSend == NULL)
{
printf("CreateFileMapping fails,GetLastError is:%d", GetLastError());
}
DWORD m_msgHdlThreadId;
CreateThread(NULL, 0, MsgHandleProc, NULL, 0, &m_msgHdlThreadId);
int msg{ 0 };
while (1)
{
cout << "请输入消息:" << endl;
cin >> msg;
switch (msg)
{
case RECVMSG:
{
std::string dumpStr = "service send msg.....";
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapSend,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
CopyMemory(pBuf, dumpStr.c_str(), dumpStr.length());
SetEvent(m_hdlUkeyEventSend);
UnmapViewOfFile(pBuf);
break;
}
default:
break;
}
if (msg < 0)
break;
}
return 0;
}
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
)
{
while (1)
{
DWORD ret = WaitForSingleObject(m_hdlUkeyEventRecv, INFINITE);
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapRecv,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
cout << pBuf << endl;
UnmapViewOfFile(pBuf);
}
return 0;
}
#include
#include
using namespace std;
#define BUF_SIZE 4096
#define FM_RECV_NAME "Global\\Send"
#define FM_SEND_NAME "Global\\Recv"
#define MsgEventRecv "Global\\MessageFromEv"
#define MsgEventSend "Global\\Message2Ev"
#define RECVMSG WM_USER+1
#define SENDMSG WM_USER+2
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
);
HANDLE m_hdlUkeyEventRecv;
HANDLE m_hdlFileMapRecv;
int main()
{
m_hdlUkeyEventRecv = OpenEvent(EVENT_ALL_ACCESS, FALSE, MsgEventRecv);
if (m_hdlUkeyEventRecv==NULL)
{
m_hdlUkeyEventRecv = CreateEvent(NULL, FALSE, FALSE, MsgEventRecv);
}
auto m_hdlUkeyEventSend = CreateEvent(NULL, FALSE, FALSE, MsgEventSend);
m_hdlFileMapRecv = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
false,
FM_RECV_NAME);
if (m_hdlFileMapRecv==NULL)
{
m_hdlFileMapRecv = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE,
FM_RECV_NAME);
}
auto m_hdlFileMapSend = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE,
FM_SEND_NAME);
if (m_hdlFileMapRecv == NULL || m_hdlFileMapSend == NULL)
{
printf("CreateFileMapping fails,GetLastError is:%d", GetLastError());
}
DWORD m_msgHdlThreadId;
CreateThread(NULL, 0, MsgHandleProc, NULL, 0, &m_msgHdlThreadId);
int msg{ 0 };
while (1)
{
cout << "请输入消息:" << endl;
cin >> msg;
switch (msg)
{
case RECVMSG:
{
std::string dumpStr ="client send msg.....";
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapSend,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
CopyMemory(pBuf, dumpStr.c_str(), dumpStr.length());
SetEvent(m_hdlUkeyEventSend);
UnmapViewOfFile(pBuf);
break;
}
default:
break;
}
if (msg < 0)
break;
}
return 0;
}
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
)
{
while (1)
{
DWORD ret = WaitForSingleObject(m_hdlUkeyEventRecv, INFINITE);
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapRecv,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
cout << pBuf << endl;
UnmapViewOfFile(pBuf);
}
return 0;
}
参考:
1.https: