Connect Example (C++)

C++ code samples that demonstrate how to automate and extend the STK application or build custom applications in C++ are shipped with STK. All C++ code samples are located in the <STK install folder>/CodeSamples/CodeSamples.zip/{CustomApplications | Automation | Extend }/C++/<Project> folder. Since most users do not have write permission to the default install location, we recommend that you copy the CodeSamples folder with the .zip file to a writeable location on your system. That location is your <Code Sample location>, which will be referenced in the instructions below.

All AgConnect applications must be compiled with the AGCONNECT preprocessor definition. The _AFXDLL preprocessor definition is also required. If the AGCONNECT flag isn't defined, the compiler doesn't recognize several internal variable types defined in AgUtMsgCommon.h.

In addition to source and header files, AgConnect is shipped with a library file, AgConnect.lib that must be linked when generating any AgConnect executables.

All AgConnect applications must have the following variable defined:

char AgEAppName[ ]= "<AppName>";

where <AppName> is any name you choose, and is used in AgUtMsg messages. If your source file is written in C++, be sure to define AgEAppName as follows:

extern "C" {
char AgEAppName[ ]="<AppName>";
}

Any C or C++ program that uses AgConnect functions must include AgConnect.h:

#include "AgConnect.h"

Four variables must be defined. First, the context variable, which identifies the connection being used, should be initialized to NULL:

char *connection1 = NULL;

Second, AgConnect requires a file name string to be defined. The file name can be set to NULL, in which case the default file is used, or it can be set to the AgConnect initialization file you wish to use:

char *initFileName = NULL;

Third, AgConnect needs to know the name of the STK connection. This variable takes the form of server:port:

static char connectName[256] = "localhost:5001";

Finally, AgConnect must have a place to store the data returned by Connect commands. Therefore, a variable of type AgTConReturnInfo should be defined:

AgTConReturnInfo returnInfo;

At this point, you can initialize AgConnect by calling:

AgConInit(initFileName);

Next, a connection to STK can be opened up with the AgConOpenSTK() function:

AgConOpenSTK(&connection1, NULL, connectName);

The context variable passed to this function should be passed to any future functions that need to talk to this connection.

Now that AgConnect is initialized and a connection to STK has been established, commands can be sent to STK using a call similar to the one shown here, which loads a scenario (whose file is "C:\STKData\BasicScenarios\Basic.sc") into STK:

AgConProcessSTKCmd (connection1, "Load / Scenario C:\STKData\BasicScenarios\Basic.sc", &returnInfo);

If at any time you wish to receive an asynchronous packet of data to be returned from STK, use the following command:

AgConGetAsync(connection1, &returnInfo);

When you wish to close the connection to STK, use the following command:

AgConCloseSTK(&connection1);

Finally, the call:

AgConShutdownConnect();

frees the memory internally allocated by the AgConnect library. Subsequent to this call, the application can be shut down.

The example code shows a complete program that opens a connection to STK, loads a scenario, sends the AllAccess Connect command, closes the connection and prints out the data. Follow the steps below to set up the C++ connect example code in visual studio.

C++ Connect Example in Visual Studio

The C++ connect Example can be found at <Code Sample location>\Automation\C++\Connect. Make sure you have updated the Load command with the path to your STK install. Build the project.