Hello,
I'm currently experimenting with DLL.
Let's say I have this libxmath.dll:
__declspec(dllexport) int xmath_add(int aa, int bb){
return aa+bb;
}
__declspec(dllexport) int xmath_subtract(int aa, int bb){
return aa-bb;
}
__declspec(dllexport) int xmath_multiply(int aa, int bb){
return aa*bb;
}
podump's output:
Quote
Dump of libxmath.dll
File type: DLL
Exported symbols for libxmath.dll
0 characteristics
544931E4 time date stamp (Thu Oct 23 23:50:44 2014)
0.00 version
1 ordinal base
3 number of functions
3 number of names
ordinal hint address name
1 0 10001000 _xmath_add@8
2 1 10001020 _xmath_multiply@8
3 2 10001010 _xmath_subtract@8
SUMMARY
2000 .data
1000 .rdata
1000 .reloc
3000 .text
What I want is to load the DLL directly, without using .h and .lib files.
This is my test code:
#include <windows.h>
#include <stdio.h>
typedef int (*pFunction)(int,int);
int main(void){
HINSTANCE hLib = LoadLibrary("libxmath.dll");
pFunction pFuncAdd;
if (hLib){
printf("libxmath.dll is successfully loaded...\n");
pFuncAdd = (pFunction) GetProcAddress(hLib, "xmath_add");
if (pFuncAdd){
printf("xmath_add() is found...\n");
printf("10 + 20 = %d\n", pFuncAdd(10,20));
}
else {
printf("xmath_add() cannot be found...\n");
}
}
else printf("libxmath.dll cannot be found...\n");
FreeLibrary(hLib);
return 0;
}
The output is:
Quote
libxmath.dll is successfully loaded...
xmath_add() cannot be found...
Press any key to continue...
I wonder why the function cannot be located :-\
Include something like this def-file to project:
LIBRARY XMath
EXPORTS
xmath_add = _xmath_add@8
xmath_multiply = _xmath_multiply@8
xmath_subtract = _xmath_subtract@8
or#pragma comment(linker, "-export:xmath_add=_xmath_add@8")
#pragma comment(linker, "-export:xmath_subtract=_xmath_subtract@8")
#pragma comment(linker, "-export:xmath_multiply=_xmath_multiply@8")
int xmath_add(int aa, int bb){
return aa+bb;
}
int xmath_subtract(int aa, int bb){
return aa-bb;
}
int xmath_multiply(int aa, int bb){
return aa*bb;
}
Another approach without def file or pragma it will be
1. How you can observe from the dump the exported names are:
Quote
ordinal hint address name
1 0 10001000 _xmath_add@8
2 1 10001020 _xmath_multiply@8
3 2 10001010 _xmath_subtract@8
so you call GetProcAdress() with GetProcAddress(hLib, "_xmath_add@8");
2. You rebuild the dll but this time you check in:
Project/Project options/Compiler - undecorate exported __stdcall functions,
the dump it will be something like:
Quote
ordinal hint address name
1 0 10001000 xmath_add
2 1 10001020 xmath_multiply
3 2 10001010 xmath_subtract
and you can call now GetProcAddress(hLib, "xmath_add");.
Laur
Quote from: TimoVJL on October 23, 2014, 10:00:09 PM
Include something like this def-file to project:
LIBRARY XMath
EXPORTS
xmath_add = _xmath_add@8
xmath_multiply = _xmath_multiply@8
xmath_subtract = _xmath_subtract@8
I used the Win32 DLL template, not the Win32 DLL wizard. How to process this def file?
Quote from: laurro on October 23, 2014, 10:56:17 PM
2. You rebuild the dll but this time you check in:
Project/Project options/Compiler - undecorate exported __stdcall functions,
the dump it will be something like:
Quote
ordinal hint address name
1 0 10001000 xmath_add
2 1 10001020 xmath_multiply
3 2 10001010 xmath_subtract
and you can call now GetProcAddress(hLib, "xmath_add");.
Laur
Thank you. I like this solution. It works.
Unfortunately, now my program crashes:
Quote
libxmath.dll is successfully loaded...
xmath_add() is found...
10 + 20 = 30
CRT: unhandled exception (main) -- terminating
*** Process returned 255 ***
Press any key to continue...
The DLL handle has been unloaded, right? :-\
Quote from: andre104 on October 24, 2014, 03:19:51 AM
I used the Win32 DLL template, not the Win32 DLL wizard. How to process this def file?
just insert file like libxmath.def into project.
Quote from: andre104 on October 24, 2014, 03:23:40 AM
Thank you. I like this solution. It works.
Unfortunately, now my program crashes:
Quote
libxmath.dll is successfully loaded...
xmath_add() is found...
10 + 20 = 30
CRT: unhandled exception (main) -- terminating
*** Process returned 255 ***
Press any key to continue...
The DLL handle has been unloaded, right? :-\
define that function as stdcall as it was in dll:
typedef int __stdcall (*pFunction)(int,int);
BTW:
Project/Project options/Compiler - undecorate exported __stdcall functions, -Gn
insert these directives to object-file.
-export:xmath_add=_xmath_add@8
-export:xmath_subtract=_xmath_subtract@8
-export:xmath_multiply=_xmath_multiply@8
The following works for me.
#include <windows.h>
#include <stdio.h>
int main(void){
static FARPROC pFuncAdd;
HINSTANCE hLib = LoadLibrary("libxmath.dll");
if (hLib){
printf("libxmath.dll is successfully loaded...\n");
pFuncAdd = GetProcAddress(hLib, "xmath_add");
if (pFuncAdd){
printf("xmath_add() is found...\n");
printf("10 + 20 = %d\n", pFuncAdd(10,20));
}
else {
printf("xmath_add() cannot be found...\n");
}
}
else printf("libxmath.dll cannot be found...\n");
FreeLibrary(hLib);
return 0;
}