Can’t run Microsoft.FlightSimulator.SimConnect.dll on Visual Studio C#

  Uncategorised

I’ve been trying to make a Windofs forms application for the Flight Simulator 2020 with the help of SimConnect.
The problem is that I cant seem to run the dll’s on Visual Studio. Everytime I press Start I’ll either get the erroe mesage: "System.IO.FileNotFoundException" when compiling at x64 resp. Any CPU, or "System.BadImageFormatException" when compiling at x86.

Things I already have tried, without luck:

  • Reinstalling the MSFS SDK
  • Moving the dll’s into the folder of the Visual Studio project
  • Compiling at different setting such as x64, x86 or Any CPU
  • Trying out different versions of the .NET framework
  • etc…

The Microsoft.FlightSimulator.SimConnect.dll is in the references, and the SimConnect.dll is shown at the bottom of the Solution Explorer.
Even though I followed a YouTube Tutorial one by one I wasnt able to make it work.

BTW: I have a functioning example of a Visual Studio C# project that successfully uses SimConect to get data from and to the Simulator.That Project can be found here.

I suspect that I need to change some properties of my Visual Studio project, but I have no idea whatsoever, I’m literally just starting to code xD. So I would be very thankful if someone could help me with my issue.

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<Struct1>(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
    }


}

}

Source: Visual Studio Questions

LEAVE A COMMENT