Simconnect disconnects immediately after connection is established

SDK supports Prepar3D’s philosophy of an open development architecture and encourages third parties to bring new innovations with improved add-ons and training content.
Locked
KLM613
Posts: 4
Joined: Sun Apr 26, 2020 5:45 pm

Simconnect disconnects immediately after connection is established

Post by KLM613 »

Hello everyone, I'm desperately looking for help about this issue.
I had this working on FSX but since I migrated to P3D it stopped doing it.

As title says the problem is I the connection is closed as soon as it's established.
I have my own implementation but to discard any mistake I can be doing I use the example that comes with the PMDG NGX addon.
This is the main loop:

Code: Select all

        while( quit == 0 )
        {
	    // receive and process the NGX data
            SimConnect_CallDispatch(hSimConnect, MyDispatchProc, NULL);

            Sleep(1);
        } 
SIMCONNECT_RECV_ID_QUIT is sent in the first iteration so the communication is lost.
I don't have idea why tho, as I said few years ago I had this working on FSX SP2.

I'm using Prepar3D v4, SimConnect headers and .lib are from its SDK

I'm not really familiar with the SimConnect API, not sure how can I add some extra debug.

Please if there is anything you think it's worth to try let me know, I'm totally frustrated :/
Thanks!
EllipticCurve
Posts: 152
Joined: Mon Jun 12, 2017 6:14 pm

Re: Simconnect disconnects immediately after connection is established

Post by EllipticCurve »

Sleep(1) doesn't look correct.

Your loop should not be infinite, but it shouldn't sleep, either.

Note this is from an old SimConnect project I wrote in C# many years gao, but the basics should be the same.

There should be a message pump within your application,. This is what mine looks like:

Code: Select all

		protected override void DefWndProc(ref Message m)
		{
			if (m.Msg == WM_USER_SIMCONNECT)
			{
				if (simconnect != null)
				{
					try
					{
						simconnect.ReceiveMessage();
					}
					catch (Exception)
					{
						txtStatus.Text = "A critical error has occurred in FSX. Disconnected.";
						simconnect_close();
					}
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}
As part of setting up SimConnect event notifications, you should define callback functions, so that when SC raises an event that you are registered to receive, it will call that defined callback function.

Are you familiar with these?

A callback function is something YOU define, as a function within YOUR code.

SimConnect will call it when necessary.

You will write the function to handle whatever it is that you registered to know within that function.

Hope it helps!
All comments and opinions are my own.
BenBaron
Posts: 80
Joined: Fri Jan 16, 2015 7:51 am

Re: Simconnect disconnects immediately after connection is established

Post by BenBaron »

To chime in: do you do some simconnect exception handling somewhere in your code? Maybe an exception with further information is thrown before you receive SIMCONNECT_RECV_ID_QUIT. But of course you have to handle it.

Greets,
Ben
Locked