- Server creates shared memory
1#include <stdio.h>2#include <stdlib.h>3#include <Windows.h>4 5 #defineSIZE 40966 //The pointer identifies the first address7LPVOID lpdata =NULL;8 9 Ten voidMain () One { A if(lpdata!=NULL) - { -Puts"Shared memory exists"); the } - //Create shared Memory -HANDLE Hmap =Createfilemappinga (Invalid_handle_value, NULL, -Page_readwrite | Sec_commit,0, SIZE,"Cgwmem"); + - if(hmap==NULL) + { APuts"creation failed"); at } - Else - { - //mapping files to pointers -lpdata = MapViewOfFile (Hmap, File_map_read | File_map_write,0,0,0); - in inta[Ten] = {1,2,3,4,5,6,7,8,9,0 }; -memcpy (Lpdata, A, +); to } + -System"Pause"); the * $UnmapViewOfFile (lpdata);//liftedPanax Notoginseng CloseHandle (hmap); -System"Pause"); the + A}
- The server uses this piece of shared memory
1#include <stdio.h>2#include <stdlib.h>3#include <Windows.h>4 5 voidMain ()6 {7HANDLE hmapfile = Openfilemappinga (File_map_read, FALSE,"Cgwgmem");8 if(Hmapfile = =NULL)9 {Tenprintf"failed to open up"); One } A - //Create pointers, point to this piece of memory -LPVOID lpbase = MapViewOfFile (Hmapfile, File_map_read,0,0,0); the if(Lpbase = =NULL) - { -printf"Open Failed"); - + } - + int*p =lpbase; A for(inti =0; I <Ten; i++) at { -printf"%d\n", P[i]); - } - - -UnmapViewOfFile (lpbase);//lifted in CloseHandle (hmapfile); - to +System"Pause"); -}
80. Shared Memory for process communication