Unsuccessful execution of Microsoft.FlightSimulator.SimConnect.dll in Visual Studio using C#

The Microsoft Graph connectors provide a user-friendly approach to integrate outside information with Microsoft Graph, thereby improving the intelligent experiences of Microsoft 365. For additional details, refer to the section on using external groups to regulate access to data sources for Microsoft Graph connectors.

Question:

In an attempt to create a Windows forms application for
Flight Simulator
2020 using SimConnect, I have encountered an issue. The problem lies in the inability to run the dll’s on
Visual Studio
. Whenever I attempt to start the application, I receive error messages such as “System.IO.FileNotFoundException” when compiling at x64 or Any CPU, and “System.BadImageFormatException” when compiling at x86.

I have attempted several methods that have not been successful.

  • Reinstalling the MSFS SDK
  • Relocating the dll files to the directory identified as
    Visual Studio project
    .
  • Creating a compilation with various configurations, including x64, x86, or Any CPU.
  • Experimenting with various editions of the .NET framework.
  • etc…

Despite adding the Microsoft.FlightSimulator.SimConnect.dll to the references, the SimConnect.dll still appears at the bottom of the Solution Explorer. However, even after meticulously following a YouTube tutorial, I was unable to successfully implement it.

By the way, I have a Visual Studio C# project which makes use of SimConnect and is capable of successfully receiving and transmitting data to and from the simulator. You may find the project here as a working example.

As a beginner in coding, I am unsure which properties of my Visual Studio project need to be changed. I suspect some modifications are necessary, but I lack the knowledge to identify them. Any assistance with this issue would be greatly appreciated.

