In this example, _loadmod loads the DLL mark and gets the address of the function PLUS within the DLL. It then calls that function, which adds two integers passed to it. The types PFN and APIRET are required to use the OS/2 DosQueryProcAddr API, and are defined by including the <os2.h> header file.
Note: To run this program, you must first create mark.dll. Copy the code for the PLUS function into a file called mark.c, and the code for the .DEF file into mark.def. Eliminate the comments from these two files. Compile the example.
You can then run the example.
#define INCL_DOS #include <os2.h> #include <stdio.h> #include <stdlib.h>
/* This is the code for MARK.DEF
LIBRARY MARK
PROTMODE
EXPORTS PLUS */
/* This is the code for PLUS function in the DLL
#pragma linkage(PLUS, system)
int PLUS(int a, int b)
{
return a + b;
}
int main(void)
{
int x = 4, y = 7;
unsigned long handle;
char *modname = "MARK"; /* DLL name */
char *fname = "PLUS"; /* function name */
PFN faddr; /* pointer to function */
APIRET rc; /* return code from DosQueryProcAddr */
/* dynamically load the 'mark' DLL */
if (_loadmod(modname, &handle)) {
printf("Error loading module %s\n", modname);
return EXIT_FAILURE;
}
/* get function address from DLL */
rc = DosQueryProcAddr(handle, 0, fname, &faddr);
if (0 == rc) {
printf("Calling the function from the %s DLL to
add %d and %d\n", modname, x, y);
printf("The result of the function call is %d\n",
faddr(x, y));
}
else {
printf("Error locating address of function %s\n",
fname);
_freemod(handle);
return EXIT_FAILURE;
}
if (_freemod(handle)) {
printf("Error in freeing the %s DLL module\n",
modname);
return EXIT_FAILURE;
}
printf("Reference to the %s DLL module has been freed\n",
modname);
return 0;
/********************************************************
The output should be:
Calling the function from the MARK DLL to add 4 and 7
The result of the function call is 11
Reference to the MARK DLL module has been freed
********************************************************/
}