function that returns value from dlsym()?

1.7k Views Asked by At

Stupid question that I'm sure is some bit of syntax that's not right. How do I get dlsym to work with a function that returns a value? I'm getting the error 'invalid conversion of void* to LSError (*)()' in the following code - trying to get the compile the linux lightscribe sample program hoping that I can link it against the OSX dylib (why the hell won't HP release an actual Cocoa SDK? LS has only been around for what? 6 or 7 years now?):

void* LSHandle = dlopen("liblightscribe.1.dylib", RTLD_LOCAL|RTLD_LAZY);
    if (LSHandle) {
        LSError (*LS_DiscPrinter_ReleaseExclusiveUse)() = dlsym(LSHandle, "LS_DiscPrinter_ReleaseExclusiveUse");

..
lsError = LS_DiscPrinter_ReleaseExclusiveUse( pDiscPrinter);
1

There are 1 best solutions below

0
On BEST ANSWER

The C standard does not actually define behaviour for converting to and from function pointers. Explanations vary as to why; the most common being that not all architectures implement function pointers as simple pointers to data. On some architectures, functions may reside in an entirely different segment of memory that is unaddressable using a pointer to void.

The “recommended” way to use dlsym is:

 LSError (*LS_DiscPrinter_ReleaseExclusiveUse)(LS_DiscPrinterHandle);

 *(void **)&LS_DiscPrinter_ReleaseExclusiveUse = dlsym("LS_DiscPrinter_ReleaseExclusiveUse");

Read the rationale and example on the POSIX page for dlsym for more information.