C: Launching an external .exe from within a gauge

From FSDeveloper Wiki
Jump to: navigation, search

This short piece of code allows you to launch an external executable (.exe file) and detect when the program has been started. Place the code inside the gauge callback.


//---------------------------------------------------------------------------
// Gauge callback
//---------------------------------------------------------------------------
void FSAPI  aircraft_config_update(PGAUGEHDR pgauge, int service_id, PUSERDATA extra_data)
{
  HINSTANCE ret = 0;
  static int retSuccess = 0;
 
  switch (service_id)
  {
   case PANEL_SERVICE_PRE_UPDATE:

     if (!retSuccess)
     {
       // Try to open NotePad
       ret = ShellExecute(NULL, "open", "c:\\windows\\notepad.exe", NULL, NULL, SW_SHOWNORMAL);
       // If ret > 32 i.e. success then set retSuccess to 1
       if (int(ret) > 32)retSuccess = 1;
     }

Note the use of PUSERDATA in the method header. This allows the code to be FSX / P3D agnostic but you must have the correct #ifdef somewhere in your project.

#ifdef _64BIT
  #define PUSERDATA UINT_PTR
 (etc.)
#else
  #define PUSERDATA UINT32
  (etc.)
#endif