-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc.c
80 lines (68 loc) · 1.8 KB
/
ipc.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
69
70
71
72
73
74
75
76
77
78
79
80
/* Inter-process Communication */
#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include "ipc.h"
static STARTUPINFO StartupInfo;
static PROCESS_INFORMATION ProcessInfo;
static char *program = "ezshare.exe child";
//static char *program = "a.exe child";
int spawn(char* parentName)
{
memset(&StartupInfo, 0, sizeof(StartupInfo));
memset(&ProcessInfo, 0, sizeof(ProcessInfo));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
char str[80];
strcpy(str, program);
strcat(str, parentName);
if (!CreateProcess(NULL, str, NULL, NULL, FALSE,
0, NULL, NULL, &StartupInfo, &ProcessInfo)) {
return 0;
}
return 1;
}
int lock(void* whom)
{
return WaitForSingleObject(whom, INFINITE);
}
int unlock(void* whom)
{
return ReleaseSemaphore(whom, 1, NULL);
}
static HANDLE hMemory;
char* mapMemory(uint32_t memsize, char* memoryName, int child)
{
if (child) {
hMemory = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
memoryName);
}
else {
hMemory=CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,PAGE_READWRITE,0,
memsize,memoryName);
}
assert(hMemory!=NULL);
return (char*) MapViewOfFile(hMemory,
FILE_MAP_WRITE,
0, 0, 0);
}
void* init(char *parentName)
{
HANDLE semaphore;
if (parentName == "") {
return OpenSemaphore(SYNCHRONIZE|SEMAPHORE_MODIFY_STATE,
FALSE, "Global\\EZShare");
}
else {
semaphore = CreateSemaphore(NULL, 1, 1, "Global\\EZShare");
if (!spawn(parentName)) {
return NULL;
}
return semaphore;
}
}