-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHMEM_SERVER.cpp
42 lines (34 loc) · 1.15 KB
/
SHMEM_SERVER.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
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;
int main()
{
// Ñòâîðåííÿ ðåã³îíó ñï³ëüíî¿ ïàì'ÿò³
HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 1024, _T("MySharedMemory"));
if (hMapFile == NULL) {
cerr << "Could not create file mapping object (" << GetLastError() << ")" << endl;
return 1;
}
// ϳäêëþ÷åííÿ äî ðåã³îíó ñï³ëüíî¿ ïàì'ÿò³
LPTSTR pBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 1024);
if (pBuf == NULL) {
cerr << "Could not map view of file (" << GetLastError() << ")" << endl;
CloseHandle(hMapFile);
return 1;
}
// ×åêàºìî íà äàí³ â³ä ê볺íòà
cout << "Waiting for client data..." << endl;
while (_tcslen(pBuf) == 0) {
Sleep(100);
}
// Âèâåäåííÿ äàíèõ â³ä ê볺íòà
_tprintf(TEXT("Received data: %s\n"), pBuf);
// ϳäòâåðäæåííÿ ê볺íòó, ùî äàí³ îòðèìàíî
_tcscpy(pBuf, TEXT("Data received"));
// Çâ³ëüíåííÿ ðåñóðñ³â
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}