Connect Example (C++)

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.

Set Up C++ Connect Example in Visual Studio

To set up the C++ connect example in Visual Studio:

  1. Open Visual Studio.
  2. Create a new project.
  3. Under Visual C++ > Other, select Empty Project.
  4. Enter a name for your project and click OK.
  5. In the Solution Explorer, right-click the Source Files folder and select Add > New Item... .
  6. Select C++ file and enter a name for your file and click OK.
  7. Use the Example code link above and copy all text on that page.
  8. Paste the text to your “cpp” file.
  9. Make sure you have the x64 configuration selected as STK is a 64-bit application.
  10. Right-click on the project and select properties then follow these steps:
    1. Under VC++ Directories:
      1. Add {Your install location goes here}\Connect\Includes to “Include Directories”.
      2. Add {Your install location goes here}\Connect\lib\Debug to "Library Directories".
    2. Under C/C++ > Preprocessor, add "AGCONNECT" to "Preprocessor Definitions".
    3. Under Linker > Input, add “agconnect.lib” to “Additional Dependencies”.
  11. Make sure you have updated the Load command with the path to your STK install.
  12. Build the project.
  13. After building the project, copy AgConnect.dll from {Your install location goes here}\Connect\bin\Debug to your project's output directory.