Explicit use of a DLL in a C application
The following example shows explicit use of a DLL in a C application.
#include <dll.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int (DLL_FN)(void);
#ifdef __cplusplus
}
#endif
#define FUNCTION "FUNCTION"
#define VARIABLE "VARIABLE"
static void Syntax(const char* progName) {
fprintf(stderr, "Syntax: %s <DLL-name> <type> <identifier>\n"
" where\n"
" <DLL-name> is the DLL to load,\n"
" <type> can be one of FUNCTION or VARIABLE\n"
" and <identifier> is the function or variable\n"
" to reference\n", progName);
return;
}
main(int argc, char* argv[]) {
int value;
int* varPtr;
char* dll;
char* type;
char* id;
dllhandle* dllHandle;
if (argc != 4) {
Syntax(argv[0]);
return(4);
}
dll = argv[1];
type = argv[2];
id = argv[3];
dllHandle = dllload(dll);
if (dllHandle == NULL) {
perror("DLL-Load");
fprintf(stderr, "Load of DLL %s failed\n", dll);
return(8);
}
if (strcmp(type, FUNCTION)) {
if (strcmp(type, VARIABLE)) {
fprintf(stderr,
"Type specified was not " FUNCTION " or " VARIABLE "\n");
Syntax(argv[0]);
return(8);
}
/*
* variable request, so get address of variable
*/
varPtr = (int*)(dllqueryvar(dllHandle, id));
if (varPtr == NULL) {
perror("DLL-Query-Var");
fprintf(stderr, "Variable %s not exported from %s\n", id, dll);
return(8);
}
value = *varPtr;
printf("Variable %s has a value of %d\n", id, value);
}
else {
/*
* function request, so get function descriptor and call it
*/
DLL_FN* fn = (DLL_FN*) (dllqueryfn(dllHandle, id));
if (fn == NULL) {
perror("DLL-Query-Fn");
fprintf(stderr, "Function %s() not exported from %s\n", id, dll);
return(8);
}
value = fn();
printf("Result of call to %s() is %d\n", id, value);
}
dllfree(dllHandle);
return(0);
}
The following example shows explicit use of a DLL in an application
using the dlopen() family of functions.
#define _UNIX03_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int (DLL_FN)(void);
#ifdef __cplusplus
}
#endif
#define FUNCTION "FUNCTION"
#define VARIABLE "VARIABLE"
static void Syntax(const char* progName) {
fprintf(stderr, "Syntax: %s <DLL-name> <type> <identifier>\n"
" where\n"
" <DLL-name> is the DLL to open,\n"
" <type> can be one of FUNCTION or VARIABLE,\n"
" and <identifier> is the symbol to reference\n"
" (either a function or variable, as determined by"
" <type>)\n", progName);
return;
}
main(int argc, char* argv[]) {
int value;
void* symPtr;
char* dll;
char* type;
char* id;
void* dllHandle;
if (argc != 4) {
Syntax(argv[0]);
return(4);
}
dll = argv[1];
type = argv[2];
id = argv[3];
dllHandle = dlopen(dll, 0);
if (dllHandle == NULL) {
fprintf(stderr, "dlopen() of DLL %s failed: %s\n", dll, dlerror());
return(8);
}
/*
* get address of symbol (may be either function or variable)
*/
symPtr = (int*)(dlsym(dllHandle, id));
if (symPtr == NULL) {
fprintf(stderr, "dlsym() error: symbol %s not exported from %s: %s\n"
, id, dll, dlerror());
return(8);
}
if (strcmp(type, FUNCTION)) {
if (strcmp(type, VARIABLE)) {
fprintf(stderr,
"Type specified was not " FUNCTION " or " VARIABLE "\n");
Syntax(argv[0]);
return(8);
}
/*
* variable request, so display its value
*/
value = *(int *)symPtr;
printf("Variable %s has a value of %d\n", id, value);
}
else {
/*
* function request, so call it and display its return value
*/
value = ((DLL_FN *)symPtr)();
printf("Result of call to %s() is %d\n", id, value);
}
dlclose(dllHandle);
return(0);
}
For more information on the DLL functions, see z/OS XL C/C++ Runtime Library Reference.