I tried to load C++ dll dynamically, first I loaded the dll using "LoadLibrary" function and it is getting its handle correctly. After that I tried to get the function pointer of DLL file function using "GetProcAddress", it is returning NULL. Please find my DLL code and testing application code and let me know where is going wrong in the code.
dummy2.h
namespace newer
{
class dllclass
{
public:
static __declspec(dllexport) int run(int a,int b);
};
}
dummy2.cpp
#include <iostream>
using namespace std;
#include "dummy2.h"
namespace newer
{
int dllclass::run(int a,int b)
{
return a+b;
}
}
dummy1.cpp
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
typedef int (*Addition)(int,int);
int _tmain(int argc, _TCHAR* argv[])
{
Addition add;
HINSTANCE hDLL;
hDLL = LoadLibrary(TEXT("Dummy2.dll"));
add = (Addition)GetProcAddress(hDLL, "run");
getchar();
return 0;
}
Please refer above code and guide me.
It's because the name is mangled (i.e. the name of the function is not "run" but something different).
Your code will work with (for MSVC 2013 where I tested):
In general, if you want to load a class via plugin, your best shot is to use virtual interface. An example: