From 18daa3e97cf4b8b65473fb52b2717ebc3c5f3770 Mon Sep 17 00:00:00 2001 From: bstudtma Date: Sat, 23 Aug 2025 03:16:50 -0500 Subject: [PATCH 1/4] Add header structure and make Value Object type --- .../Structs/SimConnectRecvGetInputEvent.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs b/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs index 13bcba9..5151e04 100644 --- a/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs +++ b/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs @@ -8,6 +8,27 @@ namespace SimConnect.NET /// The SimConnectRecvGetInputEvent structure is used to return the value of a specific input event. /// public struct SimConnectRecvGetInputEvent + { + /// + /// Gets or sets the client-defined request ID. + /// + public uint RequestId { get; set; } + + /// + /// Gets or sets the type of the input event. This is used to cast the Value to the correct type. + /// + public SimConnectInputEventType Type { get; set; } + + /// + /// Gets or sets the value of the requested input event, which should be cast to the correct format (float / string). + /// + public Object Value { get; set; } + } + + /// + /// The SimConnectRecvGetInputEvent structure is used to return the value of a specific input event. + /// + public struct SimConnectRecvGetInputEventHeader { /// /// Gets or sets the total size of the returned structure in bytes. @@ -33,10 +54,5 @@ public struct SimConnectRecvGetInputEvent /// Gets or sets the type of the input event. This is used to cast the Value to the correct type. /// public SimConnectInputEventType Type { get; set; } - - /// - /// Gets or sets the value of the requested input event, which should be cast to the correct format (float / string). - /// - public IntPtr Value { get; set; } } } From 6fdc9a0a53ff6d32162ef36e484e722f337504af Mon Sep 17 00:00:00 2001 From: bstudtma Date: Sat, 23 Aug 2025 03:44:12 -0500 Subject: [PATCH 2/4] Fix getinput event --- .../InputEvents/InputEventManager.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/SimConnect.NET/InputEvents/InputEventManager.cs b/src/SimConnect.NET/InputEvents/InputEventManager.cs index 11a939d..f89766e 100644 --- a/src/SimConnect.NET/InputEvents/InputEventManager.cs +++ b/src/SimConnect.NET/InputEvents/InputEventManager.cs @@ -671,26 +671,39 @@ private void ProcessGetInputEvent(IntPtr ppData) { try { - var recvGet = Marshal.PtrToStructure(ppData); + var recvGet = Marshal.PtrToStructure(ppData); + int headerSize = Marshal.SizeOf(); + int totalSize = checked((int)recvGet.hdr.dwSize); + int payloadSize = totalSize - headerSize; + IntPtr pValue = IntPtr.Add(ppData, headerSize); + double v = Marshal.PtrToStructure(pValue); // Extract value based on the type object value; switch (recvGet.Type) { case SimConnectInputEventType.DoubleValue: - value = Marshal.PtrToStructure(recvGet.Value); + value = Marshal.PtrToStructure(pValue); break; case SimConnectInputEventType.StringValue: - value = Marshal.PtrToStringAnsi(recvGet.Value) ?? string.Empty; + byte[] buf = new byte[payloadSize]; + Marshal.Copy(pValue, buf, 0, buf.Length); + int nul = Array.IndexOf(buf, (byte)0); + int len = (nul >= 0 ? nul : buf.Length); + value = Encoding.ASCII.GetString(buf, 0, len); break; default: - value = recvGet.Value.ToInt64(); + byte[] buf = new byte[payloadSize]; + Marshal.Copy(pValue, buf, 0, buf.Length); + int nul = Array.IndexOf(buf, (byte)0); + int len = (nul >= 0 ? nul : buf.Length); + value = buf break; } var inputEventValue = new InputEventValue { - Hash = recvGet.RequestId, + // Hash = recvGet.RequestId, Type = recvGet.Type, Value = value, }; From a6bc0731f3d298d20e2c0b9cc4d184ef9d7e9da7 Mon Sep 17 00:00:00 2001 From: bstudtma Date: Sat, 23 Aug 2025 03:50:01 -0500 Subject: [PATCH 3/4] Replace SimConnectRecvGetInputEvent with header structure --- .../Structs/SimConnectRecvGetInputEvent.cs | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs b/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs index 5151e04..12b5332 100644 --- a/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs +++ b/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs @@ -5,28 +5,7 @@ namespace SimConnect.NET { /// - /// The SimConnectRecvGetInputEvent structure is used to return the value of a specific input event. - /// - public struct SimConnectRecvGetInputEvent - { - /// - /// Gets or sets the client-defined request ID. - /// - public uint RequestId { get; set; } - - /// - /// Gets or sets the type of the input event. This is used to cast the Value to the correct type. - /// - public SimConnectInputEventType Type { get; set; } - - /// - /// Gets or sets the value of the requested input event, which should be cast to the correct format (float / string). - /// - public Object Value { get; set; } - } - - /// - /// The SimConnectRecvGetInputEvent structure is used to return the value of a specific input event. + /// The SimConnectRecvGetInputEventHeader structure is used to return the header of a specific input event including the type but without the value. /// public struct SimConnectRecvGetInputEventHeader { From aac05515bdd0e9718dc66317380897df9d6372b7 Mon Sep 17 00:00:00 2001 From: bstudtma Date: Sat, 23 Aug 2025 06:38:33 -0500 Subject: [PATCH 4/4] Fix SimConnect_GetInputEvent --- .../Enums/SimConnectInputEventType.cs | 5 ---- .../InputEvents/InputEventManager.cs | 27 ++++++++++--------- ...s => SimConnectRecvGetInputEventHeader.cs} | 2 +- 3 files changed, 16 insertions(+), 18 deletions(-) rename src/SimConnect.NET/Structs/{SimConnectRecvGetInputEvent.cs => SimConnectRecvGetInputEventHeader.cs} (94%) diff --git a/src/SimConnect.NET/Enums/SimConnectInputEventType.cs b/src/SimConnect.NET/Enums/SimConnectInputEventType.cs index 718a3ab..233196f 100644 --- a/src/SimConnect.NET/Enums/SimConnectInputEventType.cs +++ b/src/SimConnect.NET/Enums/SimConnectInputEventType.cs @@ -10,11 +10,6 @@ namespace SimConnect.NET /// public enum SimConnectInputEventType { - /// - /// No data type specification required (C++ only). - /// - None, - /// /// Specifies a double value. /// diff --git a/src/SimConnect.NET/InputEvents/InputEventManager.cs b/src/SimConnect.NET/InputEvents/InputEventManager.cs index f89766e..d5d6d92 100644 --- a/src/SimConnect.NET/InputEvents/InputEventManager.cs +++ b/src/SimConnect.NET/InputEvents/InputEventManager.cs @@ -8,6 +8,7 @@ using System.Globalization; using System.Linq; using System.Runtime.InteropServices; +using System.Text; using System.Threading; using System.Threading.Tasks; using SimConnect.NET.Events; @@ -179,7 +180,9 @@ await Task.Run( using (cancellationToken.Register(() => tcs.TrySetCanceled())) { - return await tcs.Task.ConfigureAwait(false); + var value = await tcs.Task.ConfigureAwait(false); + value.Hash = hash; // Set the hash for reference + return value; } } finally @@ -591,14 +594,13 @@ private void ProcessEnumerateInputEvents(IntPtr ppData) var typeValue = Marshal.ReadInt32(currentPtr); var inputEventType = Enum.IsDefined(typeof(SimConnectInputEventType), typeValue) ? (SimConnectInputEventType)typeValue - : SimConnectInputEventType.None; // Default to None if invalid + : SimConnectInputEventType.DoubleValue; // Default to None if invalid // Convert to SimConnectDataType for the descriptor var type = inputEventType switch { SimConnectInputEventType.DoubleValue => SimConnectDataType.FloatDouble, SimConnectInputEventType.StringValue => SimConnectDataType.String128, - SimConnectInputEventType.None => SimConnectDataType.FloatDouble, // Default to double for unspecified type _ => SimConnectDataType.FloatDouble, // Default fallback }; @@ -672,11 +674,13 @@ private void ProcessGetInputEvent(IntPtr ppData) try { var recvGet = Marshal.PtrToStructure(ppData); - int headerSize = Marshal.SizeOf(); - int totalSize = checked((int)recvGet.hdr.dwSize); + int headerSize = Marshal.SizeOf(); + int totalSize = checked((int)recvGet.Size); int payloadSize = totalSize - headerSize; + System.Diagnostics.Debug.WriteLine($"[InputEventManager] Payload size: {payloadSize}"); IntPtr pValue = IntPtr.Add(ppData, headerSize); - double v = Marshal.PtrToStructure(pValue); + + // double v = Marshal.PtrToStructure(pValue); // Extract value based on the type object value; @@ -689,15 +693,14 @@ private void ProcessGetInputEvent(IntPtr ppData) byte[] buf = new byte[payloadSize]; Marshal.Copy(pValue, buf, 0, buf.Length); int nul = Array.IndexOf(buf, (byte)0); - int len = (nul >= 0 ? nul : buf.Length); + int len = nul >= 0 ? nul : buf.Length; value = Encoding.ASCII.GetString(buf, 0, len); break; default: - byte[] buf = new byte[payloadSize]; - Marshal.Copy(pValue, buf, 0, buf.Length); - int nul = Array.IndexOf(buf, (byte)0); - int len = (nul >= 0 ? nul : buf.Length); - value = buf + byte[] defBuf = new byte[payloadSize]; + Marshal.Copy(pValue, defBuf, 0, defBuf.Length); + value = defBuf; + value = BitConverter.ToDouble(defBuf, 0); break; } diff --git a/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs b/src/SimConnect.NET/Structs/SimConnectRecvGetInputEventHeader.cs similarity index 94% rename from src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs rename to src/SimConnect.NET/Structs/SimConnectRecvGetInputEventHeader.cs index 12b5332..1166c55 100644 --- a/src/SimConnect.NET/Structs/SimConnectRecvGetInputEvent.cs +++ b/src/SimConnect.NET/Structs/SimConnectRecvGetInputEventHeader.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) BARS. All rights reserved. //