服务端:
#include "windows.h"
#include "stdio.h"
#define SHARE_MEMORY_NAME "share_memory_channel_i"
#define SHARE_MEMORY_MAX_SIZE 3840*2160*4
LPVOID share_memory_p_data = NULL;
int main(int argc, char* argv[])
{
HANDLE hMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, SHARE_MEMORY_MAX_SIZE, SHARE_MEMORY_NAME);
if (hMap == NULL)
{
printf("CreateFileMappingA error\n");
CloseHandle(hMap);
hMap = NULL;
return 0;
}
LPVOID share_memory_p_data = NULL;
share_memory_p_data = (CHAR*)MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
if (share_memory_p_data == NULL)
{
printf("view map error!");
return 0;
}
int* p = (int *)share_memory_p_data;
while (1)
{
if (!p[0]) {
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
memcpy(share_memory_p_data, a, 40);
}
Sleep(3000);
}
UnmapViewOfFile((LPCVOID)share_memory_p_data);
share_memory_p_data = NULL;
CloseHandle(hMap);
return 0;
}
客户端:
#include "windows.h"
#include "stdio.h"
#define SHARE_MEMORY_NAME "share_memory_channel_i"
#define SHARE_MEMORY_MAX_SIZE 3840*2160*4
LPVOID share_memory_p_data = NULL;
int main(int argc, char* argv[])
{
HANDLE hMap = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, SHARE_MEMORY_NAME);
if (hMap == NULL)
{
printf("hMap error\n");
CloseHandle(hMap);
hMap = NULL;
return 0;
}
share_memory_p_data = (CHAR*)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (share_memory_p_data == NULL)
{
printf("view map error!");
return 0;
}
int* p = (int*)share_memory_p_data;
while (1)
{
if (p[0]) {
for (int i = 0; i < 10;i++) {
printf("%d\n", p[i]);
}
int a[1] = { 0 };
memcpy(share_memory_p_data, a, 4);
}
Sleep(300);
}
UnmapViewOfFile((LPCVOID)share_memory_p_data);
share_memory_p_data = NULL;
CloseHandle(hMap);
return 0;
}