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; }
/// <summary> /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes. /// </summary> /// <param name="buffer"> /// An array of bytes. When this method returns, the buffer contains the specified /// byte array with the values between offset and (offset + count - 1) replaced /// by the bytes read from the current source. /// </param> /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param> /// <param name="count">The maximum number of bytes to be read from the current stream.</param> /// <returns> /// The total number of bytes read into the buffer. This can be less than the /// number of bytes requested if that many bytes are not currently available, /// or zero (0) if the end of the stream has been reached. /// </returns> public static int myRead(byte[] buffer, int offset, int count, RTSharedMemory sharedMemory) { IntPtr m_targetBaseAddress = sharedMemory.Address; long m_position = sharedMemory.Position; if (offset < 0) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0) { throw new ArgumentOutOfRangeException("count"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if ((offset + count) > buffer.Length) { throw new ArgumentException("Read request is larger than supplied buffer"); } IntPtr pos = new IntPtr(m_targetBaseAddress.ToInt64() + m_position); Marshal.Copy(pos, buffer, offset, count); sharedMemory.Position += count; return(count); }
public static bool OpenShareMemory() { const string sharedDataName = "rtxSHM"; var memorySize = Marshal.SizeOf(typeof(SharedParameter)); sharedMemory = new RTSharedMemory(memorySize, sharedDataName); string[] appParams = { "/h", "/s", "/f", String.Format("/m {0}", sharedDataName), "15" }; return(true); }
public static RTSharedMemory OpenSharedMemory <T>(string name_memory, ref T location) { #if (!WITH_OUT_RTX) UInt64 shareSize = (UInt64)Marshal.SizeOf(location); RTSharedMemory rtSharedMemory; try { RTSharedMemory.OpenExisting(name_memory); } catch { //clear_memory_bool = true; } /// Create shared memory for RTSS process try { rtSharedMemory = new RTSharedMemory((int)shareSize, name_memory); getContentsSharedMemory(ref location, rtSharedMemory); } catch { //MessageBox.Show("Can't open rtss shared memory"); return((RTSharedMemory)null); } /// Initialize shared memory //ClearStructsMemory(ref location); return(rtSharedMemory); #else int nLen = Marshal.SizeOf(location); /// Create a temp byte array byte[] byArr = new byte[nLen]; /// Create a pointer IntPtr pPtr = Marshal.AllocHGlobal(nLen); GCHandle pinnedPacket = GCHandle.Alloc(byArr, GCHandleType.Pinned); /// ConvertExt the raw buffer to our shared memory struct location = (T)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(T)); pinnedPacket.Free(); Marshal.FreeHGlobal(pPtr); return((RTSharedMemory)null); #endif }
//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; }
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; }