• 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 OnRecvOpen doesn't execute

Hi, I can't find what am I missing in my code.
C#:
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
Function simconnect_OnRecvOpen never executie
I thought that I did something wrong with class division (Form one class, Simconnect other class) so I put everything in Form class. No luck, OnRecevOpen still doesn't work.
I've tried creating new Windows Form Application with code what I believe is mandatory to establish connection (code below). I've tried creating new WPF App with same code. No luck
SimVarWatcher is working. Even my old FSX simconnect application somehow connected with MSFS and received simvariables correctly
My new code is based on SimVarWatcher and my old FSX application
Complete code what I believe is mandatory:
C#:
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Connect();
        }

        public const int WM_USER_SIMCONNECT = 0x0402;
        private SimConnect simconnect = null;

        enum DEFINITIONS
        {
            test
        }

        enum DATA_REQ
        {
            REQ_1
        };

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct test
        {
            public double hdgd;
        };
        public test t;

        public void Connect()
        {
            Console.WriteLine("Connect");

            if (simconnect == null)
            {
                try
                {
                    /// The constructor is similar to SimConnect_Open in the native API
                    simconnect = new SimConnect("test", this.Handle, WM_USER_SIMCONNECT, null, 0);

                    /// Listen to connect and quit msgs
                    Console.WriteLine("1");
                    simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
                    simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
                    Console.WriteLine("2");

                    simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);

                    simconnect.AddToDataDefinition(DEFINITIONS.test, "AUTOPILOT HEADING LOCK DIR", "Degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                    simconnect.RegisterDataDefineStruct<test>(DEFINITIONS.test);

                    simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);

                    simconnect.RequestDataOnSimObjectType(DATA_REQ.REQ_1, DEFINITIONS.test, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
                }
                catch (COMException ex)
                {
                    Console.WriteLine("Connection to KH failed: " + ex.Message);
                }
            }
        }

        void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
        {
            Console.WriteLine("Open");
            //timer1.Interval = 1;
        }

        void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
        {

        }

        void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {

        }

        public void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
        {
            Console.WriteLine("3");
            switch ((DATA_REQ)data.dwRequestID)
            {
                case DATA_REQ.REQ_1:
                    {
                        t = (test)data.dwData[0];
                        break;
                    }
                default:
                    //displayText("Unknown request ID: " + data.dwRequestID);
                    break;
            }
        }
    }
}

So in console I got messages:
Connect
1
2

Thats it. There should be "Open" for OnRecvOpen and "3" for receiving data. Can someone copy this code and confirm that OnRecvOpen doesn.t run and tell what am I missing?
 
Nevermind... I couldn't find this for 3 days and I found it 10 minutes after posting this thread... This is what was missing:
C#:
protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_USER_SIMCONNECT)
            {
                if (simconnect != null)
                {
                    simconnect.ReceiveMessage();
                }
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }
 
Top