A minimalistic SimConnect example: work but I received nothing with string request

Hi to all,
here found a .sln project modified for read the TITLE string (link).

I have modified the A minimalistic SimConnect example (original post), where I try to read the TITLE
(from : file:///C:/MSFS%20SDK/Documentation/04-Developer_Tools/SimConnect/SimConnect_Status_of_Simulation_Variables.html)

Why can’t I read string variables? Where is the mystery?

Hi @jockerfox72,

I tried myself and actually didn’t received any string out so far. I will have to look if I can get something or not.

I will try to keep you updated.

1 Like

This had me stumped for quite a while but the following helped a lot - it’s for p3D but seems to be consistent with MSFS https://www.prepar3d.com/SDKv4/sdk/simconnect_api/managed_simconnect_projects.html

The only gotcha is that you have to use “number” as the units parameter - no idea why but it only works if you do - found that little tidbit in a post on FSDeveloper.com - sorry don’t have the link handy.

This code works flawlessly for me returning string vars (“Title” in this example) - it’s VB but should translate to C# easily.

'Declare a structure for the string
Private Structure string256Struct
	<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public strVal As String
End Structure

'Register the data type in the OnRecvOpen callback
sim.AddToDataDefinition(CType(Index, DUMMYENUM), "Title", "number", SIMCONNECT_DATATYPE.STRING256, 0, SimConnect.SIMCONNECT_UNUSED)
sim.RegisterDataDefineStruct(Of string256Struct)(CType(Index, DUMMYENUM))

'Receive the data in the OnRecvSimobjectDataBytype callback
Dim t As string256Struct = CType(data.dwData(0), string256Struct)
dim strValue = t.strVal
1 Like

Yes now work fine !
The Marshal was the culprit ! :+1:

.

.
So here part of miniSimConnect converted :

 private struct string256Struct
    {
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string strVal;
    }

    Dictionary<int, string> simConnectProperties = new Dictionary<int, string>
    {
    { 1, "title," },
    };

.

private void Sim_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
    {
       lblStatus.Content = "Connected";
       foreach (var toConnect in simConnectProperties)
       {
          var values = toConnect.Value.Split(new char[] { ',' });
          sim.AddToDataDefinition((DUMMYENUM)toConnect.Key, values[0], values[1], SIMCONNECT_DATATYPE.STRING256, 0, SimConnect.SIMCONNECT_UNUSED);
          GetLabelForUid(100 + toConnect.Key).Content = values[1];
          /// IMPORTANT: Register it with the simconnect managed wrapper marshaller
          /// If you skip this step, you will only receive a uint in the .dwData field.
          sim.RegisterDataDefineStruct<string256Struct>((DUMMYENUM)toConnect.Key);
       }
    }

.

private void Sim_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
        {
            int iRequest = (int)data.dwRequestID;
            string256Struct t = (string256Struct)data.dwData[0];
            var strValue = t.strVal;
            GetLabelForUid(iRequest).Content = strValue;
        }
1 Like