• 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.

MSFS C# SimConnect Windows Form example

rhumbaflappy

Administrator
Staff member
Resource contributor
Messages
5,932
Country
us-wisconsin
Attached is a C# windows form example of the MSFS SimConnect.

MSFS .NET requires both the Microsoft.FlightSimulator.SimConnect.dll (.NET) and the SimConnect.dll (native C++) to be in the folder with the finished program. Look near the final lines of TCalc_004.csproj.:
C#:
  <ItemGroup>
    <Content Include=".\SimConnect.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

This will copy the SimConnect.dll to the finished debug or release folder in VS19.

The code is C# v4.72, I believe, and AnyCPU. It won't work with x86 because the SimConnect.dll is 64-bit.
 

Attachments

  • TCalc_Code.zip
    297.6 KB · Views: 2,308
Messages
15
Country
unitedstates
I use VS code and I did <PlatformTarget> x64
You can also use PreserveNewest instead of Always, not that it matters really.

Also for anyone reading this that uses VS Code or otherwise, if you keep SDK in a folder somewhere else you can use:


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

edited: to use the evironment variable forgot I had done that. I installed from MSFS2020 dev mode installer and it set an environment variable MSFS_SDK
 
Last edited:
Messages
125
Country
unitedstates
you can place this in Visual Studio (config) Build Events and it will copy the files when you build a new version.

xcopy "C:\MSFS SDK\SimConnect SDK\lib\SimConnect.dll" "$(TargetDir)" /y
xcopy "C:\MSFS SDK\Samples\SimvarWatcher\SimConnect.cfg" "$(TargetDir)" /y
 
Messages
1,243
Country
canada
Does this mean that you have to include a copy of the dlls in your setup program, or is there a specific folder to put your program into? I understand that webassembly modules are to be put into a modules sub folder. But what about standalone exe's?
 
Messages
244
Country
unitedkingdom
Note this code users a client-side timer to repeatedly issue RequestDataOnSimObjectType calls. This is categorically not the usage SimConnect was designed for. Instead the RequestDataOnSimObject call should be issued ONCE, with a callback that processes the data as it is provided by the sim.
 
Messages
84
Country
australia
Note this code users a client-side timer to repeatedly issue RequestDataOnSimObjectType calls. This is categorically not the usage SimConnect was designed for. Instead the RequestDataOnSimObject call should be issued ONCE, with a callback that processes the data as it is provided by the sim.
Yup. Thanks for pointing that out. Using a timer like this is a no no as it eats resources.
 
Messages
1
Country
us-texas
Note this code users a client-side timer to repeatedly issue RequestDataOnSimObjectType calls. This is categorically not the usage SimConnect was designed for. Instead the RequestDataOnSimObject call should be issued ONCE, with a callback that processes the data as it is provided by the sim.

This is incorrect. RequestDataOnSimObjectType is a one shot request and there is no provision in this api calI to set a repetition period. You can verify this by altering the example by removing the timer and adding a button to trigger the request manualy. You will see you only get one response per request.

You can set a repetition period on RequestDataOnSimeObject though.
The SIMCONNECT_PERIOD enumeration type is used with the SimConnect_RequestDataOnSimObject call to specify how often data is to be sent to the client.

Edit: I suppose you can argue how often you should issues requests like this.
 
