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 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 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; }