Wiki Page Content

Differences between revisions 7 and 8
Revision 7 as of 2013-12-07 14:02:14
Size: 1825
Comment: Changed spaces in example.
Revision 8 as of 2017-03-11 22:54:16
Size: 1835
Comment: Fixed include and comments in example.
Deletions are marked like this. Additions are marked like this.
Line 25: Line 25:
#include "SDL_loadso.h" #include "SDL.h"
Line 27: Line 27:
// Variable declaration /* Variable declaration */
Line 32: Line 32:
// Dynamically load mylib.so /* Dynamically load mylib.so */
Line 35: Line 35:
// Load the exported function from mylib.so
// The exported function has the following prototype
// void myFancyFunction(int anInt);
/* Load the exported function from mylib.so
 * The exported function has the following prototype
 * void myFancyFunction(int anInt);
 */
Line 40: Line 41:
// Call myFancyFunction with a random integer /* Call myFancyFunction with a random integer */
Line 44: Line 45:
    // Error handling here     /* Error handling here */

SDL_LoadFunction

Use this function to look up the address of the named function in the shared object and return it.

Syntax

void* SDL_LoadFunction(void*       handle,
                       const char* name)

Function Parameters

handle

a valid shared object handle returned by SDL_LoadObject()

name

the name of the function to look up

Return Value

Returns a pointer to the function or NULL if there was an error; call SDL_GetError() for more information.

Code Examples

#include "SDL.h"

/* Variable declaration */
void* myHandle = NULL;
char* myFunctionName = "myFancyFunction";
void (*myFancyFunction)(int anInt);

/* Dynamically load mylib.so */
myHandle = SDL_LoadObject("mylib.so");

/* Load the exported function from mylib.so
 * The exported function has the following prototype
 * void myFancyFunction(int anInt);
 */
myFancyFunction = (void (*)(int))SDL_LoadFunction(myHandle, myFunctionName);

/* Call myFancyFunction with a random integer */
if (myFancyFunction != NULL) {
    myFancyFunction(15);
} else {
    /* Error handling here */
}

Remarks

This function pointer is no longer valid after calling SDL_UnloadObject().

This function can only look up C function names. Other languages may have name mangling and intrinsic language support that varies from compiler to compiler.

Make sure you declare your function pointers with the same calling convention as the actual library function. Your code will crash mysteriously if you do not do this.


CategoryAPI, CategorySharedObject

None: SDL_LoadFunction (last edited 2017-03-11 22:54:16 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit