瀏覽代碼

Merge pull request #716 from InvictusMB/marshaling

Fix InputRecord marshalling when built with .NET Native toolchain
Charlie Kindel 5 年之前
父節點
當前提交
1dff8de541
共有 1 個文件被更改,包括 20 次插入8 次删除
  1. 20 8
      Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

+ 20 - 8
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -364,7 +364,7 @@ namespace Terminal.Gui {
 		[DllImport ("kernel32.dll", EntryPoint = "ReadConsoleInputW", CharSet = CharSet.Unicode)]
 		public static extern bool ReadConsoleInput (
 			IntPtr hConsoleInput,
-			[Out] InputRecord [] lpBuffer,
+			IntPtr lpBuffer,
 			uint nLength,
 			out uint lpNumberOfEventsRead);
 
@@ -420,6 +420,24 @@ namespace Terminal.Gui {
 				return v;
 			}
 		}
+		
+		public InputRecord [] ReadConsoleInput ()
+		{
+			const int bufferSize = 1;
+			var pRecord = Marshal.AllocHGlobal (Marshal.SizeOf<InputRecord> () * bufferSize);
+			try {
+				ReadConsoleInput (InputHandle, pRecord, bufferSize,
+					out var numberEventsRead);
+
+				return numberEventsRead == 0
+					? null
+					: new [] {Marshal.PtrToStructure<InputRecord> (pRecord)};
+			} catch (Exception) {
+				return null;
+			} finally {
+				Marshal.FreeHGlobal (pRecord);
+			}
+		}
 
 #if false // See: https://github.com/migueldeicaza/gui.cs/issues/357
 		[StructLayout (LayoutKind.Sequential)]
@@ -577,13 +595,7 @@ namespace Terminal.Gui {
 				waitForProbe.Wait ();
 				waitForProbe.Reset ();
 
-				uint numberEventsRead = 0;
-
-				WindowsConsole.ReadConsoleInput (winConsole.InputHandle, records, 1, out numberEventsRead);
-				if (numberEventsRead == 0)
-					result = null;
-				else
-					result = records;
+				result = winConsole.ReadConsoleInput ();
 
 				eventReady.Set ();
 			}