Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 dlfcn.h
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: Tue Mar 08, 2005 9:16 am   Post subject: dlfcn.h

I'm having some trouble figuring out just how dlsym works.

The man page for it says that
Quote:
The function dlsym() takes a "handle" of a dynamic library returned by dlopen and the NUL-terminated symbol name, returning the address where that symbol is loaded into memory.


Now I realize that this code returns a function pointer, but just how (or why) we do this I'm really not getting.

Here's the function that I'm working with:

code:
/*
   Determine what mapping function must be called for the given datspot
   record and call it
*/
int processDatSpotRecord(void *dl, xmlDocPtr xml_doc, xmlNodePtr xml_node, lc2if_ *pLC2DB, mapping::Mapper *mapper)
{
        int retval;
        struct datspot_id_t *id;

        int ( *func )( xmlDocPtr xml_doc, xmlNodePtr xml_node,struct datspot_id_t *id, lc2if_ *pLC2DB, mapping::Mapper *mapper );

        id = lookup_datspot_id();
        if (id == NULL)
        {
                return -99;
        }

//      printf("before function call\n");
        /* run the function for this unit if we know about it. */
        func = (int (*)( xmlDocPtr, xmlNodePtr, struct datspot_id_t *, lc2if_ *, mapping::Mapper *)) dlsym(dl, id->action);

        printf("action name is %s\n", id->action);
        if (!func)
        {
                fprintf(stderr,"whoopsie!  dlsym failed: %s\n",dlerror());
                return -98;
        }

        return func(xml_doc, xml_node, id, pLC2DB, mapper);
}
Sponsor
Sponsor
Sponsor
sponsor
McKenzie




PostPosted: Thu Mar 17, 2005 10:35 am   Post subject: (No subject)

Function pointers:
The problem with calling a function is that you need to know the name of the function you are calling, right? Well in C the idea was if they allowed a reference to a function then to call the function all you would have to do is derefference the pointer.

In your example, if dlsym() returned say an integer then you would have to make a large if or select statement to actually call the function it was referring to. As you know with a dll this wouldn't even work because the actual functions can change.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: