Reprint Please specify source: http://blog.csdn.net/xiaoy_h/article/details/26090277
Needless to say, IPC is inter-process communication.
interprocess communication can be used in a number of ways, such as the creation of a port after the use of multicast technology for handshake connection, here is to be described by the method of memory file mapping implementation.
First add the relevant API functions to the project:
[DllImport ("kernel32.dll", EntryPoint = "OpenFileMapping", SetLastError = true, CharSet = CharSet.Auto)] pr Ivate static extern IntPtr openfilemapping (UINT dwdesiredaccess, bool binherithandle, String lpname); [DllImport ("Kernel32.dll")] private static extern IntPtr MapViewOfFile (IntPtr hfilemappingobject, uint DWDESIREDACC ESS, uint Dwfileoffsethigh, uint dwfileoffsetlow, uint dwnumberofbytestomap); [DllImport ("Kernel32.dll", EntryPoint = "UnmapViewOfFile", SetLastError = true, CharSet = CharSet.Auto)] Private St atic extern bool UnmapViewOfFile (IntPtr lpbaseaddress); [DllImport ("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool CloseHandle (IntPtr hhandle); [DllImport ("Kernel32.dll", EntryPoint = "CreateFileMapping", SetLastError = true, CharSet = CharSet.Auto)] Private static extern IntPtr createfilemapping (UINT hfile, IntPtr lpattributes, uint flproteCT, uint Dwmaximumsizehigh, uint dwmaximumsizelow, string lpname);
Then add the code on the shared side:
String shareName = "Xiaoy_h memory sharing"; Char[] myshrcntnt= "Shared memory passwordFuck985211". ToCharArray (); IntPtr hmapfile = createfilemapping (Invalid_handle_value, IntPtr.Zero, page_readwrite, 0, N, shareName); IntPtr PBuf = MapViewOfFile (hmapfile, file_map_all_access, 0, 0, N); Marshal.Copy (myshrcntnt, 0, PBuf, myshrcntnt.length); Console.WriteLine ("Shared memory is established, it is here: intptr->{0:g}", Pbuf.toint32 ()); Console.readkey (); UnmapViewOfFile (PBUF); CloseHandle (Hmapfile);
Add code to the shared side:
String shareName = "Xiaoy_h memory sharing"; IntPtr hmapfile = OpenFileMapping (File_map_all_access, False, shareName); if (Hmapfile==intptr.zero) { Console.WriteLine ("Null mapfile!"); return; } IntPtr PBuf = MapViewOfFile (hmapfile, file_map_all_access, 0, 0, N); if (Pbuf==intptr.zero) { Console.WriteLine ("Null buffer!"); return; } Console.WriteLine ("Shared Memory data:" + Marshal.ptrtostringuni (PBuf)); Console.readkey (); UnmapViewOfFile (PBUF); CloseHandle (Hmapfile);
【. NET process communication ". The simple implementation of interprocess communication in net