Thread: Overloaded functions and Dll(with Explicit Linking)

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Overloaded functions and Dll(with Explicit Linking)

    Hi,
    I wish to export two overloaded function from a dll and then use them in a client program which access the dll through explicit linking. The GetProcAddress() function fails! I understand that it is because when Dll is built , the names of exported functions are changed. If I use extern "C" while exporting then I can't overload the functions.

    code of the dll written in a .cpp file is:

    #define DLLEXPORT __declspec(dllexport)

    DLLEXPORT int MySquareFunction(int);
    DLLEXPORT double MySquareFunction(double);

    int MySquareFunction(int i)
    {
    return i*i;
    }

    double MySquareFunction(double i)
    {
    return i*i;
    }


    code of the client is:

    #include <iostream.h>
    #include <windows.h>

    int main(void)
    {
    int i ,ii;
    double d,dd;
    typedef int (*FPTR1)(int);
    typedef double (*FPTR2)(double);
    HINSTANCE hInstance;
    FPTR1 fp1;
    FPTR2 fp2;


    hInstance = LoadLibrary("F:\\nishant\\My Code\\StudyofDlls\\cpp\\ExpLink\\ExpCppDllProj\\De bug\\ExpCppDllProj.dll");
    if(hInstance != NULL)
    {
    fp1 = (FPTR1) GetProcAddress(hInstance,"MySquareFunction");
    if(fp1 != NULL )
    {
    cout<<"\nEnter an integer: ";
    cin>>i;
    ii = (*fp1)(i);
    cout<<"\nSquare of "<<i<<" is: "<<ii;
    }
    else
    {
    cout<<"\nThe address of int MySquareFunction(int) function could not be located.";
    }

    fp2 = (FPTR2) GetProcAddress(hInstance,"MySquareFunction");
    if(fp2 != NULL )
    {
    cout<<"\nEnter a double: ";
    cin>>d;
    dd = (*fp2)(d);
    cout<<"\nSquare of "<<d<<" is: "<<dd;
    }
    else
    {
    cout<<"\nThe address of double MySquareFunction(double) function could not be located.\n";
    }

    FreeLibrary(hInstance);
    }
    else
    {
    cout<<"\nThe LoadLibrary() function failed.\n";
    }
    return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You cant do it.....GetProcAddress expects C linkage...no name decoration...therefore no overloading.......either use 2 function names or link via a lib file

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Hi Fordy,
    Thanx for clarification, but that means i can't export classes and then import them explicitlv with GetProcAdress().

    Regards

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Yes you can export objects, but it's not straight forward.

    Normally, you export a function to create an instance of your object and return a pointer to it. You also need to export a function to destroy it.

    In addition, if you want compatability between compilers, and/or between different versions of your DLL, you need to use pure virtual base classes and export pointers to these.

    Here's some info:

    www.bcbdev.com/articles/vcdll2.htm

Popular pages Recent additions subscribe to a feed