public static void getArrayFromSharedMemory <T>(T stSharedMemoryStruct, RTSharedMemory sharedMemory, int numberOfArray) { //T stSharedMemoryStruct = new T(); #if (!WITH_OUT_RTX) try { int sizeOfStruct = Marshal.SizeOf(stSharedMemoryStruct); /// Get the sizeof sharedmemory struct //int nLen = 84* countOfPacketToCopy; /// Declare a temporary byte array /// Declare pointer //IntPtr pPtr = Marshal.AllocHGlobal(nLen); /// Make sure the pointer offset is zero sharedMemory.Seek(numberOfArray * 84 * 200000, 0); /// Perform the shared memory read into byte array #if (WIN32) int byteReadSize = sharedMemory.Read(bylocArray, 0, sizeOfStruct * (int)200000); #else int byteReadSize = myRead(bylocArray, 0, sizeOfStruct * (int)200000, sharedMemory); #endif //Parallel.For() //if (byteReadSize != nLen) // return; /// Reset shared memory pointer to zero for future usage sharedMemory.Seek(0, 0); } catch { MessageBox.Show("Failed to get contents of shared memory"); } #else #endif //return stSharedMemoryStruct; }
public static UInt32 getParamOfVS_RTXSharedMemory(UInt32 stSharedMemoryStruct, RTSharedMemory sharedMemory) { //T stSharedMemoryStruct = new T(); #if (!WITH_OUT_RTX) try { /// Get the sizeof sharedmemory struct int nLen = Marshal.SizeOf(stSharedMemoryStruct); /// Declare a temporary byte array /// Declare pointer //IntPtr pPtr = Marshal.AllocHGlobal(nLen); /// Make sure the pointer offset is zero sharedMemory.Seek(84000000, 0); /// Perform the shared memory read into byte array #if (WIN32) int byteReadSize = sharedMemory.Read(byArray, 0, nLen); #else int byteReadSize = sharedMemory.Read(byArray, 0, nLen); //int byteReadSize = myRead(byArray, 0, 4, sharedMemory); #endif if (byteReadSize != nLen) { return(stSharedMemoryStruct); } /// Reset shared memory pointer to zero for future usage sharedMemory.Seek(0, 0); /// Pin the bytearray so that it cant be touched by the garbage collector GCHandle pinnedPacket = GCHandle.Alloc(byArray, GCHandleType.Pinned); //IntPtr pinnedPacket = Marshal.AllocHGlobal(Marshal.SizeOf(stSharedMemoryStruct)); /// Convert the raw buffer to our shared memory struct stSharedMemoryStruct = (UInt32)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(UInt32)); /// Free the pinned packet pinnedPacket.Free(); //Marshal.FreeHGlobal(pinnedPacket); //byArray = null; //GC.Collect(); //Marshal.PtrToStructure(sharedMemory.Address, stSharedMemoryStruct); return(stSharedMemoryStruct); } catch { MessageBox.Show("Failed to get contents of shared memory"); return(stSharedMemoryStruct); } #else #endif //return stSharedMemoryStruct; }
public static string ReadShareMemory() { // seek to start sharedMemory.Seek(0, SeekOrigin.Begin); // create binary reader var binRead = new BinaryReader(sharedMemory); // read each field in order strSharedParameter.test = binRead.ReadDouble(); binRead.ReadBytes(2); //iRTSS //strSharedParameter.iRTSS = binRead.ReadInt64(); binRead.ReadBytes(Marshal.SizeOf(strSharedParameter.strStick)); binRead.ReadBytes(Marshal.SizeOf(strSharedParameter.strStick2)); return(String.Empty); }
//private static RTSharedMemory e1000SharedMemory; public static void setContentsSharedMemory <T>(T stSharedMemoryStruct, RTSharedMemory sharedMemory) { #if (!WITH_OUT_RTX) try { /// Get the size of the shared memory structure int nLen = Marshal.SizeOf(stSharedMemoryStruct); /// Create a temp byte array /// Create a pointer IntPtr pPtr = Marshal.AllocHGlobal(nLen); //GCHandle gch = GCHandle.Alloc(arr, GCHandleType.Pinned); //MemSet(pPtr, 0, nLen); /// Convert the struct to a generic block of memory Marshal.StructureToPtr(stSharedMemoryStruct, pPtr, true); /// Copy the contents to the temp array Marshal.Copy(pPtr, byArr, 0, nLen); /// Write the temp array to shared memory //sharedMemory.Write(byArr, 0, nLen); myWrite(byArr, 0, nLen, sharedMemory); /// Reset the offset to zero sharedMemory.Seek(0, 0); //if (clearMemory) // sharedMemory.Flush(); /// Free the pointer Marshal.FreeHGlobal(pPtr); //byArr = null; //GC.Collect(); } catch { MessageBox.Show("Не удалось записать в общую область памяти"); //MessageBox.Show("Failed to set the contents of shared memory"); } #endif }
public static void getArrayFromSharedMemory2 <T>(ref T stSharedMemoryStruct, RTSharedMemory sharedMemory, int numberOfArray) { //T stSharedMemoryStruct = new T(); #if (!WITH_OUT_RTX) try { /// Get the sizeof sharedmemory struct //int nLen = 84* countOfPacketToCopy; /// Declare a temporary byte array /// Declare pointer //IntPtr pPtr = Marshal.AllocHGlobal(nLen); /// Make sure the pointer offset is zero sharedMemory.Seek(numberOfArray * 84, 0); /// Perform the shared memory read into byte array #if (WIN32) int byteReadSize = sharedMemory.Read(byArray, 0, 84); #else int byteReadSize = myRead(byArray, 0, 84, sharedMemory); #endif //Parallel.For() //if (byteReadSize != nLen) // return; /// Reset shared memory pointer to zero for future usage sharedMemory.Seek(0, 0); /// Pin the bytearray so that it cant be touched by the garbage collector //GCHandle pinnedPacket = GCHandle.Alloc(byArray, GCHandleType.Pinned); /// Convert the raw buffer to our shared memory struct //var temp = (T)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(T)); //IntPtr adrOfArray = Marshal.AllocHGlobal(Marshal.SizeOf(stSharedMemoryStruct)); //Marshal.StructureToPtr(stSharedMemoryStruct, adrOfArray, false); ////GCHandle pinnedPacket = GCHandle.Alloc(stSharedMemoryStruct, GCHandleType.Pinned); //Marshal.Copy(byArray, 0, adrOfArray, Marshal.SizeOf(stSharedMemoryStruct)); //stSharedMemoryStruct = (T)Marshal.PtrToStructure(adrOfArray, typeof(T)); ////pinnedPacket.Free(); //Marshal.FreeHGlobal(adrOfArray); GCHandle pinnedPacket = GCHandle.Alloc(byArray, GCHandleType.Pinned); /// Convert the raw buffer to our shared memory struct stSharedMemoryStruct = (T)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(T)); /// Free the pinned packet pinnedPacket.Free(); //GCHandle pinnedPacket1 = GCHandle.Alloc(stSharedMemoryStruct, GCHandleType.Pinned); //IntPtr AddrOfArray = pinnedPacket1.AddrOfPinnedObject() + countOfArray * countOfPacketToCopy * 84; //IntPtr AddrOfArray = new IntPtr((int)stSharedMemoryStruct.Vnesh_Svyazi[0].time_IRQ); //AddrOfArray += countOfArray * countOfPacketToCopy * 84; //Marshal.Copy(byArray,0, AddrOfArray, (84 * countOfPacketToCopy)); //Array.Copy(byArray, 0, (stSharedMemoryStruct as VNESH_SVYAZI[]), countOfArray * countOfPacketToCopy * 84, countOfPacketToCopy * 84); //stSharedMemoryStruct = (T)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(T)); /// Free the pinned packet //pinnedPacket.Free(); //pinnedPacket1.Free(); //byArray = null; //GC.Collect(); //Marshal.PtrToStructure(sharedMemory.Address, stSharedMemoryStruct); } catch { MessageBox.Show("Failed to get contents of shared memory"); } #else #endif //return stSharedMemoryStruct; }