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

WPF + SimConnect

Messages
73
Hi everyone !

I'am trying to set up a SimConnect connexion using a WPF application but without success.

I used this topic but it doesn't work : http://forum.avsim.net/topic/224001-simconnect-from-windows-presentation-foundation-wpf/

Here my sample code :

Code:
public partial class MainWindow : Window
    {
        enum EVENTS { SIMSTART, SIMSTOP, SIMPAUSED, SIMUNPAUSED, POSITIONCHANGED, CRASHED };

        // BackgroundWorker Surveillance connexion à SimConnect
        private BackgroundWorker workerCheckingSimConnect = new BackgroundWorker();
        
        // Objet SimConnect
        public SimConnect gSimConnect = null;

        public MainWindow()
        {
            InitializeComponent();

            workerCheckingSimConnect.DoWork += workerCheckingSimConnect_DoWork;
            workerCheckingSimConnect.RunWorkerCompleted += workerCheckingSimConnect_RunWorkerCompleted;

            // On démarre la surveillance de connexion SimConnect
            workerCheckingSimConnect.RunWorkerAsync();
        }

        private void workerCheckingSimConnect_DoWork(object sender, DoWorkEventArgs e)
        {
            if (workerCheckingSimConnect.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            Boolean lConnecte;
            do
            {
                Thread.Sleep(3000);
                try
                {
                    WindowInteropHelper lWih = new WindowInteropHelper(this);
                    IntPtr lWindowHandle = lWih.Handle;
                    HwndSource lHwndSource = HwndSource.FromHwnd(lWindowHandle);
                    lHwndSource.AddHook(new HwndSourceHook(ProcessSimConnectWin32Events));

                    gSimConnect = new SimConnect(Config.NOM_APPLICATION, lHwndSource.Handle, Config.WM_USER_SIMCONNECT, null, SimConnect.SIMCONNECT_OPEN_CONFIGINDEX_LOCAL);
                    lConnecte = true;
                }
                catch
                {
                    lConnecte = false;
                }
            }
            while (!lConnecte);
        }

        private IntPtr ProcessSimConnectWin32Events(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            handled = false;

            if (msg == Config.WM_USER_SIMCONNECT)
            { 
                if (gSimConnect != null)
                {
                    gSimConnect.ReceiveMessage();
                    handled = true;
                }
            }
            return (IntPtr)0;
        }

        private void workerCheckingSimConnect_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                // Associations des événements et des fonctions "CallBack"
                gSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
                gSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
                gSimConnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(SimConnect_OnRecvEvent);

                // Association des événements du simulateur
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMSTART, "SimStart");
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMSTOP, "SimStop");
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMPAUSED, "Paused");
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMUNPAUSED, "Unpaused");
                gSimConnect.SubscribeToSystemEvent(EVENTS.POSITIONCHANGED, "PositionChanged");
                gSimConnect.SubscribeToSystemEvent(EVENTS.CRASHED, "Crashed");

                // Activation des événements du simulateur
                gSimConnect.SetSystemEventState(EVENTS.SIMSTART, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.SIMSTOP, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.SIMPAUSED, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.SIMUNPAUSED, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.POSITIONCHANGED, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.CRASHED, SIMCONNECT_STATE.ON);

                // label SimConnectStatus
                lbsSimConnectStatus.Content = "OK";
                lbsSimConnectStatus.Foreground = new SolidColorBrush(Colors.Gold);
            }
        }

        private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
        {
            // ...
        }

        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            // ...
        }

        private void SimConnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data)
        {
            // ...
        }
}

If someone already succeeded to open a SimConnect connexion using a WPF application ?

Thank you in advance.
 
Hello and thank you for the answers.

I succeeded to connect my WPF application to FSX with SimConnect using this way :

Code:
private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper lWih = new WindowInteropHelper(this);
            IntPtr lHwnd = lWih.Handle;

            gHs = HwndSource.FromHwnd(lHwnd);

            gHs.AddHook(new HwndSourceHook(ProcessSimConnectWin32Events));

            workerCheckingSimConnect.WorkerSupportsCancellation = true;

            workerCheckingSimConnect.DoWork += workerCheckingSimConnect_DoWork;
            workerCheckingSimConnect.RunWorkerCompleted += workerCheckingSimConnect_RunWorkerCompleted;

            workerCheckingSimConnect.RunWorkerAsync();

        }

private void workerCheckingSimConnect_DoWork(object sender, DoWorkEventArgs e)
        {
            if (workerCheckingSimConnect.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            do
            {
                Thread.Sleep(3000);
                try
                {
                    gSimConnect = new SimConnect("VASF", gHs.Handle, 0, null, 0);
                    gConnected = true;
                }
                catch
                {
                    gConnected = false;
                }
            }
            while (!gConnected);
        }

        private void workerCheckingSimConnect_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                gSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
                gSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
                gSimConnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(SimConnect_OnRecvEvent);
                gSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvData);

                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMSTART, "SimStart");
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMSTOP, "SimStop");
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMPAUSED, "Paused");
                gSimConnect.SubscribeToSystemEvent(EVENTS.SIMUNPAUSED, "Unpaused");
                gSimConnect.SubscribeToSystemEvent(EVENTS.POSITIONCHANGED, "PositionChanged");
                gSimConnect.SubscribeToSystemEvent(EVENTS.CRASHED, "Crashed");

                gSimConnect.SetSystemEventState(EVENTS.SIMSTART, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.SIMSTOP, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.SIMPAUSED, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.SIMUNPAUSED, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.POSITIONCHANGED, SIMCONNECT_STATE.ON);
                gSimConnect.SetSystemEventState(EVENTS.CRASHED, SIMCONNECT_STATE.ON);

                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "PLANE LATITUDE", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "PLANE LONGITUDE", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "PLANE ALTITUDE", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "PLANE ALT ABOVE GROUND", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "VERTICAL SPEED", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "GROUND VELOCITY", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                gSimConnect.AddToDataDefinition(DEFINITIONS.StructAmbient, "SIM ON GROUND", "bool", SIMCONNECT_DATATYPE.INT32, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                gSimConnect.RegisterDataDefineStruct<StructAmbient>(DEFINITIONS.StructAmbient);

                // label SimConnectStatus
                lbsSimConnectStatus.Content = "OK";
                lbsSimConnectStatus.Foreground = new SolidColorBrush(Colors.Gold);
            }
        }

        private IntPtr ProcessSimConnectWin32Events(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (gConnected && gSimConnect != null)
            {
                gSimConnect.ReceiveMessage();
            }

            return (IntPtr)0;
        }
 
Back
Top