【. NET process communication ". The simple implementation of interprocess communication in net

Source: Internet
Author: User

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 handshake connection, here is to be described through the memory file mapping method implementation.

Add the relevant API functions to your project first:

        [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 password Fuck985211". 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 on 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);        




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.