aboutsummaryrefslogtreecommitdiff
path: root/Plugins/sharemem.h
blob: a26d198f42b343ca74ddecd0e2c6669803bea984 (plain)
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
#ifndef __SHAREMEM_H
#define __SHAREMEM_H

#include <windows.h>
#include <memory.h>

//
// allocate named shared memory
//
LPVOID smalloc(HANDLE *h, char *name, DWORD size)
{
	LPVOID p;

	*h = CreateFileMapping(
	 INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
	 size, name
	);

	if(*h==NULL) return(NULL);

	p = MapViewOfFile( *h, FILE_MAP_WRITE, 0, 0, 0);

	if(p==NULL)
	{
		CloseHandle(*h);
		return(NULL);
	}
	return(p);
}

//
// open access to named shared memory
//
LPVOID smopen(HANDLE *h, char *name)
{
	LPVOID p;

	*h = OpenFileMapping(FILE_MAP_ALL_ACCESS, TRUE, name);

	if(*h==NULL) return(NULL);

	p = MapViewOfFile(*h, FILE_MAP_WRITE, 0, 0, 0);

	if(p==NULL)
	{
		CloseHandle(*h);
		return(NULL);
	}
	return(p);
}

//
// cleanup
//
void smfree(HANDLE *h, LPVOID p)
{
	UnmapViewOfFile(p);
	CloseHandle(*h);
}

#endif