DLL Programmer's Guide for TNS/R Systems
Sample Sessions and Usage Notes
DLL Programmer’s Guide for TNS/R Systems—522203-002
6-8
Sample Session Three
Sample Session Three
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.
For more information on Visual Inspect, see Inspect and Visual Inspect in the Usage
Notes.
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( );