【.NET進程通訊】初探.NET中處理序間通訊的簡單的實現

來源:互聯網
上載者:User

標籤:c#   ipc   進程通訊   .net   

廢話不多說,IPC就是處理序間通訊。

處理序間通訊可以採用的方法很多,比如建立連接埠後採用組播技術進行握手串連,這裡要講到的就是通過記憶體檔案對應的方法實現。

先在項目中添加相關API函數:

        [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);        
然後在共用端添加代碼:

            string shareName = "XiaoY_H的記憶體共用";            char[] myShrCntnt="共用記憶體密碼Fuck985211".ToCharArray();            IntPtr hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, 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("共用記憶體建立完畢,它就在這兒:IntPtr->{0:G}",pBuf.ToInt32());            Console.ReadKey();            UnmapViewOfFile(pBuf);            CloseHandle(hMapFile);                   
在被共用端添加代碼:

            string shareName = "XiaoY_H的記憶體共用";            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("共用記憶體資料:" + Marshal.PtrToStringUni(pBuf));            Console.ReadKey();            UnmapViewOfFile(pBuf);            CloseHandle(hMapFile);        




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.