0

I have a dll and header file. Now I am creating a console application using vs2015. and load this library. But while getting GetProcAddress of function. It is always returning NULL. Here is my code. Contents of header file (test.h) (This header file is only for your reference)

    #ifdef APPLYDLL_EXPORTS
#define APPLYDLL_API __declspec(dllexport)
#else
#ifdef BUILD_EXE
#define APPLYDLL_API
#else
#define APPLYDLL_API __declspec(dllimport)
#endif
#endif

#include <string>
#include <winerror.h>

APPLYDLL_API HRESULT ApplySettings(std::string input);

Contents of console application (cpp file)

    #include "stdafx.h"
#include <Windows.h>
#include <string>

using namespace std;
//Define the function prototype
typedef HRESULT(CALLBACK* ApplySettings)(std::string);
int main()
{

    HINSTANCE hLib = LoadLibrary(TEXT("ApplyTool.dll"));
    if (NULL != hLib)
    {
        //Get pointer to our function using GetProcAddress:
        ApplySettings applySettings = (ApplySettings)GetProcAddress(hLib,"ApplySettings");


    DWORD errorcode=GetLastError(); //errorcode 127, Procedure not Found
    }

    return 0;
}

I don't know, Where I am doing silly mistake.

Someone please help me.

Thanks in adv.

Paul R
  • 208,748
  • 37
  • 389
  • 560
CrazyCoder
  • 772
  • 5
  • 11
  • 31
  • 1
    [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Oct 23 '17 at 07:15
  • 3
    Most likely since you use C++ the name is mangled and you’ll have to find the actual name for it – Sami Kuhmonen Oct 23 '17 at 07:16
  • I used dependecy walker to find the actual name of procedure. Other than any way. – CrazyCoder Oct 23 '17 at 07:19
  • 3
    You should use use dumpbin or similar tools to check export table of your library and determine whether it actually contains function named `ApplySettings`. – user7860670 Oct 23 '17 at 07:25
  • you can use next in function body `__pragma(message(__FUNCDNAME__ ));` - it print for you actual function name – RbMm Oct 23 '17 at 07:35
  • or say `__pragma(message(__FUNCDNAME__ " -> " __FUNCSIG__ ));` for print actual function name (which need use in `GetProcAddress` with signature for self comfort) – RbMm Oct 23 '17 at 07:39
  • also when you declare function in header file, which can be used from another project, need always direct set calling conventions. because different project can use different. say you declare `typedef HRESULT(CALLBACK* ApplySettings)(std::string);` when no `CALLBACK` in `APPLYDLL_API HRESULT ApplySettings(std::string input);`. are you use `CALLBACK` as default convention ? – RbMm Oct 23 '17 at 07:46
  • I don't no the default convention in library. I also tried to print function name printf("%s \t %s", __FUNCDNAME__, __FUNCSIG__); it is printing main int __cdecl main(void) – CrazyCoder Oct 23 '17 at 08:47
  • @CrazyCoder - `__FUNCDNAME__` this name of current function. need use it not from main but only from function which name you want get - are this not obvious ? and not `print` but exactly `__pragma(message(__FUNCDNAME__ ));` – RbMm Oct 23 '17 at 09:38
  • and why you at all use `GetProcAddress` instead use generated lib file for static linking ? – RbMm Oct 23 '17 at 09:39
  • 2
    Paste the result of `dumpbin /exports ApplyTool.dll | grep ApplySettings` into the question. – Michael Burr Oct 23 '17 at 14:24
  • Function name was different.. This is like ?ApplySettings@@YAJV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z. – CrazyCoder Oct 24 '17 at 08:52
  • All that craziness (`#ifdef APPLYDLL_EXPORTS` and so on) is useless if you end up using dynamic loading. Just export via a definition file (extension .def) – manuell Nov 24 '17 at 09:25

1 Answers1

0

I Agree with CrazyCoder, check the DLL using Dependency Walker with that function name.

and try to change define as like below,

#define APPLYDLL_API __declspec(dllexport)

to

#define APPLYDLL_API extern "C" __declspec( dllexport )

Thanks, Jake.

Jodocus
  • 7,493
  • 1
  • 29
  • 45
Jake
  • 129
  • 10