Advertisement
Guest User

SimConnect Error (OPEN then QUIT)

a guest
Aug 21st, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.71 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <string>
  3. #include "scinterface.hpp"
  4. #include <SimConnect.h>
  5. #include <stdexcept>
  6. #include "ui_mainwindow.h"
  7. #include "mainwindow.h"
  8. #include <stdio.h>
  9. #include <iostream>
  10. #include <strsafe.h>
  11. SimConnectInterface *parent;
  12. HANDLE hSimConnect;
  13. void CALLBACK scDispatch(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext);
  14. struct Struct1
  15. {
  16.     char    title[256];
  17.     double  kohlsmann;
  18.     double  altitude;
  19.     double  latitude;
  20.     double  longitude;
  21. };
  22. enum EVENT_ID{
  23.     EVENT_SIM_START,
  24. };
  25. enum DATA_DEFINE_ID {
  26.     DEFINITION_1,
  27. };
  28. enum DATA_REQUEST_ID {
  29.     REQUEST_1,
  30. };
  31. SimConnectInterface::SimConnectInterface(MainWindow *caller)
  32. {
  33.     quit=0;
  34.     SimConnectInterface::caller = caller;
  35.     int i=1;
  36.     while(!SUCCEEDED(SimConnect_Open(&hSimConnect, "SimConnect Thing", NULL, 0, NULL, 0))) {
  37.         if(i==6) {
  38.             delete hSimConnect;
  39.             throw std::runtime_error("Could not connect to simulator!");
  40.         }
  41.         caller->appendToLog(QStringLiteral("SC connection attempt #%1 failed.").arg(i++));
  42.         Sleep(500);
  43.     }
  44.     caller->appendToLog(QStringLiteral("Connected to SimConnect after %1 attempts.").arg(i));
  45. }
  46. void SimConnectInterface::pollSim() {
  47.     HRESULT hr;
  48.     if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Data", NULL, 0, 0, 0)))
  49.     {
  50.         std::cout << "Succeeded" << std::endl;
  51.         // Set up the data definition, but do not yet do anything with it
  52.         hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "title", NULL, SIMCONNECT_DATATYPE_STRING256);
  53.         hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Kohlsman setting hg", "inHg");
  54.         hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Altitude", "feet");
  55.         hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Latitude", "degrees");
  56.         hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Longitude", "degrees");
  57.         // Request an event when the simulation starts
  58.         hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");
  59.         /*
  60.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::EVENT_BRAKES, "brakes");
  61.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::EVENT_BRAKES);
  62.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::VELOCITY_WORLD_Z, "VELOCITY WORLD Z");
  63.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::VELOCITY_WORLD_Z);
  64.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::VELOCITY_WORLD_X, "VELOCITY WORLD X");
  65.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::VELOCITY_WORLD_X);
  66.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::VELOCITY_WORLD_Y, "VELOCITY WORLD Y");
  67.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::VELOCITY_WORLD_Y);
  68.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ACCELERATION_WORLD_X, "ACCELERATION WORLD X");
  69.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ACCELERATION_WORLD_X);
  70.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ACCELERATION_WORLD_Y, "ACCELERATION WORLD Y");
  71.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ACCELERATION_WORLD_Y);
  72.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ACCELERATION_WORLD_Z, "ACCELERATION WORLD Z");
  73.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ACCELERATION_WORLD_Z);
  74.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ROTATION_VELOCITY_BODY_X, "ROTATION VELOCITY BODY X");
  75.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ROTATION_VELOCITY_BODY_X);
  76.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Y, "ROTATION VELOCITY BODY Y");
  77.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Y);
  78.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Z, "ROTATION VELOCITY BODY Z");
  79.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Z);
  80.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_ALTITUDE, "PLANE ALTITUDE");
  81.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_ALTITUDE);
  82.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_LATITUDE, "PLANE LATITUDE");
  83.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_LATITUDE);
  84.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_LONGITUDE, "PLANE LONGITUDE");
  85.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_LONGITUDE);
  86.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_PITCH_DEGREES, "PLANE PITCH DEGREES");
  87.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_PITCH_DEGREES);
  88.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_BANK_DEGREES, "PLANE BANK DEGREES");
  89.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_BANK_DEGREES);
  90.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_HEADING_DEGREES_TRUE, "PLANE HEADING DEGREES TRUE");
  91.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_HEADING_DEGREES_TRUE);
  92.         SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::SIM_ON_GROUND, "SIM ON GROUND");
  93.         SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::SIM_ON_GROUND);
  94.         SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP0, SIMCONNECT_GROUP_PRIORITY_HIGHEST);
  95.         */
  96.         ::parent = this;
  97.         while (0 == quit) {
  98.             SimConnect_CallDispatch(hSimConnect, scDispatch, this);
  99.             std::cout << "SimConnect Call Dispatch" << std::endl;
  100.             Sleep(50);
  101.         }
  102.         if(quit==1)
  103.             hr = SimConnect_Close(hSimConnect);
  104.     }
  105. }
  106. void CALLBACK scDispatch(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext) {
  107.     HRESULT hr;
  108.     std::cout << "scDispatch" << std::endl;
  109.     SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
  110.     switch(pData->dwID) {
  111.         case SIMCONNECT_RECV_ID_EVENT:
  112.             std::cout << evt->uEventID << " Hello" << std::endl;
  113.             switch(evt->uEventID) {
  114.                 case EVENT_SIM_START:
  115.                     // Now the sim is running, request information on the user aircraft
  116.                     hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
  117.                     std::cout << "START" << std::endl;
  118.                     break;
  119.                 case SC_EVENT_ID::EVENT_BRAKES:
  120.                     parent->caller->appendToLog(QStringLiteral("Event brakes: %1").arg(evt->dwData));
  121.                     std::cout << "BREAKS" << std::endl;
  122.                     break;
  123.                 default:
  124.                     std::cout << "STUFF" << std::endl;
  125.                     parent->caller->appendToLog(QStringLiteral("%1: %2").arg(std::to_string(evt->uEventID).c_str()).arg(evt->dwData));
  126.                     break;
  127.             }
  128.             break;
  129.             case SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
  130.             {
  131.                 std::cout << "BTYPE" << std::endl;
  132.                 SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pData;
  133.                 switch(pObjData->dwRequestID)
  134.                 {
  135.                     case REQUEST_1:
  136.                     {
  137.                         DWORD ObjectID = pObjData->dwObjectID;
  138.                         Struct1 *pS = (Struct1*)&pObjData->dwData;
  139.                         if (SUCCEEDED(StringCbLengthA(&pS->title[0], sizeof(pS->title), NULL))) // security check
  140.                         {
  141.                             printf("\nObjectID=%d  title=\"%s\"\nLat=%f  Lon=%f  Alt=%f  Kohlsman=%.2f", ObjectID, pS->title, pS->latitude, pS->longitude, pS->altitude, pS->kohlsmann );
  142.                         }
  143.                         break;
  144.                     }
  145.                     default:
  146.                        break;
  147.                 }
  148.                 break;
  149.             }
  150.         case SIMCONNECT_RECV_ID_QUIT:
  151.             std::cout << "SC Stuffs" << std::endl;
  152.             parent->caller->appendToLog(QStringLiteral("Disconnected from simulator"));
  153.             parent->quit = 1;
  154.             break;
  155.         case SIMCONNECT_RECV_ID_EXCEPTION:
  156.         {
  157.             SIMCONNECT_RECV_EXCEPTION *e = (SIMCONNECT_RECV_EXCEPTION*)pData;
  158.             std::cout << "Exception ID: " << e->dwException << std::endl;
  159.         }
  160.         case SIMCONNECT_RECV_ID_OPEN:
  161.         {
  162.             SIMCONNECT_RECV_OPEN *pOpen = (SIMCONNECT_RECV_OPEN*)pData;
  163.             std::cout << "Open: AppName=" << pOpen->szApplicationName << "  AppVersion=" << pOpen->dwApplicationVersionMajor << "." << pOpen->dwApplicationVersionMinor
  164.                       << "." << pOpen->dwApplicationBuildMajor << "." << pOpen->dwApplicationBuildMinor << "  SimConnectVersion=" << pOpen->dwSimConnectVersionMajor << "." <<
  165.                          pOpen->dwSimConnectVersionMinor << "." << pOpen->dwSimConnectBuildMajor << "." << pOpen->dwSimConnectBuildMinor << std::endl;
  166.             std::cout << "Done" << std::endl;
  167.             break;
  168.         }
  169.         default:
  170.             std::cout << pData->dwID << " Unknown Event" << std::endl;
  171.             break;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement