超级难题:.net 中CreateFileMapping 创建共享内存问题 .net中可以通过InteropServices调用unmanaged库的方法CreateFileMapping等来创建和使用共享内存。但是如何将一个对象数组对应到创建的内存块呢?这样一来,内存创建后就不用管了,只要对对象数组进行操作就可以了,请高手指点。 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #region 非托管函数声明 [DllImport( "kernel32.dll ",EntryPoint= "OpenFileMapping ",SetLastError=true, CharSet=CharSet.Auto) ] private static extern IntPtr OpenFileMapping (int dwDesiredAccess, bool bInheritHandle,String lpName ); [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); [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(uint hHandle); [DllImport( "kernel32.dll ",EntryPoint= "GetLastError ",SetLastError=true,CharSet=CharSet.Auto)] private static extern uint GetLastError(); #endregion struct Money {}; Money[] g_Money = new Money[100]; for(int i = 0; i < 100; i++) {} try {} g_hMoney = MapViewOfFile(memoryFileHandle,(uint)983071,0,0,(uint)(100*8)); if(g_hMoney == IntPtr.Zero) {} int basePos = g_hMoney.ToInt32(); for(int j = 0; j < 100; j++) {} } catch(System.Exception exception) {} 我想要得到的效果就是:只要访问g_Money数组就可以直接访问内存,象VC中一样,不要每次对g_Money赋值后再调用Marshal.StructureToPtr修改内存,而每次内存修改后又用Marshal.StructureToPtr读取内存到g_Money来 |