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;
}
[1995 byte] By [nishantghai] at [2007-11-18 3:46:53]
# 1 Re: Overloaded functions and Dll(with Explicit Linking)
hi,

1. check with Depends utility how are these functions exported, i.e. which names do they have

2. if you wanna use the names like MySquareFunction, define these functions inside def-file:

; user.def : ...

LIBRARY "user"
DESCRIPTION 'user Windows Dynamic Link Library'

EXPORTS

MySquareFunction

Regards,
alex_gusev at 2007-11-9 13:03:27 >
# 2 Re: Overloaded functions and Dll(with Explicit Linking)
must be in an extern "C" block for it to export in C fashion without C++ name mangling. Just put your whole code into an extern "C" block.

extern "C" {
// all the rest
}
JamesSchumacher at 2007-11-9 13:04:26 >
# 3 Re: Overloaded functions and Dll(with Explicit Linking)
plz help
when i comply my dll project i got 3 error, i can understand what it is, can you tell me !!

-------Configuration: MyWinsock2DLL - Win32 Release-------
Compiling...
MyDLL.cpp
Linking...
MyDLL.def : error LNK2001: unresolved external symbol MyDLL
Release/MyDLL.lib : fatal error LNK1120: 1 unresolved externals
LINK : fatal error LNK1141: failure during build of exports file
Error executing link.exe.

MyDLL.dll - 3 error(s), 0 warning(s)
neocarton at 2007-11-9 13:05:36 >