| Front End Extensions / Using Front End Extensions | |
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.
/*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);
#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 stopsvoid finalize() {
}
// our makeSum function. // --> in parameters
the front End Interface Structureint makeSum(const struct frontEndInterface &fx) {
//
initialize the statusshort 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;
}
$fglrun testDLL 1 + 3 = 4 4