0

i am trying to communicate with a dll from an exe and when i add WINAPI in the dll i get a NULL value during the call GetProcAddress().

Sample Code is:

EXE:

typedef int __declspec(dllexport) (WINAPI *fun)(int, int);

int main()
{
    HINSTANCE hdll;
    fun fp;

    int a = 120;
    int b = 80;

    hdll = LoadLibrary(TEXT("baladll.dll"));
    if (hdll != NULL)
    {
        fp = (fun)GetProcAddress((HMODULE)hdll, "add");
        if (fp != NULL)
        {
            int c = fp(a, b);
            printf("\n Add is : %d", c);;
        }
    }
    return 0;
}

DLL:

Header file:

int __declspec(dllexport) WINAPI add(int a, int b);

Source file:

#include "dll.h"

int  __declspec(dllexport) WINAPI add(int a, int b)
 {
     return a + b;
 }

When the same code is run without WINAPI call i get a non NULL value in GetProcAddress().

Can anyone help me with this.

Thanks.

Buddy
  • 10,874
  • 5
  • 41
  • 58
  • You get a NULL when you pass the wrong argument to GetProcAddress(). Not a lot of choice when you have to guess which one could be wrong. It is not "add". Use the linker's .map file or use Dumpbin.exe /exports on the DLL to see the real name. – Hans Passant Sep 08 '15 at 18:51
  • Hi @Hans, I did try using DUMPBIN /Exports and saw that dll name was same as that of what I have mentioned in the exe. what my question here was when i use typedef int __declspec(dllexport) (*fun)(int, int); in my exe and the same declaration in my dll I do not see a NULL during the GetProcAddress call. why i get a NULL on adding the "__stdcall " calling convention typedef int __declspec(dllexport) (WINAPI *fun)(int, int); so any help on this would be useful. Thanks. – Jiraya Sama Sep 09 '15 at 05:34
  • That sounds unlikely, show us the output of Dumpbin. The name should be "_add@8", the normal decoration for stdcall functions. – Hans Passant Sep 09 '15 at 07:31
  • Hi @Hans Sorry about before. I did Dumpbin.exe /exports on the dll which does not have WINAPI in the function. the output of which was like : 1 0 000110FA _add@8 = @ILT+245(_add@8) Once I added WINAPI in the function I see the change, what you mentioned 1 0 000110FA _add@8 = @ILT+245(_add@8) when I changed the code from fp = (fun)GetProcAddress((HMODULE)hdll, "add"); to fp = (fun)GetProcAddress((HMODULE)hdll, "_add@8"); I got a non Null value and am able to communciate with both the app's. Thanks a lot! – Jiraya Sama Sep 09 '15 at 12:10

0 Answers0