Front End example: create a basic extension

This section provides a Front End example.

This very basic extension illustrates how to create a DLL. It has been created using Visual C++ 6.

This DLL has only one function called makeSum. Two Integers are given in parameters, and the function returns a String and an Integer. The Integer contains the sum, the String a small text.

Header file : myDLL.h
/*the interface structure*/struct frontEndInterface {
short (* getParamCount) ();
short (* getReturnCount) ();
void ( * popInteger) (long & , short & );
void ( * pushInteger) (const long , short );
void ( * popString) (char *, short &, short &);
void ( * pushString) (const char *,short , short );
void ( * getFrontEndEnv(const char *, char *, short&);
void ( * popWString) (wchar_t *, short &, short &);
void ( * pushWString) (const wchar_t*,short , short );
};
// a small macro used to declare the "exportable" functions#ifdef WIN32
/*dllexport is only valid (and mandatory) under windows*/
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT extern "C"
#endif

// the functions we want to be available from the front endEXPORT void initialize();
EXPORT void finalize();
EXPORT int makeSum(const frontEndInterface &fx);
Source file : myDLL.cpp// some includes
#include "myDLL.h"
#include <stdio.h>

// this function will be called by the Front End the first time the DLL is loaded
void initialize() {
}
// this function will be called by the Front End when the Front End stops
void finalize() {
}

// our makeSum function. // --> in parameters the front End Interface Structure
int makeSum(const struct frontEndInterface &fx) {
// initialize the status
  short status = -1;
// check if the in and out parameters are correct
    if (fx.getParamCount() == 2 && fx.getReturnCount() == 2) {
      long param1, param2;
      short isNull1, isNull2;
// get from the stack each parameter
    fx.popInteger(param2, isNull2);
    fx.popInteger(param1, isNull1);
   
// check if they are not null
    if (!isNull1 && !isNull2) {
    
// create the answer
    long sum = param1 + param2;
    char msg[255];
    sprintf(msg, "%d + %d = %d", 	param1, param2, sum);
 
// push the answer on the stack
    fx.pushInteger(sum, 0);
    fx.pushString(msg, strlen(msg), 0);
// successful -> status = 0
    status = 0;
    }
  }
  return status;
}
Running the program gives the following output:
$fglrun testDLL
1 + 3 = 4
4