NO

Author Topic: Problem with loading function in DLL  (Read 9321 times)

andre104

  • Guest
Problem with loading function in DLL
« on: October 23, 2014, 06:57:47 PM »
Hello,

I'm currently experimenting with DLL.

Let's say I have this libxmath.dll:
Code: [Select]
__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:
Code: [Select]
#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  :-\

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Problem with loading function in DLL
« Reply #1 on: October 23, 2014, 10:00:09 PM »
Include something like this def-file to project:
Code: [Select]
LIBRARY XMath
EXPORTS
xmath_add = _xmath_add@8
xmath_multiply = _xmath_multiply@8
xmath_subtract = _xmath_subtract@8
or
Code: [Select]
#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;
}
« Last Edit: October 23, 2014, 10:05:56 PM by TimoVJL »
May the source be with you

laurro

  • Guest
Re: Problem with loading function in DLL
« Reply #2 on: October 23, 2014, 10:56:17 PM »
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

andre104

  • Guest
Re: Problem with loading function in DLL
« Reply #3 on: October 24, 2014, 03:19:51 AM »
Include something like this def-file to project:
Code: [Select]
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?

andre104

  • Guest
Re: Problem with loading function in DLL
« Reply #4 on: October 24, 2014, 03:23:40 AM »
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?  :-\


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Problem with loading function in DLL
« Reply #5 on: October 24, 2014, 05:50:59 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.

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:
Code: [Select]
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
« Last Edit: October 24, 2014, 06:02:13 AM by TimoVJL »
May the source be with you

Offline Robert

  • Member
  • *
  • Posts: 247
Re: Problem with loading function in DLL
« Reply #6 on: October 24, 2014, 09:21:45 AM »
The following works for me.

Code: [Select]

#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;
}