The problems is that the exported names of class member are decorated: extern "C" does not work with classes, as they have to be C++ (there's no such thing as a C class).
Building your files into a dll (MathFuncsDll.dll) and then dumping its exports (using "link /dump /exports MathFuncsDll.dll" in a Visual Studio Command Prompt) I get
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file MathFuncsDll.dll
File Type: DLL
Section contains the following exports for MathFuncsDll.dll
00000000 characteristics
4ED1F9AF time date stamp Sun Nov 27 08:49:51 2011
0.00 version
1 ordinal base
5 number of functions
5 number of names
ordinal hint RVA name
1 0 000111A4 ?Add@MyMathFuncs@@SANNN@Z = @ILT+415(?Add@MyMathFuncs@@SANNN@Z)
2 1 0001100A ?Divide@MyMathFuncs@@SANNN@Z = @ILT+5(?Divide@MyMathFuncs@@SANNN@Z)
3 2 00011037 ?Multiply@MyMathFuncs@@SANNN@Z = @ILT+50(?Multiply@MyMathFuncs@@SANNN@Z)
4 3 0001108C ?Subtract@MyMathFuncs@@SANNN@Z = @ILT+135(?Subtract@MyMathFuncs@@SANNN@Z)
5 4 000110C3 f = @ILT+190(_f)
Summary
1000 .data
2000 .idata
3000 .rdata
1000 .reloc
1000 .rsrc
6000 .text
10000 .textbss |
So I need to call GetProcAddress with "?Add@MyMathFuncs@@SANNN@Z" to find MyMathFuncs::Add(). The name mangling is version specific (I'm using VC++ 2008, so you might be lucky with '2005 or 2010, but prob not otherwise)
But you can use a def file to provide a friendly alias for the name.
1 2 3 4 5 6 7 8
|
; MathsFuncDll.def
LIBRARY "MathFuncsDll"
EXPORTS
MyMathFuncs_Add = ?Add@MyMathFuncs@@SANNN@Z
MyMathFuncs_Divide = ?Divide@MyMathFuncs@@SANNN@Z
MyMathFuncs_Multiply = ?Multiply@MyMathFuncs@@SANNN@Z
MyMathFuncs_Subtract = ?Subtract@MyMathFuncs@@SANNN@Z
|
After rebuilding, the linker export dump shows 4 extra new entrypoints: MyMathFuncs_Add, MyMathFuncs_Divide, MyMathFuncs_Multiply, and MyMathFuncs_Subtract.
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file MathFuncsDll.dll
File Type: DLL
Section contains the following exports for MathFuncsDll.dll
00000000 characteristics
4ED1FCBB time date stamp Sun Nov 27 09:02:51 2011
0.00 version
1 ordinal base
9 number of functions
9 number of names
ordinal hint RVA name
1 0 000111A4 ?Add@MyMathFuncs@@SANNN@Z = @ILT+415(?Add@MyMathFuncs@@SANNN@Z)
2 1 0001100A ?Divide@MyMathFuncs@@SANNN@Z = @ILT+5(?Divide@MyMathFuncs@@SANNN@Z)
3 2 00011037 ?Multiply@MyMathFuncs@@SANNN@Z = @ILT+50(?Multiply@MyMathFuncs@@SANNN@Z)
4 3 0001108C ?Subtract@MyMathFuncs@@SANNN@Z = @ILT+135(?Subtract@MyMathFuncs@@SANNN@Z)
5 4 000111A4 MyMathFuncs_Add = @ILT+415(?Add@MyMathFuncs@@SANNN@Z)
6 5 0001100A MyMathFuncs_Divide = @ILT+5(?Divide@MyMathFuncs@@SANNN@Z)
7 6 00011037 MyMathFuncs_Multiply = @ILT+50(?Multiply@MyMathFuncs@@SANNN@Z)
8 7 0001108C MyMathFuncs_Subtract = @ILT+135(?Subtract@MyMathFuncs@@SANNN@Z)
9 8 000110C3 f = @ILT+190(_f)
Summary
1000 .data
2000 .idata
3000 .rdata
1000 .reloc
1000 .rsrc
6000 .text
10000 .textbss |
which means you can now use
ptr2 add = (ptr2)GetProcAddress(hDLL, "MyMathFuncs_Add");
to obtain a pointer to your add function.
See "Explicitly Linking to Classes in DLL's"
http://www.codeguru.com/cpp/w-p/dll/importexportissues/article.php/c123
for further information, and another approach or two.
Andy
PS I see your function pointers are declared with explicit calling convention. The functions themselves should also be delcared with (the same) explicit calling convention. This is always a good idea for DLL exports, in case the default calling convention of the DLL is different to the calling app.