1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<Windows.h>
4
5 #define SIZE 4096
6 //指针标识首地址
7 LPVOID lpdata = NULL;
8
9
10 void main()
11 {
12 if (lpdata!=NULL)
13 {
14 puts("共享内存存在");
15 }
16 //创建共享内存
17 HANDLE hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,
18 PAGE_READWRITE | SEC_COMMIT, 0, SIZE, "cgwmem");
19
20 if (hmap==NULL)
21 {
22 puts("创建失败");
23 }
24 else
25 {
26 //映射文件到指针
27 lpdata = MapViewOfFile(hmap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
28
29 int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
30 memcpy(lpdata, a, 40);
31 }
32
33 system("pause");
34
35
36 UnmapViewOfFile(lpdata);//解除
37 CloseHandle(hmap);
38 system("pause");
39
40
41 }