forked from mosamosa/GSD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedmem.cpp
More file actions
86 lines (67 loc) · 1.59 KB
/
sharedmem.cpp
File metadata and controls
86 lines (67 loc) · 1.59 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* GSD
*
* Copyright (C) 2008 mosa ◆e5bW6vDOJ. <[email protected]>
*
* This is free software with ABSOLUTELY NO WARRANTY.
*
* You can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License.
*
*/
#include <windows.h>
#include "sharedmem.h"
#include "tools.h"
//-----------------------------------------------------------------------------
CSharedMemory::CSharedMemory()
{
error = 0;
map = NULL;
data = NULL;
length = 0;
}
//-----------------------------------------------------------------------------
CSharedMemory::~CSharedMemory()
{
release();
}
//-----------------------------------------------------------------------------
bool CSharedMemory::create(const char *name, int len)
{
release();
map = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, len, name);
if(!map)
{
dprintf("CreateFileMapping() failed. (%s)", name);
return false;
}
error = GetLastError();
data = (char *)MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if(!data)
{
dprintf("MapViewOfFile() failed. (%s)", name);
CloseHandle(map);
map = NULL;
error = 0;
return false;
}
length = len;
if(data != NULL && error != ERROR_ALREADY_EXISTS)
{ // 最初に共有メモリを確保した
memset(data, 0, len);
}
return true;
}
//-----------------------------------------------------------------------------
void CSharedMemory::release()
{
if(data != NULL)
UnmapViewOfFile(data);
if(map != NULL)
CloseHandle(map);
error = 0;
map = NULL;
data = NULL;
length = 0;
}
//-----------------------------------------------------------------------------