2

I have quite novice to DLL programming.

I have created a DLL project as,

In DLL project SimpleH.h

namespace ME{
    class base
    {
    public:
        static __declspec(dllexport) void Hello();
    };
}
__declspec(dllexport) void HelloWorld();

DLL.cpp

#include "stdafx.h"
#include <iostream>
#include "SimpleH.h"
using namespace std;
namespace ME
{
    void base::Hello()
        {
            cout << "Hello World\n";
        }
}
void HelloWorld()
{
    cout << "Hello I am world\n";
}

I have created an .exe.

Main.exe

    #include "stdafx.h"
    #include <stdlib.h>
    #include <Windows.h>
    #include <iostream>

    using namespace std;
    typedef void (*FunctionFunc)();
    typedef void (*FunctionAno)();
    int _tmain(int argc, _TCHAR* argv[])
    {

        FunctionFunc _FunctionFunc;
        FunctionAno _FunctionAno;
        HINSTANCE hInstLibrary = LoadLibrary(L"MyDLL.dll");
        if(hInstLibrary)
        {
            /*Problem Happens here*/
            _FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary, "Hello");
            _FunctionAno = (FunctionFunc)GetProcAddress(hInstLibrary, "HelloWorld");
/**/
            if(_FunctionFunc)
            {
                _FunctionFunc(); 
            }
            if(_FunctionAno)
            {
                _FunctionAno();
            }
          DWORD error = ::GetLastError();
          if (error)
          {
            LPVOID lpMsgBuf;
            LPVOID lpDisplayBuf;

            FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            error,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );

          }
        }
        FreeLibrary(hInstLibrary);
        system("pause");
        return 0;
    }

hInstance is getting updated properly. But the GetProcAddress() are not getting updated. Please help me. Where is it going wrong?

Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • You might want to read about [name mangling in C++](http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B). – Some programmer dude Oct 17 '13 at 09:16
  • possible duplicate of [GetProcAddress cannot find my functions](http://stackoverflow.com/questions/2601908/getprocaddress-cannot-find-my-functions) – librik Oct 17 '13 at 09:51
  • @librik some kind of duplicate, BUT the extern "C" trick won't work for the static member function (or will it?) – manuell Oct 17 '13 at 10:17

2 Answers2

4

Exported functions have "decorated names". Use DUMPBIN to see them.

DUMPBIN /EXPORTS Your.dll

manuell
  • 7,528
  • 5
  • 31
  • 58
2

Your function and method names are mangled according to Visual C++ rules.

If you use DUMPBIN /EXPORTS MyDLL.dll from the command line, you will read some identifier names like:

     1    0 00001000 ?Hello@base@ME@@SAXXZ
     2    1 00001020 ?HelloWorld@@YAXXZ

So, for example, HelloWorld is actually exported as ?HelloWorld@@YAXXZ. So GetProcAddress() can't find the original identifier. Use extern "C" to avoid name mangling on HelloWorld:

extern "C" __declspec(dllexport) void HelloWorld();

More details on proper ways to export C++ classes from DLL can be found in this CodeProject article.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162