• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

MSFS20 MSFS 2020 Managed Simconnect

Messages
53
Country
us-hawaii
I cannot build a C# application using the supplied DLL with the MSFS 2020 SDK. I have tried compiling in both x86 and x64. I have successfully compiled FSX and P3D SimConnect programs before. Is there anything I am missing here?

I am receiving the error "System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.FlightSimulator.SimConnect.dll' or one of its dependencies. The specified module could not be found." When I inspect the new Managed DLL it also states that it is missing dependencies.

Thanks!

-Matty
 
Last edited:
It needs to be in the same directory as your executable (see the managed sample, under "C:\MSFS SDK\Samples\SimvarWatcher\bin\x64\Release"). I use Fody/Costura to weave it into my executable
 
I have had success converting Prepar3d C# Sim Connect samples over to MSFS2020.
C# project needs to be x64 specific and both SimConnect.dll and SimConnect.cfg need to be in the same folder along with the Managed DLL and the app exe
 
Last edited:
You can check the SDK documentation for more information on the project setup. To make things easier they suggest to add a Post-Build Event to copy the SimConnect.dll to the target directory automatically:

Code:
xcopy /D /Y "$(MSFS_SDK)SimConnect SDK\lib\SimConnect.dll" "$(TargetDir)"

simconnect-dll.png
 
Last edited:
I had a small issue where I simply needed to surround $(TargetDir) in quotes.
Code:
xcopy /D /Y "$(MSFS_SDK)SimConnect SDK\lib\SimConnect.dll" "$(TargetDir)"

as I had spaces in my TargetDir filename I think!
 
Good point...if your target directory contains spaces you need to put that in brackets too, otherwise the xcopy command will fail. I adjusted my post accordingly.
 
I cannot build a C# application using the supplied DLL with the MSFS 2020 SDK. I have tried compiling in both x86 and x64. I have successfully compiled FSX and P3D SimConnect programs before. Is there anything I am missing here?

I am receiving the error "System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.FlightSimulator.SimConnect.dll' or one of its dependencies. The specified module could not be found." When I inspect the new Managed DLL it also states that it is missing dependencies.

Thanks!

-Matty
Hi Matty,
I have exactly the same problem (Visual Studio 2019, C#.Net,
I:\SDK\SimConnect SDK\lib\managed\Microsoft.FlightSimulator.SimConnect.dll, 64-Bit).
I never had a problem with FSX or P3D SimConnections.
Have you already a solution?
Is the Dll wrong and have we to wait for an Update?
You can directly answer to me on: drwm@chello.at.
Thanky you.
Best regards,
Wolfgang Meisner, Austria
 
I use VS Code but all I had to do to get the managed dll to work was:

Set the following in the csproj
Code:
<PlatformTarget>x64</PlatformTarget>



And then:
C#:
  <ItemGroup>
    <ContentWithTargetPath Include="$(MSFS_SDK)/SimConnect SDK/lib/SimConnect.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <TargetPath>SimConnect.dll</TargetPath>
    </ContentWithTargetPath>
  </ItemGroup>
 
I use VS Code but all I had to do to get the managed dll to work was:

Set the following in the csproj
Code:
<PlatformTarget>x64</PlatformTarget>



And then:
C#:
  <ItemGroup>
    <ContentWithTargetPath Include="$(MSFS_SDK)/SimConnect SDK/lib/SimConnect.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <TargetPath>SimConnect.dll</TargetPath>
    </ContentWithTargetPath>
  </ItemGroup>
I use VS Code but all I had to do to get the managed dll to work was:

Set the following in the csproj
Code:
<PlatformTarget>x64</PlatformTarget>



And then:
C#:
  <ItemGroup>
    <ContentWithTargetPath Include="$(MSFS_SDK)/SimConnect SDK/lib/SimConnect.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <TargetPath>SimConnect.dll</TargetPath>
    </ContentWithTargetPath>
  </ItemGroup>

