FSX Position user a/c using c# code

connomar

Resource contributor
Do any of you knowledgeable folks have a code snippet showing how, using C# to position the user aircraft by sending it the lat lon coordinates. The code samples are all C++ of course and life is too short to reinvent the wheel.

I have code which can read the user data, I just want to reverse it.

Thanks
Martin
 

connomar

Resource contributor
Thanks DragonflightDesign & Jon for responding. I studied that code for ages, but got caught up with the complexities of c++ to c# plus not sending an array. Although I finally got it to work, I struggled with Exception 19 and 20. I ended up with:


private unsafe void PositionAircraft(string title,double lat,double lon)
{
if (!simconnectStatus) return;
//Create managed structure and insert wanted data
var s = new Struct1();
s.title = title;
s.latitude = lat;
s.longitude = lon;


simconnect.SetDataOnSimObject(DEFINITIONS.Struct1, 0, SIMCONNECT_DATA_SET_FLAG.DEFAULT, s);
MessageBox.Show($"Sent aircraft to position {lat},{lon}", "Step Complete", MessageBoxButtons.OK);


}


This code throws Exception 20, BUT it does position the aircraft, so I just ignore the exception. I was expecting to have to allocate memory for the structure and pass the pointer to the function.

Martin
 

DragonflightDesign

Resource contributor
It's possible that the Exception 20 is appearing because you appear to be creating the struct on-the-fly. SimConnect tends not to like that: it wants everything in place beforehand. Yes, it sounds awful and it caught me out a few times in all three languages but when you get used to it, it isn't too bad.
 

connomar

Resource contributor
I originally learned C on Unix and I think used Microsoft Version 3 C which came on 10 or more 3.5 inch floppies, but things have moved on faster than my brain.
 

connomar

Resource contributor
It's possible that the Exception 20 is appearing because you appear to be creating the struct on-the-fly. SimConnect tends not to like that: it wants everything in place beforehand. Yes, it sounds awful and it caught me out a few times in all three languages but when you get used to it, it isn't too bad.
Many thanks for the heads up on creating structures inside the method. That fixed it.

Martin
 
Top