-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamedSharedMemoryExample1.cpp
57 lines (44 loc) · 1.29 KB
/
NamedSharedMemoryExample1.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
// CONSOLE 1 - creator of the named shared memory block
#include <iostream>
#include <windows.h>
#include <conio.h>
struct MyData {
int i;
float f;
char c;
bool b;
double d;
};
int main() {
MyData myData = { 99, 7.56f, 'A', true, 69.42 };
// open a named shared memory block
HANDLE fileHandle = CreateFileMapping(INVALID_HANDLE_VALUE, // a handle to an existing virtual file, or invalid
nullptr, // optional security attributes
PAGE_READWRITE, // read/write access control
0, sizeof(MyData), // size of the memory block,
L"MySharedMemory");
if (fileHandle == nullptr) {
std::cout << "Could not create file mapping object: " << GetLastError() << std::endl;
return 1;
}
// map the memory from the shared block to a pointer we can manipulate
MyData* data = (MyData*)MapViewOfFile(fileHandle,
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof(MyData));
if (data == nullptr) {
std::cout << "Could not map view of file: " << GetLastError() << std::endl;
CloseHandle(fileHandle);
return 1;
}
// write to the memory block
*data = myData;
// wait for a keypress to close
_getch();
// unmap the memory block since we're done with it
UnmapViewOfFile(data);
// close the shared file
CloseHandle(fileHandle);
return 0;
}