Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It appears the .dll gets loaded correctly but it cannot reference to the function
Here's the .dll header :
C++
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport) 
#else
#define MATHFUNCSDLL_API __declspec(dllimport) 
#endif

namespace MathFuncs
{
	// This class is exported from the MathFuncsDll.dll
	class MyMathFuncs
	{
	public:
		// Returns a + b
		static MATHFUNCSDLL_API double Add(double a, double b);
	};
}


.cpp :
C++
#include "Header.h"
#include <stdexcept>

using namespace std;

namespace MathFuncs
{
	double MyMathFuncs::Add(double a, double b)
	{
		return a + b;
	}
}



And finally a console application that reference to the dll

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

using namespace std;

typedef double(__stdcall *Add)(double, double);
HINSTANCE hinstDLL;
BOOL fFreeDLL;

int _tmain(int argc, _TCHAR* argv[])
{
	float y;
	
	hinstDLL = LoadLibrary(L"Win32Project2.dll");
	if (hinstDLL != NULL)
	{
		Add func = (Add)GetProcAddress(hinstDLL,
			"Add");
		if (func != NULL) 
			double test = func(5, 3);

		fFreeDLL = FreeLibrary(hinstDLL);
	}

	cin >> y;
	return 0;
}


Thanks in advance
Posted

the exported functions shouldnt be in a namespace or class, but a global function. Take care that the export is "naked". Use the tool Dependency walker too see what your dll is exporting.
 
Share this answer
 
You will never find the undecorated name Add because your DLL is C++ and thus the name will be decorated as described in https://msdn.microsoft.com/en-us/library/56h2zst2.aspx[^]. If you wish to refer to it in your application then you either need to use the fully decorated name, or use the namspace and class prefixes and let the linker find it at build time. Alternatively you can use ordinary functions (not in classes or namespaces) and enclose your header definitions in a C block thus:
C++
extern "C"
{
    // C-style function definitions here
}
 
Share this answer
 
Comments
Member 11287295 28-Jan-15 8:04am    
So I deleted the namespace and called the function with the "MyMathFuncs::Add" string. Obviously I still got nothing out of it and it's unclear to me how am I supposed to convert the undecorated void __stdcall b::c(float){}; function to :
?c@b@@AAGXM@Z
Richard MacCutchan 28-Jan-15 8:17am    
You read the documentation about decorated names in the link I gave you. But, as I suggested, it would be much simpler to link the library into your application, and let the linker figure out how to convert the name.
I found a working tutorial which uses the C-style function definition, i'll leave it for anybody who happens to read this question after having my same problem :
http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855/DLL-Tutorial-For-Beginners.htm[^]

Just keep in mind that in this tutorial there's a little writing error, in the dll's .cpp file, the #define statement must appera BEFORE anything else in that same file.

Thanks for the answers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900