Using SimConnect in an MFC Dialog based application

Hello guys,

I’m trying to get a few data from MSFS into my MFC dialog based application in c++. I want to retrieve the Pitch and bank angle of the plane, I took some code from the TaggedData sample from the Simconnect SDK. But I’m not quite sure when to run the testTaggedDataRequest() function, that establishes the connection. I tried to place it at the end of my OnInitDialog function, but apparently that places my app in an endless loop.

void testTaggedDataRequest()
{
HRESULT hr;

if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Tagged Data", NULL, 0, 0, 0)))
{
  hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_PDR, "ATTITUDE INDICATOR BANK DEGREES", "Radians",
  									SIMCONNECT_DATATYPE_FLOAT32, 0, DATA_AILERON_AVERAGE_DEFLECTION);
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_PDR, "ATTITUDE INDICATOR PITCH DEGREES", "Radians",
  									SIMCONNECT_DATATYPE_FLOAT32, 0, DATA_ELEVATOR_DEFLECTION);

    hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");

    while( 0 == quit )
    {
        SimConnect_CallDispatch(hSimConnect, MyDispatchProcPDR, NULL);
        Sleep(1);
    } 

    hr = SimConnect_Close(hSimConnect);
}

}

The following function gets the data from the sim:

void CALLBACK MyDispatchProcPDR(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext)
{
CAirbusFlightDisplaysDlg* procThis = reinterpret_cast<CAirbusFlightDisplaysDlg*>(pContext);
procThis->DispatchProc(pData, cbData);
}

void DispatchProc(SIMCONNECT_RECV* pData, DWORD cbData)
{
HRESULT hr;

switch (pData->dwID)
{
case SIMCONNECT_RECV_ID_EVENT:
{
SIMCONNECT_RECV_EVENT* evt = (SIMCONNECT_RECV_EVENT*)pData;
switch (evt->uEventID)
{
case EVENT_SIM_START:

  	hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_PDR, DEFINITION_PDR,
  		SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_SECOND,
  		SIMCONNECT_DATA_REQUEST_FLAG_CHANGED | SIMCONNECT_DATA_REQUEST_FLAG_TAGGED);

  	break;

  default:
  	break;
  }
  break;

}

case SIMCONNECT_RECV_ID_SIMOBJECT_DATA:
{
SIMCONNECT_RECV_SIMOBJECT_DATA* pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;

  switch (pObjData->dwRequestID)
  {
  case REQUEST_PDR:
  {
  	int	count = 0;;
  	StructDatum* pS = (StructDatum*)&pObjData->dwData;

  	while (count < (int)pObjData->dwDefineCount)
  	{
  		switch (pS->datum[count].id)
  		{
  		case DATA_AILERON_AVERAGE_DEFLECTION:
  			BankAngle = (double)pS->datum[count].value * 57.296;
  			break;

  		case DATA_ELEVATOR_DEFLECTION:
  			PitchAngle = (double)pS->datum[count].value * 57.296;
  			break;

  		default:
  			break;
  		}
  		++count;
  	}
  	break;
  }

  default:
  	break;
  }
  break;

}

case SIMCONNECT_RECV_ID_QUIT:
{
quit = 1;
break;
}

default:
break;
}
}

This is really more of an MFC question than MSFS. CDialog::OnInitDialog is used to initialize a dialog and cannot run indefinitely, but SimConnect_CallDispatch needs to be called frequently as long as your application lives. You’ll need to take out the CallDispatch call into a periodically invoked place, not while looping indefinitely on the UI thread.
You might want to take a look at either using a timer, setting up a dedicated thread to handle SimConnect messages, or something like CDialog::OnPaint.