Hi dear rupture,
Thank you for you precious answer.
In my case I had only to add to *.csproj your code-snippet:
<ItemGroup>
<ContentWithTargetPath Include="I:\SDK\SimConnect SDK\lib\SimConnect.dll">//My Path
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>SimConnect.dll</TargetPath>
</ContentWithTargetPath>
</ItemGroup>
And it works.
Microsoft relaeases all as it is, with no minimum documentation at all.
To hit on it spends to much time (I'm 75 years old and have not so much of it).
Thank you, you are my rescuer.
Have a nice time.

Wolfgang Meisner, Austria (drwm@chello.at)
 
Hi dear rupture,
...
Microsoft relaeases all as it is, with no minimum documentation at all.
To hit on it spends to much time (I'm 75 years old and have not so much of it).
Thank you, you are my rescuer.
Have a nice time.

Wolfgang Meisner, Austria (drwm@chello.at)

Wolfgangmeisner,

I've been a developer for 15+ years while I'm 39 I am not surprised about the documentation. We all wish it was better. I was on vacation last week and spent my vacation trying to write something to use SimConnect to control autopilot in MSFS. 50% of my time was spent just getting the basics in place to be able to compile and use the Managed DLL and then 40% was spent trying to find out how to do certain things. I found the documentation missing EVENT IDs that I need to control Vertical Speed. The EVENT IDs existed and work, but MSFS and Prepar3d both do not have the ID documented. After I got that sorted I was able to continue.

I hope you're journey is a little smoother now that you are up and running. I also hope I'm still doing some development when I'm your age.

Cheers
-Rup
 
Hi Rup,
Thanks for your warm answer.
But another problem I posted this day to the Forum:

In Visual Studio 2019 and C#.Net (WPF) I can establish a working connection to MSFS
(is setting up with an aircraft before takeoff).
Subscribed events like OnRecvOpen are fired correctly.
But the subscrition to the RequestDataOnSimObjectType will not fire.
My code snippets:

using Microsoft.FlightSimulator.SimConnect;
enum ENUM {A=0}
static SimConnect smcn; //Global
--------------------
void connect() {
HwndSource src;
src=HwndSource.FromHwnd (new WindowInteropHelper (this).Handle); //this is my WPF-WindowClasss
smcn=new SimConnect("Any", src.Handle, 0x0402, null, 0);
src.AddHook(new HwndSourceHook (WndProc));
smcn.OnRecvSimobjectDataBytype+=
new SimConnect.RecvSimobjectDataBytypeEventHandler (OnGetDataByte);
//And now Call it:
smcn.RequestDataOnSimObjectType (ENUM.A, ENUM.A, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
}
--------------------
static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wpar, IntPtr lpar, ref bool handled) {
switch(msg) {
case 0x0402:
handled=true; smcn.ReceiveMessage(); break;
}return IntPtr.Zero;
}
--------------------
void OnGetDataByte(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
IS NEVER BE FIRED !!!!!!!
}

Perhaps you are the expert, helping me.
Cheers,
Wolfgang, Austria
 
Wolfgang,

You are missing the
smcn.RegisterDataDefineStruct that tells SimConnect what the data looks like that you are wanting.

I can give you an example of say the planes location. This is something I'm doing to periodically write the location to a file so I can plot the flight on a map later on if I want to. I didn't include a full .cs file just copied the relevant parts and reworded some things to match how you're doing it. One thing I have two ENUMs one for DATA_REQUESTS and one for DEFINITIONS.

When my event is fired I can evaluate which definition it is so I know what structure to cast it into. Which is the purpose of the AddToDataDefinition. Make sure those are in the same order as the structure of your data and use the right DATATYPE

I hope this helps.

C#:
// You Need A Structure for the data
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct LOCAL_STRUCT
{
    //Position
    public double Plane_Latitude;
    public double Plane_Longitude;
    public double Plane_Altitude;
}

public enum DATA_REQUESTS
{
    REQUEST_1,
}

// I setup multiple under this enum, here for different data that may want ever second, or only once etc.
public enum DEFINITIONS
{
    LOCATION,
}

// Then you create your SimConnect like you are.

// Register your Event handlers like you are
// OnRecvSimobjectData += .... What you have is fine
//

// Here is what you're missing:
smcn.AddToDataDefinition(DEFINITIONS.LOCATION, "Plane Latitude", "degrees latitude", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
smcn.AddToDataDefinition(DEFINITIONS.LOCATION, "Plane Longitude", "degrees longitude", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
smcn.AddToDataDefinition(DEFINITIONS.LOCATION, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);

// You're also missing this part
smcn.RegisterDataDefineStruct<LOCAL_STRUCT>(DEFINITIONS.LOCATION);

// Then finally this line will do the request and you're event handler should fire:
smcn.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.LOCATION, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0);

// Also Since I sometimes request data only at certain times, and some data I want all the time..
// I have a switch case to evaluate the DEFINITION enum against the data.dwDefineID
// to know which data the event handler received and which structure I need to cast it to to read it.
void OnGetDataByte(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {

    switch((DEFINITIONS)data.dwDefineID)
    {
        case DEFENITIONS.LOCATION
        {
            var location = (LOCAL_STRUCT)data.dwData[0];
        }
    }
}
 
Last edited:
Also, the call to RequestDataOnSimObjectType is called after receipt of the SimStart event in all of the examples I've seen.
 
Not sure what SimStart is. It's not an event in the Manageged DLL anyway. Maybe you are meaning OnRecvOpen? Meaning it actually connected to the simulator? Yes it would make sense to not request data until you've connected to the sim.
 
This is from the Managed C# Code Samples from the FSX SDK... example for Managed System Event.
Code:
                simconnect.SubscribeToSystemEvent(EVENTS.SIMSTART, "SimStart");
There is a significant wealth of information contained in the SDK documentation of all the prior sim versions and to a great extent they still apply to the newer MSFS. A bit more time reading will reduce your frustration levels in trying to figure things out extensively.
 
Wolfgang,

You are missing the
smcn.RegisterDataDefineStruct that tells SimConnect what the data looks like that you are wanting.

I can give you an example of say the planes location. This is something I'm doing to periodically write the location to a file so I can plot the flight on a map later on if I want to. I didn't include a full .cs file just copied the relevant parts and reworded some things to match how you're doing it. One thing I have two ENUMs one for DATA_REQUESTS and one for DEFINITIONS.

When my event is fired I can evaluate which definition it is so I know what structure to cast it into. Which is the purpose of the AddToDataDefinition. Make sure those are in the same order as the structure of your data and use the right DATATYPE

I hope this helps.

C#:
// You Need A Structure for the data
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct LOCAL_STRUCT
{
    //Position
    public double Plane_Latitude;
    public double Plane_Longitude;
    public double Plane_Altitude;
}

public enum DATA_REQUESTS
{
    REQUEST_1,
}

// I setup multiple under this enum, here for different data that may want ever second, or only once etc.
public enum DEFINITIONS
{
    LOCATION,
}

// Then you create your SimConnect like you are.

// Register your Event handlers like you are
// OnRecvSimobjectData += .... What you have is fine
//

// Here is what you're missing:
smcn.AddToDataDefinition(DEFINITIONS.LOCATION, "Plane Latitude", "degrees latitude", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
smcn.AddToDataDefinition(DEFINITIONS.LOCATION, "Plane Longitude", "degrees longitude", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
smcn.AddToDataDefinition(DEFINITIONS.LOCATION, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);

// You're also missing this part
smcn.RegisterDataDefineStruct<LOCAL_STRUCT>(DEFINITIONS.LOCATION);

// Then finally this line will do the request and you're event handler should fire:
smcn.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.LOCATION, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0);

// Also Since I sometimes request data only at certain times, and some data I want all the time..
// I have a switch case to evaluate the DEFINITION enum against the data.dwDefineID
// to know which data the event handler received and which structure I need to cast it to to read it.
void OnGetDataByte(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {

    switch((DEFINITIONS)data.dwDefineID)
    {
        case DEFENITIONS.LOCATION
        {
            var location = (LOCAL_STRUCT)data.dwData[0];
        }
    }
}
Thank you Rup,
I was several years out of programming Sim and I was too lazy to look in my old P3D-Files.
Off course I forgot AddToDataDefinition(... and RegisterDataDefineStruct!
All worked fine now.
Perhaps I'm too old to dive once more in Sim-Programming.
Unfortunately the MSFS-variables are different to those in FSX and P3D.
The MSFS-SDK Help file is a desaster.

Best regards,
Wolfgang
 
Based on what I've seen in the SDK, the variables are the same, except that not all of them function in MSFS at this time.
 
Yeah I don't have the FSX stuff I just figured it out looking at the samples from the MSFS SDK. It's only managed example was for SimVars and it didn't use anything with events. However searching all files in the Sim Connect examples there are 4 files with SimStart in cpp files. I'll have to take a look. I've so far not run into any issues not using it. Probably best practice to use it but what I'm doing is just for personal use at the moment anyway. Thanks for the heads up.
 
can someone give me a short example of controlling the throttle programmatically ... C# ? or c++

I am able to read most the variables from simconnect now .. now I need to hook up some functions or updates to control things any help appreciated
 
I use EVENT IDs
THROTTLE_SET to be exact. This is C# but should get you going I would think.

C#:
public enum GROUP
{
    GROUP1
}


//DEFINE ALL YOUR EVENT IDs in one ENUM
//Then you can map them all with a simple function
//I am only including 4 in my example but you can put every single event id
//in here in case you ever need to use it.
public enum EventEnum
{
    AUTOPILOT_ON,
    AUTOPILOT_OFF,
    THROTTLE_SET,
    AP_SPD_VAR_SET,
}

...
//After creating your var simconnect = new SimConnect ...
//for example I called mine simconnect.
//This will map all the event ids to their names.  Be sure
// you define your ENUM with the same text that it maps to in the docs.
// change GROUP.GROUP1 to match whatever your enum is for your group.
foreach (EventEnum item in Enum.GetValues(typeof(EventEnum)))
{
    simconnect.MapClientEventToSimEvent(item, item.ToString());
    simconnect.AddClientEventToNotificationGroup(GROUP.GROUP1, item, false);
}

// Then I define a method to send events
// If you are doing an event that doesn't have any data just use "0" for string data value
public static void TransmitEvent(EventEnum cmd, string data)
{
    Byte[] Bytes = BitConverter.GetBytes(Convert.ToInt32(data));
    UInt32 Param = BitConverter.ToUInt32(Bytes, 0);
    try
    {
        simconnect.TransmitClientEvent(0, cmd, Param, GROUP.GROUP1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
    }
    catch (Exception e)
    {
        Console.WriteLine("The Transmit Request Failed: " + e.Message);
    }
}


// Then If I want to set throttle...
// Take 16383 * <the pecentage> you want to set it to.
// This sets the throttle to 42% just as an example.
var throttle = (int)(16383 * .42);
TransmitEvent(EventEnum.THROTTLE_SET, throttle.ToString());
 
Last edited:
Back
Top