Last edited:
Messages
112
Country
unitedkingdom
I don't want to hijack this thread (so am happy to start a separate one if people would prefer) but I noticed that many of the LM examples for P3D do indeed call RequestDataOnSimObjectType within an endless loop rather than using RequestDataOnSimObject. Indeed, I've yet to find a managed code example (I'm sure there is one - I just cant find it) in the FSX SimConnect documentation that uses this to get periodic responses (the Data Request.cs sample illustrates the command but only calls for one occurrence).

Is this common across all versions of SimConnect? Is there a known issue with getting the callback from RequestDataOnSimObject? I've tried it in my C# console application and although the line of code executes, I never get a callback - whereas the ...Type version works just fine. I figured it was me but then remembered this thread.


Edit: SOLVED. I found that the solution was in NOT relying on the SimStart event to be the point at which I call RequestDataOnSimObject since in my case the sim was already running when I connected to it. As a stop-gap, I'm now calling RequestDataOnSimObject on the Unpause event since I'm pausing the sim to check things anyway at this point in development. Perhaps ultimately the home for this code will be on successful connection (instantiation of a SimConnect object in my case).
 
Last edited:
Messages
8
Country
chile
Attached is a C# windows form example of the MSFS SimConnect.

Thank you very much.
After been tempted by Python Simconnect wrapper and finding out that it is almost imposible to create a proper functioning interactive App...... Back to the roots.
Your example shows clearly how to implement Simconnect's jargon into an C# Form. ( Managed )

I tested in MSFS2020 , all ok. But FSX Steam edition does not connect.
Supose one have to reference both libraries to the ones in FSX SDK....
Wonder how we could do it , so the app can talk to both versions.

I use the excellent "LittleNavMap" application , last version, and to my surprise it connects on both !
This app explicitly shows both MSFS2020 and FSX in the same tab under "connection options".

May be you can guide me into this ?

Thank you again.
Greetings
Francisco - Chile
 

DragonflightDesign

Resource contributor
Messages
1,082
Country
northernireland
Not being an MSFS developer, I can see one possible problem. IF MSFS2000 is a 64-bit program then you will need to build two different versions of your addon: 32-bit for FSX and 64-bit for MSFS2000 using the relevant SimConnect librariries for each version.
 
Last edited:
Messages
8
Country
chile
using the relevant SimConnect librariries for each version.
You would have noticed that an important part of your answer is not published here.
I am attaching capture of the mail notification i received.
This answer of yours is very important. Maybe you should repeat it

Thank you again
Greetings
 

Attachments

  • dragon.JPG
    dragon.JPG
    98.1 KB · Views: 301

DragonflightDesign

Resource contributor
Messages
1,082
Country
northernireland
You were fast! I edited that out of existance because it's not entirely accurate. Better summary:

- SimConnect is not completely backwards compatible in either 32-bit or 64-bit versions.
- 32-bit versions (FSX -> P3Dv3): if you are only using calls that exist in the FSX SDK, all libraries will work. Otherwise, if you want to be absolutely safe and cover all listed versions, use only the FSX SDK libraries.
- 64-bit versions (P3Dv4 onwards): if you are only using calls that exist in the P3Dv4 SDK, all libraries will work. Otherwise, if you want to be absolutely safe and cover all listed versions, use only the P3Dv4 SDK libraries.
- If you're programming for MSFS2000; as of the time of writing it's a 32-bit SDK that's an incomplete and pretty much unusable mess that is being based (is based?) on the FSX SDK. Just IMHO. I was on the beta program and watched it go from the FSX SDK to something... not worth looking at. I'll take another look at it when it reaches v1.0 but not before then.
 
Last edited:
Messages
8
Country
chile
Perfect.
So, the questions is ..... where do i get an P3Dv4 SDK?
I guess I will ask google for it.
Thank you again.

Greetings
 

rhumbaflappy

Administrator
Staff member
Resource contributor
Messages
5,932
Country
us-wisconsin
For MSFS, you should be able to use the old 32-bit FSX SimConnect as well. The DLL should be in the WindowsSxS system.
 

DragonflightDesign

Resource contributor
Messages
1,082
Country
northernireland
Also, as Dick said in his OP, make sure you set the compiler for 'AnyCPU' so that your app can run on the O/S 32-bit subsystem. You can't do this for P3Dv4 onwards where it needs to be set to 'x64'.

Which also means that I was wrong in my summary (now corrected): I thought MSFS2020 would be a 64-bit SDK, but as Dick points out (and was graceful enough not to say 'RTFM' :D ), if it's giving you the FSX libraries then MSFS2020 must be a 32-bit SDK still. There was talk of making it 64-bit quite some time ago, but Asobo must have changed their minds.
 

rhumbaflappy

Administrator
Staff member
Resource contributor
Messages
5,932
Country
us-wisconsin
Untitled.png


The 32-bit is using the FSX-WindowsSxS SimConnect, and the AnyCpu is using it as well.

The 64-bit is using the MSFS SimConnect, and it needs to be included in the folder with the EXE.
 
Messages
8
Country
chile
Oh my. To be in the safe side , I have been dragging everything according to original rhumbaflappys example....
Even generated an VS Template. It seems I do NOT understand , never mind.
But your comments are crucial if I want to program something that works in FSX too. My Laptop is not strong enoguh to handle MSFS2020 but runs excellent in FSX
Thank you all again
Greetings
 
Messages
8
Country
chile
The 32-bit is using the FSX-WindowsSxS SimConnect, and the AnyCpu is using it as well
Well , that sounds nice.
If 32 bit is working with MSFS 2020 , then it should do with FSX - Steam as well ?
Only that I have not been able to do it right.

I am holding back to FSX until I get to update my 2016 Laptop....to be able to fly MSFS 2020 descently.

I have tried multiple combinations of "SimConnect.dll" ( putting it inside the solution like you do in TcalcCode example instead of post build command ) and "Microsoft.FlightSimulator.SimConnect.dll" ( referencing by the project in VStudio ).
I swear you my friends , I have done my homework .... to no avail.
I even identified versions and put inside extra folder for using both in same one....
Any Cpu , x86 , x64 ..... Will not link to FSX Steam.

Please , refrase your explanations , in a way that I can follow ...... ? very basic - mode?
Wich "Microsoft.FlightSimulator.SimConnect.dll" to reference and wich "SimConnect.dll" to copy to executable folder.
Maybe package both of them and put on cloud ?

Thanks a lot.
Greetings
Francisco
Chile
 
Top