Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SimConnect.NET/SimConnectNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static extern int SimConnect_AddToDataDefinition(
IntPtr hSimConnect,
uint defineId,
[MarshalAs(UnmanagedType.LPStr)] string datumName,
[MarshalAs(UnmanagedType.LPStr)] string unitsName,
[MarshalAs(UnmanagedType.LPStr)] string? unitsName,
uint datumType = 4,
float fEpsilon = 0,
uint datumId = SimConnectUnused);
Expand Down
21 changes: 19 additions & 2 deletions src/SimConnect.NET/SimVar/SimVarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,14 @@ internal void CancelRequest(ISimVarRequest request)
request.SetCanceled();
}

private static bool IsStringDataType(SimConnectDataType dataType) =>
dataType is SimConnectDataType.String8
or SimConnectDataType.String32
or SimConnectDataType.String64
or SimConnectDataType.String128
or SimConnectDataType.String256
or SimConnectDataType.String260;

private static SimConnectDataType InferDataType<T>()
{
var type = typeof(T);
Expand Down Expand Up @@ -850,12 +858,21 @@ private uint EnsureScalarDefinition(string name, string? unit = null, SimConnect
}
}

if (unit == null || dataType is not SimConnectDataType dt)
if (dataType is not SimConnectDataType dt)
{
throw new ArgumentException($"Unit and dataType must not be null for SimVar '{name}'.");
}

if (IsStringDataType(dt))
{
unit = null;
Comment on lines +866 to +868

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Struct subscriptions still fail

The fix only covers scalar subscriptions. Subscribe<T>(period, onValue) calls EnsureTypeDefinition<T>, which still passes unit ?? string.Empty to SimConnect_AddToDataDefinition. String fields therefore send "string" or an empty string instead of the required null pointer, so these subscriptions remain broken. Apply the same string-type check in that registration path.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/SimConnect.NET/SimVar/SimVarManager.cs
Line: 866-868

Comment:
**Struct subscriptions still fail**

The fix only covers scalar subscriptions. `Subscribe<T>(period, onValue)` calls `EnsureTypeDefinition<T>`, which still passes `unit ?? string.Empty` to `SimConnect_AddToDataDefinition`. String fields therefore send `"string"` or an empty string instead of the required null pointer, so these subscriptions remain broken. Apply the same string-type check in that registration path.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

}
else if (unit is null)
{
throw new ArgumentException($"Unit and dataType must not be null for SimVar '{name}'.");
}

var key = (Name: name, Unit: unit, DataType: dt);
var key = (Name: name, Unit: unit ?? string.Empty, DataType: dt);

if (this.dataDefinitions.TryGetValue(key, out var existingId))
{
Expand Down