Hello All i have a program written in C, and i want to call a sharedobject, which i can do fine!

The only problem i am having is passing parameters.. Here is my code

int main()
{
    static int (*fp)(int);  
    int *handle;
    int MatchFound;

    struct SharedObject_{
        char name[10];
        char add[80];
    }SharedObject;
 
    strcpy(SharedObject.name, "STEPHEN" );
    strcpy(SharedObject.add, "PLACE HOUSE FARM" );
    
    printf("Sharedobject name = %s\n", SharedObject.name );
    printf("Sharedobject add = %s\n", SharedObject.add );
    
    /* Open shared object */
    if ( (handle = dlopen("sharedobject.so", RTLD_LAZY)) == NULL)
        return -1;
    if ((fp = (int (*)())dlsym(handle, "my_add")) == NULL)
        return -1;
    if ( (MatchFound = (*fp)((int) &SharedObject)) == -1 )
        return -1;                                        
}

The my add is a procuedure in my shared object, which looks like below

#include<stdio.h>
int my_add(char *name, char *add)
{
    
    printf("In Sharedobject\n" );
    printf("name  = %s\n", name );
    printf("add  = %s\n", add );
}

When i run this, i am getting the following output:-

Sharedobject name = STEPHEN
Sharedobject add = PLACE HOUSE FARM
In Sharedobject
name = STEPHEN
add = my_add

When i would expect to see :-

Sharedobject name = STEPHEN
Sharedobject add = PLACE HOUSE FARM
In Sharedobject
name = STEPHEN
add = PLACE HOUSE FARM

Any ideas?

Thanks

First of all, your function pointer MUST be declared to be exactly the same as the function you're pointing at. Declaring int where it expects two char*'s for example isn't the way to go.

Second, use typedef's for function pointers. I usually avoid typedefs for pointers, but function pointers are so unweildy that I make an exception.

Try this

typedef int (*addfn_t)(char*,char*);

int main()
{
    // static int (*fp)(int);  why was this static?
    addfn_t fn;
    int *handle;
    int MatchFound;

    struct SharedObject_{
        char name[10];
        char add[80];
    }SharedObject;
 
    strcpy(SharedObject.name, "STEPHEN" );
    strcpy(SharedObject.add, "PLACE HOUSE FARM" );
    
    printf("Sharedobject name = %s\n", SharedObject.name );
    printf("Sharedobject add = %s\n", SharedObject.add );
    
    /* Open shared object */
    if ( (handle = dlopen("sharedobject.so", RTLD_LAZY)) == NULL)
        return -1;
    if ((fp = (addfn_t)dlsym(handle, "my_add")) == NULL)
        return -1;
    if ( (MatchFound = fp(SharedObject.name, SharedObject.add) ) == -1 )
        return -1;                                        
}

Aside from casting the result of dlsym(), you shouldn't need any casting magic to get the function to be called properly. If you do, you're definitely doing something wrong.

In particular, this fp(SharedObject.name, SharedObject.add) should just look exactly like what you would have done calling the function directly, like this my_add(SharedObject.name, SharedObject.add)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.