wmain.c:
#include <stdio.h> /* printf */
#include <string.h> /* strcpy, strcat */
#include <windows.h> /* DLL stuff */
/* conveniences for casting */
typedef void (*PRINTME)(const char *);
typedef int (*ADD)(int, int);
int main(void)
{
const char *soname = "hello"; /* base name of a .dll or .so */
const char *fname1 = "printme"; /* function to call (text) */
const char *fname2 = "add"; /* function to add (text) */
PRINTME printme; /* void printme(const char *) */
ADD add; /* int add(int, int) */
HANDLE lib; /* library handle */
int result; /* result of the addition */
char buffer[100]; /* used to build the filename */
/* decorate the filename: hello ==> hello.dll */
strcpy(buffer, soname);
strcat(buffer, ".dll");
/* load the shared library */
lib = LoadLibrary(buffer);
if (!lib)
{
printf("Unable to load DLL: %s\n", buffer);
return -1;
}
/* get print function and call it */
printme = (PRINTME)GetProcAddress(lib, fname1);
if (!printme)
printf("Function not found: %s\n", fname1);
else
printme("Print this!");
/* get add funtion and call it */
add = (ADD)GetProcAddress(lib, fname2);
if (!add)
printf("Function not found: %s\n", fname2);
else
{
result = add(10, 20);
printf("The sum is: %i\n", result);
}
/* library no longer needed */
FreeLibrary(lib);
return 0;
}
Output:
Hello from library: Print this!
The sum is: 30