为什么要提到在从其他c ++ dll调用c ++函数时不要破坏名称 [英] why to mention not to mangle the name when calling a c++ function from other c++ dll

查看:86
本文介绍了为什么要提到在从其他c ++ dll调用c ++函数时不要破坏名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在dll1中导出的功能





This is my exported function in dll1


extern"C" __declspec(dllexport) int FUN1(char* p){
return p[0];
}





我从其他项目调用此FUN1。下面是代码





I am calling this FUN1 from other project.Below is the code

#include<windows.h>
#include<iostream>
using namespace std;
typedef int (*MYFUN1)(char*);

int main()
{

	HMODULE hMod = LoadLibrary ("C:\\Users\\admin\\Documents\\Visual Studio 2010\\Projects\\CalledFun\\Debug\\CalledFun.exe");
	if(hMod != NULL)
	{
		MYFUN1 pCtor = (MYFUN1) GetProcAddress (hMod, "FUN1");
		int a = pCtor("calling a value") ;
		cout<<a;
	}
}





如果我从dll1中删除externC,那么该函数地址将返回在dll2中调用GetProcAddress时为NULL。因为两者都是用c ++编写的,我认为名称修改不会影响这个。我想如果我们结合使用c和c ++库那么我们只需要提到externC,任何人都可以帮助我离开这个混乱



提前谢谢



If I remove the extern "C" from dll1 then that function address is returning as NULL when calling GetProcAddress in dll2.Since both are written in c++ i thought name mangling will not effect this.I thought if we use c and c++ libraries combinely then only we need to mention extern "C" ,Can anyone help me to get out of this confusion

Thanks in advance

推荐答案

谢尔盖已经解释了导出/导入所需的全部内容功能。

要准时回答您的问题,请考虑您使用的是GetProcAddress()。此函数在可执行文件(exe或dll)中搜索具有指定名称的函数。它并不关心添加或删除损坏。

因此,如果删除extern C,只需在GetProcaddress()中替换受损的名称,它就会起作用;)。
Sergey already explained all that is required to export/import functions.
To punctually answer your question please consider that you are using GetProcAddress(). This function search, in the executable (exe or dll), for a function having exactly the specified name. It doesn't cares to add or remove mangles.
So if you remove "extern C" simply replace, in GetProcaddress(), the mangled name and it will work ;).

< br>

请参阅: https://msdn.microsoft .com / zh-CN / library / Aa271769%28v = VS.60%29.aspx [ ^ ]。



它表示使用 __ declspec(dllimport) __ declspec(dllexport)使用相同版本的相同编译器在同一函数声明中生成匹配命名。



如果由于某种原因,您需要为导出的函数应用任意命名,或者您必须使用任意命名导入某些函数,则使用DEF文件可以解决此问题。请参阅:

https://msdn.microsoft.com/en-us /library/hyx1zcd3.aspx [ ^ ]。



如您所见,您可以指定两个名称:C ++函数声明中显示的函数名称和替代名称(因为它出现在DLL中而受损(装饰或未装饰)。另请参阅:

https://msdn.microsoft.com/en-us /library/28d6s79h.aspx [ ^ ],

https://msdn.microsoft.com/en -us / library / 54xsd65y.aspx [ ^ ],

https://msdn.microsoft.com /en-us/library/9h658af8.aspx [ ^ ]。







对不起,我想提一下使用 GetProcAddress ,但忘了,忘了这是你实际尝试使用的。当然,您需要使用带有损坏/装饰的名称。我对Frankie-C解决方案2的信任。



-SA
Please see: https://msdn.microsoft.com/en-us/library/Aa271769%28v=VS.60%29.aspx[^].

It says that using __declspec(dllimport) and __declspec(dllexport) with the same version of the same compiler produces matching naming on the same function declaration.

If, by some reason, you need to apply arbitrary naming for exported functions or you have to import some functions with arbitrary naming, this problem is solved using DEF files. Please see:
https://msdn.microsoft.com/en-us/library/hyx1zcd3.aspx[^].

As you can see, you can specify both names: the function name as it is seen in the C++ function declaration, and the alternative name (mangled/decorated or not) as it appears in the DLL. See also:
https://msdn.microsoft.com/en-us/library/28d6s79h.aspx[^],
https://msdn.microsoft.com/en-us/library/54xsd65y.aspx[^],
https://msdn.microsoft.com/en-us/library/9h658af8.aspx[^].



Sorry, I wanted to mention the use of GetProcAddress, but forgot, and forgot that this was what you actually tried to use. Of course, you need to use mangled/decorated name with it. My credit to Solution 2 by Frankie-C.

—SA


这篇关于为什么要提到在从其他c ++ dll调用c ++函数时不要破坏名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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