C#共享内存操作类实例:
- using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.Runtime.InteropServices;
-
-
namespace ShareMemLib
- {
-
public class ShareMem
- {
-
[DllImport("user32.dll", CharSet = CharSet.Auto)]
-
public static extern IntPtr SendMessage(IntPtr hWnd,
-
int Msg, int wParam, IntPtr lParam);
-
-
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
-
public static extern IntPtr CreateFileMapping(
-
int hFile, IntPtr lpAttributes, uint flProtect,
-
uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName);
-
-
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
-
public static extern IntPtr OpenFileMapping(
-
int dwDesiredAccess,
-
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,string lpName);
-
-
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
-
public static extern IntPtr MapViewOfFile(IntPtr
-
hFileMapping,uint dwDesiredAccess,
-
uint dwFileOffsetHigh, uint dwFileOffsetLow,
-
uint dwNumberOfBytesToMap);
-
-
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
-
public static extern bool UnmapViewOfFile(IntPtr pvBaseAddress);
-
-
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
-
public static extern bool CloseHandle(IntPtr handle);
-
-
[DllImport("kernel32", EntryPoint="GetLastError")]
-
public static extern int GetLastError ();
-
-
const int ERROR_ALREADY_EXISTS = 183;
-
-
const int FILE_MAP_COPY = 0x0001;
-
const int FILE_MAP_WRITE = 0x0002;
-
const int FILE_MAP_READ = 0x0004;
-
const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004;
-
-
const int PAGE_READONLY = 0x02;
-
const int PAGE_READWRITE = 0x04;
-
const int PAGE_WRITECOPY = 0x08;
-
const int PAGE_EXECUTE = 0x10;
-
const int PAGE_EXECUTE_READ = 0x20;
-
const int PAGE_EXECUTE_READWRITE = 0x40;
-
-
const int SEC_COMMIT = 0x8000000;
-
const int SEC_IMAGE = 0x1000000;
-
const int SEC_NOCACHE = 0x10000000;
-
const int SEC_RESERVE = 0x4000000;
-
-
const int 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()
- { <
|
请发表评论