-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilemapping01.c
More file actions
121 lines (95 loc) · 2.44 KB
/
filemapping01.c
File metadata and controls
121 lines (95 loc) · 2.44 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* filemapping01.c */
/* http://www.kumei.ne.jp/c_lang/intro2/no_114.htm */
#include <stdio.h>
#include <windows.h>
#include <conio.h>
int FRead(LPSTR);
int FWrite(LPSTR);
HANDLE hFMWrite;
int main()
{
static LPSTR lpAddress1, lpAddress2;
char szNo[8];
BOOL bEnd = FALSE;
while (1) {
printf("***************\n");
printf("1. Writre\n");
printf("2. Read\n");
printf("0. End\n");
printf("***************\n");
printf("No. = ");
gets(szNo);
switch (szNo[0]) {
case '1':
FWrite(lpAddress1);
break;
case '2':
FRead(lpAddress2);
break;
case '0':
bEnd = TRUE;
break;
default:
printf("Wrong Number !\n\n");
break;
}
if (bEnd)
break;
}
if (hFMWrite) {
if (CloseHandle(hFMWrite) == 0)
printf("ERROR wirte-handle close\n");
else
printf("Success wirte-handle close\n");
}
return 0;
}
int FWrite(LPSTR lpStr)
{
char szStr[1024];
if (!hFMWrite)
CloseHandle(hFMWrite);
hFMWrite = CreateFileMapping(
(HANDLE)-1,
NULL,
PAGE_READWRITE,
0,
1024,
"NEKODEMOWAKARU");
if (hFMWrite == NULL)
return -1;
if (GetLastError() == ERROR_ALREADY_EXISTS)
printf("Already exist Mappingfile\n");
lpStr = (LPSTR)MapViewOfFile(hFMWrite,
FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (lpStr == NULL)
return -2;
printf("Write==");
gets(szStr);
strcpy(lpStr, szStr);
strcat(lpStr, "\n");
UnmapViewOfFile(lpStr);
printf("Write Done\n");
return 0;
}
int FRead(LPSTR lpStr)
{
HANDLE hFM;
hFM = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "NEKODEMOWAKARU");
lpStr = (LPSTR)MapViewOfFile(hFM,
FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (lpStr == NULL) {
printf("ERROR Rx\n");
CloseHandle(hFM);
return -1;
}
printf(lpStr);
if (UnmapViewOfFile(lpStr) == 0) {
printf("ERROR: Address Mapping for Write\n");
} else {
printf("SUCCESS: Address Mapping for Write\n");
lpStr = NULL;
}
CloseHandle(hFM);
return 0;
}