无法加载在.dll中声明的函数 [英] Load failed of function declared inside a .dll

查看:232
本文介绍了无法加载在.dll中声明的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.dll似乎已正确加载,但无法引用该函数
这是.dll标头:

It appears the .dll gets loaded correctly but it cannot reference to the function
Here''s the .dll header :

#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:



.cpp :

#include "Header.h"
#include <stdexcept>

using namespace std;

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




最后是引用dll的控制台应用程序




And finally a console application that reference to the dll

#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

推荐答案

导出的函数不应位于名称空间或类中,而应位于全局函数中.注意导出是裸"的.使用工具Dependency Walker也可以查看您的dll正在导出什么.
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.


您将永远找不到未修饰的名称Add,因为您的DLL是C ++,因此该名称将按照 https://msdn.microsoft.com/en-us/library/56h2zst2.aspx [ ^ ].如果希望在应用程序中引用它,则需要使用完全修饰的名称,或者使用namspace和class前缀,并让链接器在构建时找到它.另外,您可以使用普通函数(不在类或名称空间中)并将标头定义包含在C块中,从而:
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:
extern "C"
{
    // C-style function definitions here
}


我找到了一个使用C样式函数定义的工作教程,我将把它留给遇到相同问题后刚好阅读此问题的任何人:
http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855/DLL-Tutorial-For-Beginners.htm [
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


这篇关于无法加载在.dll中声明的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