DLL Programmer's Guide for TNS/E Systems
Example Code
DLL Programmer’s Guide for TNS/E Systems—527252-006
6-7
Example Two
Example Two
This example was created using TNS/R tools, thus uses ld instead of eld as the
linker. The use of the dynamic loader rld is the same on either TNS/R or TNS/E.
This session demonstrates dynamic loading. We create and compile a program and a
DLL, but don’t load the DLL with the program. We invoke the loader from inside the
process with a dlopen() call.
If you set breakpoints in Visual Inspect just before and after executing such calls, and
use the new TACL command LOADEDFILES, you can see the process in action.
Display the Source Code
Again there is a program and a library in the session, mainc and helloc. In mainc we
use dlopen() to load hellodll, which will be created from helloc. We use dlsym() to find
function hello(), which we invoke. We then use dlclose() to dynamically drop that
loaded file. Just to demonstrate an error routine, we then attempt to load a non-existent
DLL called NotThere.
Note that dlopen("hellodll",RTLD_NOW) takes a pathname that typically is a relative
pathname. It is qualified by the default directory searchlist which can be overridden at
link or load time. At load time, the searchlist is modified by the =RLD* defines (for
Guardian) and environmental variables (for OSS processes).
The source code for mainc is as follows:
#include <dlfcn.h>
#include "hello.h" nolist
#include <stdio.h> nolist
/***********************************************************
| main:
|
\***********************************************************/
int main(int argc, char *argv[]) {
int result = 0;
union {
int resCode;
short err[2];
} res;
typedef void (*HelloFPtr)(void);
HelloFPtr pHello;
dlHandle dlopenHandle;
dlopenHandle = dlopen("hellodll",RTLD_NOW);
if (dlopenHandle == 0) {
printf( "%s\n", dlerror( ) );
return (1);
}
pHello = (HelloFPtr)dlsym(dlopenHandle, "hello");
if (pHello == 0) {
printf( "%s\n", dlerror( ) );
return (2);
}
pHello( );










