To put it bluntly, IPC is inter-process communication.
There are many methods for inter-process communication. For example, after a port is created, the multicast technology is used for handshake connection. Here we will talk about the implementation through the memory file ing method.
First, add related API functions to the project:
[DllImport("kernel32.dll", EntryPoint = "OpenFileMapping", SetLastError = true, CharSet = CharSet.Auto)] private static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, String lpName); [DllImport("Kernel32.dll")] private static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap); [DllImport("Kernel32.dll", EntryPoint = "UnmapViewOfFile", SetLastError = true, CharSet = CharSet.Auto)] private static 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 sharing side:
String shareName = "XiaoY_H Memory Sharing"; char [] myShrCntnt = "Shared Memory password Fuck985211 ". toCharArray (); IntPtr hMapFile = CreateFileMapping (INVALID_HANDLE_VALUE, IntPtr. 0, PAGE_READWRITE, 0,256, shareName); IntPtr pBuf = MapViewOfFile (hMapFile, FILE_MAP_ALL_ACCESS, 0, 0,256); Marshal. copy (myShrCntnt, 0, pBuf, myShrCntnt. length); Console. writeLine ("after the shared memory is created, it will be here: IntPtr-> {0: G}", pBuf. toInt32 (); Console. readKey (); UnmapViewOfFile (pBuf); CloseHandle (hMapFile );
Add code to the shared terminal:
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,256); if (pBuf = IntPtr. Zero) {Console. WriteLine (" Null Buffer! "); Return;} Console. writeLine ("Shared Memory Data:" + Marshal. ptrToStringUni (pBuf); Console. readKey (); UnmapViewOfFile (pBuf); CloseHandle (hMapFile );