You can ship your own airport database, and it'll be wrong for somebody. The sim already knows every runway it has loaded, add on scenery included, and the facility API hands it to you.
The setup is the same project file as in the SimConnect basics.
How a facility request is shaped
You build a definition out of strings, like nested brackets. OPEN AIRPORT, the fields you want, OPEN RUNWAY, its fields, then close both. The sim answers with one message for the airport, one for each runway, and an end marker.
The program
using System.Runtime.InteropServices;
using Microsoft.FlightSimulator.SimConnect;
using var ready = new EventWaitHandle(false, EventResetMode.AutoReset);
using var sim = new SimConnect("Runway lookup", IntPtr.Zero, 0, ready, 0);
var icao = args.Length > 0 ? args[0].ToUpperInvariant() : "KSEA";
var running = true;
foreach (var field in new[]
{
"OPEN AIRPORT", "NAME64", "LATITUDE", "LONGITUDE", "N_RUNWAYS",
"OPEN RUNWAY", "HEADING", "LENGTH", "WIDTH", "PRIMARY_NUMBER", "PRIMARY_DESIGNATOR", "SECONDARY_NUMBER", "SECONDARY_DESIGNATOR", "CLOSE RUNWAY",
"CLOSE AIRPORT"
})
{
sim.AddToFacilityDefinition(Id.Airport, field);
}
sim.RegisterFacilityDataDefineStruct<Airport>(SIMCONNECT_FACILITY_DATA_TYPE.AIRPORT);
sim.RegisterFacilityDataDefineStruct<Runway>(SIMCONNECT_FACILITY_DATA_TYPE.RUNWAY);
static string End(int number, int designator) =>
number.ToString("00") + designator switch { 1 => "L", 2 => "R", 3 => "C", _ => "" };
sim.OnRecvFacilityData += (_, data) =>
{
switch ((SIMCONNECT_FACILITY_DATA_TYPE)data.Type)
{
case SIMCONNECT_FACILITY_DATA_TYPE.AIRPORT:
var a = (Airport)data.Data[0];
Console.WriteLine($"{a.Name} ({a.Latitude:F4}, {a.Longitude:F4}), {a.Runways} runways");
break;
case SIMCONNECT_FACILITY_DATA_TYPE.RUNWAY:
var r = (Runway)data.Data[0];
Console.WriteLine($" {End(r.PrimaryNumber, r.PrimaryDesignator)}/{End(r.SecondaryNumber, r.SecondaryDesignator)} {r.Length * 3.28084:F0} x {r.Width * 3.28084:F0} ft, heading {r.Heading:F0}");
break;
}
};
sim.OnRecvFacilityDataEnd += (_, _) => running = false;
sim.OnRecvException += (_, e) => Console.WriteLine($"SimConnect error {(SIMCONNECT_EXCEPTION)e.dwException}");
sim.RequestFacilityData(Id.Airport, Id.Airport, icao, "");
while (running && ready.WaitOne(10000))
{
sim.ReceiveMessage();
}
enum Id { Airport }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct Airport
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string Name;
public double Latitude, Longitude;
public int Runways;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Runway
{
public float Heading, Length, Width;
public int PrimaryNumber, PrimaryDesignator, SecondaryNumber, SecondaryDesignator;
}
