PDA

View Full Version : SimConnect Open/Close server code C++



MechDave
07-10-2009, 08:33 PM
Here is the code I have got working for opening and closing the server after a pause, import it into MS VS C++ after creating and setting up the project.



// SimConnectServer.cpp : Defines the entry point for the console application.
//Opens a connection to the server for 9000 ms, reports that the connection is alive
//and then closes the connection again.
//Written by MechDave using SDK Samples Open and Close as a code base.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#include "SimConnect.h"

HANDLE hSimConnect = NULL;


void Connect()
{
HRESULT hr;
if (hSimConnect == NULL)
{
printf("Requesting a SimConnect connection");
hr = SimConnect_Open(&hSimConnect,"Connect to Server for 9000 ms",NULL,0,0,SIMCONNECT_OPEN_CONFIGINDEX_LOCAL );
if (hr != S_OK)
{
//FSX may be inactive
printf("\nError %d", hr);
}
else
{
//Connection with FSX initialised
printf("\nConnection request being processed (%d)", hSimConnect);
//When connection process complete, an OPEN message will be recieved.
}
}
}

void Disconnect()
{
HRESULT hr;
hr = SimConnect_Close(hSimConnect);
printf("\nDisconnected from Flight Simulator");
if (hr != S_OK)
{
printf("\nFailed to connect to Flight Simulator");
}
else
{
printf("Disconnection Sucessful");
}
}

int __cdecl _tmain(int argc, _TCHAR* argv[])
{
Connect();
Sleep(9000);
Disconnect();

return 0;
}