共享記憶體操作類 C 原始碼
阿新 • • 發佈:2018-12-20
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace ShareMemLib{ publicclass ShareMem { [DllImport("user32.dll", CharSet = CharSet.Auto)] publicstaticextern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam); [DllImport( "Kernel32.dll", CharSet = CharSet.Auto)] publicstaticextern IntPtr CreateFileMapping(int hFile, IntPtr lpAttributes, uint flProtect, uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName); [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] publicstaticextern IntPtr OpenFileMapping( int dwDesiredAccess,[MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,string lpName); [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] publicstaticextern IntPtr MapViewOfFile(IntPtr hFileMapping,uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow,uint dwNumberOfBytesToMap); [DllImport( "Kernel32.dll", CharSet = CharSet.Auto)] publicstaticexternbool UnmapViewOfFile(IntPtr pvBaseAddress); [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] publicstaticexternbool CloseHandle(IntPtr handle); [DllImport("kernel32", EntryPoint="GetLastError")] publicstaticexternint GetLastError (); constint ERROR_ALREADY_EXISTS =183; constint FILE_MAP_COPY =0x0001; constint FILE_MAP_WRITE =0x0002; constint FILE_MAP_READ =0x0004; constint FILE_MAP_ALL_ACCESS =0x0002|0x0004; constint PAGE_READONLY =0x02; constint PAGE_READWRITE =0x04; constint PAGE_WRITECOPY =0x08; constint PAGE_EXECUTE =0x10; constint PAGE_EXECUTE_READ =0x20; constint PAGE_EXECUTE_READWRITE =0x40; constint SEC_COMMIT =0x8000000; constint SEC_IMAGE =0x1000000; constint SEC_NOCACHE =0x10000000; constint SEC_RESERVE =0x4000000; constint INVALID_HANDLE_VALUE =-1; IntPtr m_hSharedMemoryFile = IntPtr.Zero; IntPtr m_pwData = IntPtr.Zero; bool m_bAlreadyExist =false; bool m_bInit =false; long m_MemSize=0; public ShareMem() { } ~ShareMem() { Close(); } ///<summary>/// 初始化共享記憶體 ///</summary>///<param name="strName">共享記憶體名稱</param>///<param name="lngSize">共享記憶體大小</param>///<returns></returns>publicint Init(string strName, long lngSize) { if (lngSize <=0|| lngSize >0x00800000) lngSize =0x00800000; m_MemSize = lngSize; if (strName.Length >0) { //建立記憶體共享體(INVALID_HANDLE_VALUE) m_hSharedMemoryFile = CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, (uint)PAGE_READWRITE, 0, (uint)lngSize, strName); if (m_hSharedMemoryFile == IntPtr.Zero) { m_bAlreadyExist =false; m_bInit =false; return2; //建立共享體失敗 } else { if (GetLastError() == ERROR_ALREADY_EXISTS) //已經建立 { m_bAlreadyExist =true; } else//新建立 { m_bAlreadyExist =false; } } //--------------------------------------- //建立記憶體對映 m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_WRITE, 0, 0, (uint)lngSize); if (m_pwData == IntPtr.Zero) { m_bInit =false; CloseHandle(m_hSharedMemoryFile); return3; //建立記憶體對映失敗 } else { m_bInit =true; if (m_bAlreadyExist ==false) { //初始化 } } //---------------------------------------- } else { return1; //引數錯誤 } return0; //建立成功 } ///<summary>/// 關閉共享記憶體 ///</summary>publicvoid Close() { if (m_bInit) { UnmapViewOfFile(m_pwData); CloseHandle(m_hSharedMemoryFile); } } ///<summary>/// 讀資料 ///</summary>///<param name="bytData">資料</param>///<param name="lngAddr">起始地址</param>///<param name="lngSize">個數</param>///<returns></returns>publicint Read(refbyte[] bytData, int lngAddr, int lngSize) { if (lngAddr + lngSize > m_MemSize) return2; //超出資料區if (m_bInit) { Marshal.Copy(m_pwData, bytData, lngAddr, lngSize); } else { return1; //共享記憶體未初始化 } return0; //讀成功 } ///<summary>/// 寫資料 ///</summary>///<param name="bytData">資料</param>///<param name="lngAddr">起始地址</param>///<param name="lngSize">個數</param>///<returns></returns>publicint Write(byte[] bytData, int lngAddr, int lngSize) { if (lngAddr + lngSize > m_MemSize) return2; //超出資料區if (m_bInit) { Marshal.Copy(bytData, lngAddr, m_pwData, lngSize); } else { return1; //共享記憶體未初始化 } return0; //寫成功 } }}