The use of DLLs can also be explicitly controlled by C/C++ application
code at the source level. The application uses explicit source-level
calls to one or more runtime services to connect the reference to
the definition. The connections for the reference and the definition
are made at runtime.
The DLL application writer can explicitly call the following C
runtime services:
- dllload(), which loads the DLL and returns a
handle to be used in future references to this DLL
- dllqueryfn(), which obtains a pointer to a DLL
function
- dllqueryvar(), which obtains a pointer to a DLL
variable
- dllfree(), which frees a DLL loaded with dllload()
The following runtime services are also available as part of the
Single UNIX Specification, Version
3:
- dlopen(), which loads the DLL and returns a handle
to be used in future references to this DLL
- dlclose(), which frees a DLL that was loaded
with dlopen()
- dlsym(), which obtains a pointer to an exported
function or exported variable
- dlerror(), which returns information about the
last DLL failure on this thread that occurred in one of the dlopen() family
of functions
While you can use both families of explicit DLL services in a single
application, you cannot mix usage across those families. So a handle
returned by dllload() can only be used with dllqueryfn(), dllqueryvar(),
or dllfree(). And a handle returned by dlopen() can
only be used with dlsym() and dlclose().
Since the dlopen() family of functions are part
of the Single UNIX Specification,
Version 3, they should be used in new applications if cross-platform
portability is a concern.
For more information about the C runtime services, see z/OS XL C/C++ Runtime Library Reference.
To explicitly call a DLL in your application:
- Determine the names of the exported functions and variables that
you want to use. You can get this information from the DLL provider's
documentation or by looking at the definition side-deck file
that came with the DLL. A definition side-deck is
a directive file that contains an IMPORT control
statement for each function and variable exported by that DLL.
- If you are using the dllload() family of functions,
include the DLL header file <dll.h> in your application. If
you are using the dlopen() family of functions, include
the DLL header file <dlfcn.h> in your application.
- Compile your source as usual.
- Bind your object with the binder using the same AMODE value as
the DLL.
Note: You do not need to bind with the definition side-deck if
you are calling the DLL explicitly with the runtime services, since
there are no references from the source code to function or variable
names in the DLL for the binder to resolve. Therefore the DLL will
not be loaded until you explicitly load it with the dllload() or dlopen() runtime
service.
Explicit use of a DLL in a C application and Explicit use of a DLL in a COBOL/C application have examples of applications
that use explicit DLL calls.