[. NET Process Communication] A simple implementation of inter-process communication in. NET

Source: Internet
Author: User

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 );




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.