diff --git a/ScpiNet/UsbScpiConnection.cs b/ScpiNet/UsbScpiConnection.cs index 4d60c40..a227563 100644 --- a/ScpiNet/UsbScpiConnection.cs +++ b/ScpiNet/UsbScpiConnection.cs @@ -702,14 +702,13 @@ private byte[] CreateTmcRequest(UsbTmcMsgId msgId, bool eom, byte[] data, uint l byte[] buffer = new byte[bufferLength]; // Copy the header to the beginning of the array: - GCHandle handle = default; + IntPtr headerPtr = IntPtr.Zero; try { - handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); - Marshal.StructureToPtr(header, handle.AddrOfPinnedObject(), false); + headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UsbTmcHeader))); + Marshal.StructureToPtr(header, headerPtr, true); + Marshal.Copy(headerPtr, buffer, 0, Marshal.SizeOf(typeof(UsbTmcHeader))); } finally { - if (handle.IsAllocated) { - handle.Free(); - } + Marshal.FreeHGlobal(headerPtr); } // Add the write data after the header: @@ -787,14 +786,13 @@ public async Task Read(byte[] buffer, int readLength = -1, int speci } // Now convert data to the UsbTmcHeader structure: - GCHandle handle = default; + IntPtr headerPtr = IntPtr.Zero; try { - handle = GCHandle.Alloc(receptionBuffer, GCHandleType.Pinned); - header = (UsbTmcHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(UsbTmcHeader)); + headerPtr = Marshal.AllocHGlobal(headerSize); + Marshal.Copy(receptionBuffer, 0, headerPtr, headerSize); + header = Marshal.PtrToStructure(headerPtr); } finally { - if (handle.IsAllocated) { - handle.Free(); - } + Marshal.FreeHGlobal(headerPtr); } // Unfortunately Keysight multimeter leaves Tag fields always zero in the response, therefore this check cannot be used: