对win32api的封装 using System;using System.Runtime.InteropServices;namespace WindowsApplication1...{ internal class Win32 ...{ public static readonly IntPtr InvalidHandleValue = new IntPtr(-1); public const UInt32 FILE_MAP_WRITE = 2; public const UInt32 PAGE_READWRITE = 0x04; [DllImport("Kernel32")] public static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr pAttributes, UInt32 flProtect, UInt32 dwMaximumSizeHigh, UInt32 dwMaximumSizeLow, String pName); [DllImport("Kernel32")] public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, UInt32 dwDesiredAccess, UInt32 dwFileOffsetHigh, UInt32 dwFileOffsetLow, IntPtr dwNumberOfBytesToMap); [DllImport("Kernel32")] public static extern Boolean UnmapViewOfFile(IntPtr address); }} 测试用的winform设计界面 写入内存映射文件的代码 private void Write_Click(object sender, System.EventArgs e) ...{ IntPtr hFileMap = IntPtr.Zero; IntPtr address; hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue, IntPtr.Zero, Win32.PAGE_READWRITE, 0, 0x100, "MYSHARE"); if (hFileMap == IntPtr.Zero) throw new Exception("CreateFileMapping():无法创建内存映像文件"); address = Win32.MapViewOfFile(hFileMap, Win32.FILE_MAP_WRITE, 0, 0, IntPtr.Zero); byte[] bs = System.Text.Encoding.GetEncoding("gb2312").GetBytes(this.textBox1.Text); for(int i=0;i<bs.Length;i++) ...{ Marshal.WriteByte(address,i,bs[i]); } Marshal.WriteByte(address,bs.Length,0); Win32.UnmapViewOfFile(address); } 从内存映射文件读出的代码 private void Read_Click(object sender, System.EventArgs e) ...{ IntPtr hFileMap = IntPtr.Zero; IntPtr address; hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue, IntPtr.Zero, Win32.PAGE_READWRITE, 0, 0x100, "MYSHARE"); if (hFileMap == IntPtr.Zero) throw new Exception("CreateFileMapping():无法创建内存映像文件"); address = Win32.MapViewOfFile(hFileMap, Win32.FILE_MAP_WRITE, 0, 0, IntPtr.Zero); byte[] bs = new byte[0x100]; int i; for(i=0;i<bs.Length;i++) ...{ byte temp = Marshal.ReadByte(address,i); if(temp==0) break; bs[i] = temp; } Win32.UnmapViewOfFile(address); this.textBox2.Text = System.Text.Encoding.GetEncoding("gb2312").GetString(bs,0,i); } 测试结果截图