Converting SDK example to C# does not work. Help needed

Hi,

I am developing a SimConnect Client app using C# and try to convert the example in the SDK documentation from cpp to C#

static enum DATA_DEFINE_ID {
DEFINITION3
};

hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION3, “Initial Position”, “NULL”, SIMCONNECT_DATATYPE_INITPOSITION, 0);
SIMCONNECT_DATA_INITPOSITION Init;
Init.Altitude = 5000.0;
Init.Latitude = 47.64210;
Init.Longitude = -122.13010;
Init.Pitch = -0.0;
Init.Bank = -1.0;
Init.Heading = 180.0;
Init.OnGround = 0;
Init.Airspeed = 0;
SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION3, SIMCONNECT_OBJECT_ID_USER, 0, sizeof(Init), &Init);

My C# code is as seen below, but it doesn’t work. What am I doing wrong?

simconnect.AddToDataDefinition(DEFINITIONS.DEFINITION3, “Init Pos”, “NULL”, SIMCONNECT_DATATYPE.INITPOSITION, 0.0f, SimConnect.SIMCONNECT_UNUSED);

SIMCONNECT_DATA_INITPOSITION Init = new SIMCONNECT_DATA_INITPOSITION();
Init.Altitude = 5000.0;
Init.Latitude = 47.64210;
Init.Longitude = -122.13010;
Init.Pitch = -0.0;
Init.Bank = -1.0;
Init.Heading = 180.0;
Init.OnGround = 0;
Init.Airspeed = 0;

simconnect.SetDataOnSimObject(DEFINITIONS.DEFINITION3(uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, Init);

Hello @sloppy99 !

With the information you have provided, I can see some issues :

  • AddToDataDefinition indeed takes a char as second parameter. This should match a Simulation Variable’s name. However "Init Pos" is not one of them, use "Initial Position" instead.
  • Also, you should send null, wich is the equivalent of cpp’s NULL in C#, instead of "NULL" as a char
  • I think you’ve forgotten a semilicon between DEFINITIONS.DEFINITION3 and SimConnect.SIMCONNECT_OBJECT_ID_USER :slight_smile:
  • You do not need to cast SimConnect.SIMCONNECT_OBJECT_ID_USER to a uint as it is already defined as one

Please tell us if you need more help to use SimConnect, and of course here is the documentation.

Best Regards,

Alexandre

1 Like

Thanks for your response and comments. :smile:

Now working OK after the recommended changes to the code.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.