服务端
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
HANDLE hMutex = NULL;
HANDLE hFileMapping = NULL;
LPVOID lpShareMemory = NULL;
HANDLE hServerWriteOver = NULL;
HANDLE hClientReadOver = NULL;
char p = 0;
char* q = NULL;
//create share memory
hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
1024 * 1024,
L"ShareMemoryTest");
if (NULL == hFileMapping)
{
cout << "CreateFileMapping fail:" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}
lpShareMemory = MapViewOfFile(hFileMapping,
FILE_MAP_ALL_ACCESS,
0,
0, //memory start address
0); //all memory space
if (NULL == lpShareMemory)
{
cout << "MapViewOfFile" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}
q = (char*)lpShareMemory;
hMutex = CreateMutex(NULL, FALSE, L"SM_Mutex");
if (NULL == hMutex || ERROR_ALREADY_EXISTS == GetLastError())
{
cout << "CreateMutex" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}//多个线程互斥访问
//send data
hServerWriteOver = CreateEvent(NULL,
FALSE,
FALSE,
L"ServerWriteOver");
hClientReadOver = CreateEvent(NULL,
FALSE,
TRUE,
L"ClientReadOver");
if (NULL == hServerWriteOver ||
NULL == hClientReadOver)
{
cout << "CreateEvent" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}
do
{
if (WaitForSingleObject(hClientReadOver, 5 * 1000) != WAIT_OBJECT_0) goto SERVER_SHARE_MEMORY_END;
q[0] = ++p;
Sleep(500);
if (!SetEvent(hServerWriteOver)) goto SERVER_SHARE_MEMORY_END;//把指定的事件对象设置为有信号状态
} while (p != 10);
SERVER_SHARE_MEMORY_END:
//release share memory
if (NULL != hServerWriteOver) CloseHandle(hServerWriteOver);
if (NULL != hClientReadOver) CloseHandle(hClientReadOver);
if (NULL != lpShareMemory) UnmapViewOfFile(lpShareMemory);
if (NULL != hFileMapping) CloseHandle(hFileMapping);
if (NULL != hMutex) ReleaseMutex(hMutex);
return 0;
}
客户端
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
HANDLE hMutex = NULL;
HANDLE hFileMapping = NULL;
LPVOID lpShareMemory = NULL;
HANDLE hServerWriteOver = NULL;
HANDLE hClientReadOver = NULL;
char p = 0;
char* q = NULL;
hMutex = OpenMutex(MUTEX_ALL_ACCESS,
FALSE,
L"SM_Mutex");
if (NULL == hMutex)
{
if (ERROR_FILE_NOT_FOUND == GetLastError())
{
cout << "OpenMutex fail: file not found!" << endl;
goto CLIENT_SHARE_MEMORY_END;
}
else
{
cout << "OpenMutex fail:" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
}
if (WaitForSingleObject(hMutex, 5000) != WAIT_OBJECT_0)//hMutex 一旦互斥对象处于有信号状态,则该函数返回
{
DWORD dwErr = GetLastError();
goto CLIENT_SHARE_MEMORY_END;
}
//open share memory
hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS,
FALSE,
L"ShareMemoryTest");
if (NULL == hFileMapping)
{
cout << "OpenFileMapping" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
lpShareMemory = MapViewOfFile(hFileMapping,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
if (NULL == lpShareMemory)
{
cout << "MapViewOfFile" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
q = (char*)lpShareMemory;
//read and write data
hServerWriteOver = CreateEvent(NULL,
FALSE,
FALSE,
L"ServerWriteOver");
hClientReadOver = CreateEvent(NULL,
FALSE,
TRUE,
L"ClientReadOver");
if (NULL == hServerWriteOver ||
NULL == hClientReadOver)
{
cout << "CreateEvent" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
do
{
if (WaitForSingleObject(hServerWriteOver, INFINITE) != WAIT_OBJECT_0) goto CLIENT_SHARE_MEMORY_END;
p = q[0];
printf("%d\n",p);
if (!SetEvent(hClientReadOver)) goto CLIENT_SHARE_MEMORY_END;
} while (p != 10);
CLIENT_SHARE_MEMORY_END:
//release share memory
if (NULL != hServerWriteOver) CloseHandle(hServerWriteOver);
if (NULL != hClientReadOver) CloseHandle(hClientReadOver);
if (NULL != lpShareMemory) UnmapViewOfFile(lpShareMemory);
if (NULL != hFileMapping) CloseHandle(hFileMapping);
if (NULL != hMutex) ReleaseMutex(hMutex);
return 0;
}