-
Notifications
You must be signed in to change notification settings - Fork 15
/
shared_mem.c
68 lines (54 loc) · 1.09 KB
/
shared_mem.c
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
58
59
60
61
62
63
64
65
66
67
68
#include "shared_mem.h"
#include "error.h"
HANDLE mapHandle = NULL;
bool readOrWriteToSharedMemory(DWORD write, DWORD* read) {
bool succeeded = false;
mapHandle = CreateFileMappingW(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(DWORD),
L"LightWMThreadId"
);
if (mapHandle == NULL) {
reportWin32Error(L"Could not create file mapping object");
goto cleanup;
}
LPVOID mapAddress = MapViewOfFile(
mapHandle,
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof(DWORD)
);
if (mapAddress == NULL) {
reportWin32Error(L"Could not map view of file");
goto cleanup;
}
if (write) {
*(DWORD*)mapAddress = write;
} else if (read) {
*read = *(DWORD*)mapAddress;
}
succeeded = true;
cleanup:
if (mapAddress) {
UnmapViewOfFile(mapAddress);
}
if (read) {
cleanupMemoryMapFile();
}
return succeeded;
}
void cleanupMemoryMapFile() {
if (mapHandle) {
CloseHandle(mapHandle);
}
}
bool retrieveDwordFromSharedMemory(DWORD* output) {
return readOrWriteToSharedMemory(0, output);
}
bool storeDwordInSharedMemory(DWORD input) {
return readOrWriteToSharedMemory(input, 0);
}