code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;
namespace ZZA1
{
public partial class Form1 : Form
{
    bool portus1, portus2, portus3, portus4, portus5;
    String dataIN, dataIN2 , dataIN3, dataIN4, dataIN5;
    public Form1()
    {
        response = 1;
        output = "nnnnnnnnnn";
        InitializeComponent();
    }
    SimConnect simconnect = null;
    private void label1_Click(object sender, EventArgs e)
    {
    }
    public class FileNotFoundException : System.IO.IOException
    {
        
    }
    private void button1_Click(object sender, EventArgs e)
    {
        portus1 = !portus1;
        try
        { 
            switch (portus1)
            {
                case true:
                    serialPort1.PortName = Portbox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(baudbox1.Text);
                    serialPort1.Open();                       
                break;
                case false:
                    serialPort1.Close();
                break;
            }
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message,"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        if (serialPort1.IsOpen)
        {
            condiscon.BackColor = Color.Green;
            condiscon.Text = "Connected";
            Portbox1.Enabled = false;
            baudbox1.Enabled = false;
            Serialdata.Text = "";
        }
        else
        {
            condiscon.BackColor = Color.Red;
            condiscon.Text = "Disconnected";
            Portbox1.Enabled = true;
            baudbox1.Enabled = true;
        } 
    }
    private void button1_Click_1(object sender, EventArgs e)
    {
        if (my_simconnect == null)
        {
            try
            {
                my_simconnect = new Microsoft.FlightSimulator.SimConnect.SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
                initDataRequest();
            }
            catch (COMException zui)
            {
                MessageBox.Show(zui.Message, "Unable", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            contosim.Text = "ERROR - Try again";
            closeConnection();
        }
    }
    private void condiscon2_Click(object sender, EventArgs e)
    {
        portus2 = !portus2;
        try
        {
            switch (portus2)
            {
                case true:
                    serialPort2.PortName = Portbox2.Text;
                    serialPort2.BaudRate = Convert.ToInt32(baudbox2.Text);
                    serialPort2.Open();
                break;
                case false:
                    serialPort2.Close();   
                break;
            }
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        if (serialPort2.IsOpen)
        {
            condiscon2.BackColor = Color.Green;
            condiscon2.Text = "Connected";
            Portbox2.Enabled = false;
            baudbox2.Enabled = false;
            Serialdata2.Text = "";
        }
        else
        {
            condiscon2.BackColor = Color.Red;
            condiscon2.Text = "Disconnected";
            Portbox2.Enabled = true;
            baudbox2.Enabled = true;
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();
        Portbox1.Items.AddRange(ports);
        Portbox2.Items.AddRange(ports);
    }
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        dataIN = serialPort1.ReadExisting();
        this.Invoke(new EventHandler(ShowData));
    }
    private void serialPort2_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
    {
        dataIN2 = serialPort2.ReadExisting();
        this.Invoke(new EventHandler(ShowData2));
    }
    private void ShowData(object sender, EventArgs e)
    {
        Serialdata.Text += dataIN;
    }
    private void ShowData2(object sender, EventArgs e)
    {
        Serialdata2.Text += dataIN2;
    }
    private void Serialdata_TextChanged(object sender, EventArgs e)
    {
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
        }
        if (serialPort2.IsOpen)
        {
            serialPort2.Close();
        }
    }
    private void initDataRequest()
    {
        try
        {
            my_simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
            my_simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
            my_simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);
            my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Title", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Heading Degrees True", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Ground Altitude", "meters", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            my_simconnect.RegisterDataDefineStruct(DEFINITIONS.Struct1);
            my_simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
        }
        catch (COMException op)
        {
            MessageBox.Show(op.Message, "ERR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    private Microsoft.FlightSimulator.SimConnect.SimConnect my_simconnect;
    private string output;
    private int response;
    const int WM_USER_SIMCONNECT = 0x402;
   
    private enum DEFINITIONS
    {
        Struct1
    }
    private void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
    {
        contosim.BackColor = Color.Green;
        contosim.Text = "Disconnect from Flight Simulator";
    }
    private void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
    {
        closeConnection();
    }
    private void closeConnection()
    {
        if (my_simconnect != null)
        {
            my_simconnect.Dispose();
            my_simconnect = null;
            contosim.BackColor = Color.Red;
            contosim.Text = "Connect to Flight Simulator";
        }
    }
    
    private void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
    {
        contosim.Text = "Exception received:" + ((uint)data.dwException);
        contosim.BackColor = Color.Orange;
    }
    private void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
    {
        string DirNumberText = "";
        string DirNumber1Text = "";
        string DirNumber2Text = "";
        string FileNumberText = "";
        string FileNumber1Text = "";
        string FileNumber2Text = "";
        double DirNumber1 = 0;
        double DirNumber2 = 0;
        double FileNumber1 = 0;
        double FileNumber2 = 0;
        if (data.dwRequestID == 0)
        {
            Struct1 struct1 = (Struct1)data.dwData[0];
        }
    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct Struct1
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)]
        public string title;
        public double latitude;
        public double longitude;
        public double trueheading;
        public double groundaltitude;
    }
    protected override void DefWndProc(ref Message m)
    {
        if (m.Msg == WM_USER_SIMCONNECT)
        {
            if (my_simconnect != null)
            {
                my_simconnect.ReceiveMessage();
            }
        }
        else
        {
            base.DefWndProc(ref m);
        }
        
    }
    
    private enum DATA_REQUESTS
    {
        REQUEST_1
    }
}

}


Solution:

I encountered the same problem with MSFS SDK and found a solution.

As well as you I’ve tried to:

  • Compiling for various .NET versions and CPU architectures.
  • Reinstalling MSFS SDK
  • Updating GAC
  • Recreating project files
  • etc

The exception that occurred was not related to

Microsoft.FlightSimulator.SimConnect

specifically, but rather to the

SimConnect.dll

that comes with the SDK. You noted that moving the dll’s into the folder of the Visual Studio project is not a sufficient solution.

The resolution to the issue was uncomplicated – by placing

..MSFS SDKSimConnect SDKlibSimConnect.dll


in the x64 binary folder of your build, the problem was resolved. In this instance,

<project_name>binx64Debug


was the location.

Additionally, it’s important to remember to compile the project for x64 architecture. I trust that my response has provided some assistance.

Frequently Asked Questions

Posted in Uncategorized