C# SimConnect Error

Hi everyone,

Trying to write a Windows Forms app that connects to SimConnect to track times and fuel burns. I’m using C# and Visual Studio 2019. I’ve got a form with a button, and when the button is clicked, the code in the SDK for creating a SimConnect object is executed. I’m getting the following error:

System.BadImageFormatException
  HResult=0x8007000B
  Message=Could not load file or assembly 'Microsoft.FlightSimulator.SimConnect, Version=11.0.62651.3, Culture=neutral, PublicKeyToken=baf445ffb3a06b5c' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  Source=DTracker
  StackTrace:
   at DTracker.frmDtracker.btnConnect_Click(Object sender, EventArgs e) in C:\Users\markl\source\repos\DTracker\frmDtracker.cs:line 46
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at DTracker.Program.Main() in C:\Users\markl\source\repos\DTracker\Program.cs:line 21

The code that’s trying to execute (pure copy/paste from the SDK):

private void btnConnect_Click(object sender, EventArgs e)
        {
            SimConnect simconnect = null;

            const int WM_USER_SIMCONNECT = 0x0402;
            
            try
            {
                simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0);
            }
            catch (COMException ex)
            {
                
            }

            if (simconnect != null)
            {
                simconnect.Dispose();
                simconnect = null;
            }
            else
            {
                lblConnection.Text = "Connected!";
            }
        }

Any ideas? This happens if the sim is running or not. Help appreciated - thanks!

Mark

Sorry, didn’t look closely enough at your error. BadImageFormat is usually a bitness problem. Are you building a 64-bit executable? If so, could it be that Microsoft.FlightSimulator.SimConnect.dll is a 32-bit assembly? I would imagine that Microsoft.FlightSimulator.SimConnect.dll is just an interop assembly, so it will be tied to the bitness of the corresponding native dll. Alternatively, if simconnect is 64 bit, but your exe is 32-bit, you would also get this error. You can easily check if this is the case by targeting a specific platform with your build.

Did you add simconnect as a dependency of the project? You can go download the project and source for my project (PilotPath Recorder) from github. It compiles without issue and it is c# and visual studio 2019.

There are three key things for a managed (.NET) SimConnect project.

  1. Make sure you have the MSFS SDK installed.
  2. Make sure your project has a reference to “C:\MSFS SDK\SimConnect SDK\lib\managed\Microsoft.FlightSimulator.SimConnect.dll” - change the path to suit your SDK install path - that’s the default path.
  3. Make sure you copy “C:\MSFS SDK\SimConnect SDK\lib\SimConnect.dll” to your projects compiled bin output folder - both Debug and Release. This is most easily done with a Post-Build event.
  4. Make sure you set the build configuration to x64 and not AnyPC or x86.

Ok so that’s four key things :stuck_out_tongue:

Edit: Strictly speaking 1. is not absolutely necessary, but it’s the easiest way to get the two library files required and will ensure they stay up to date as the SDK matures.

1 Like

You have to include the dll in your project references. image You also need the corresponding “using” statement in the C# code. On my system, the dll is included in the SDK here: D:\Program Files\FlightSimulator\sdk\SimConnect SDK\lib\managed. You also must include SimConnect.dll in the build folder that holds Microsoft.FlightSimlulator.SimConnect.dll, as this latter dll refers to the former.