Dumpbin显示奇怪的方法名称(在MS Visual C ++中生成导出函数) [英] Dumpbin shows strange method name (generating exporting function in MS Visual C++)

查看:509
本文介绍了Dumpbin显示奇怪的方法名称(在MS Visual C ++中生成导出函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的VS中创建了新的Win32项目,并为此目的选择了动态库(* .dll)。



我已经定义了一些主要的导出功能文件:

  __ declspec(dllexport)
int TestCall(void)
{
int value = 4/2;
std :: cout<<< typeid(value).name()<<<的std :: ENDL;
返回值;
}

__declspec(dllexport)
void SwapMe(int * first,int * second)
{
int tmp = * first;
* first = * second;
* second = tmp;
}

当我查看dumpin / exports后,我有:

  ordinal提示RVA名称

1 0 00001010?SwapMe @@ YAXPEAH0 @ Z
2 1 00001270?TestCall @@ YAHXZ

我正在调用C#版本,如下所示: p>

  [DllImport(@lib1.dll,EntryPoint =?TestCall @@ YAHXZ,
CallingConvention = CallingConvention。 Cdecl)]
static extern int TestCall();

这不是使用导出方法的正确形式。在C ++ dll项目中生成这些导出方法的名称在哪里失败?

解决方案

这是正常的,C ++编译器将名称装饰应用于功能。 C ++语言支持函数重载,很像C#。所以你可以写一个 Foo(int)和一个 Foo(double)函数。显然,它们都不能被导出为一个名为Foo的函数,客户端代码不会知道要调用哪一个。所以额外的字符编码名称,以便它是唯一的重载。



你可以通过声明函数 externC ,C语言不支持重载,所以不需要相同的装饰。



但实际上,如果你T。因为它也是捕捉错误的好方法。像更改C ++代码中的函数声明,但忘记修改C#代码中的pinvoke声明。现在,您可以轻松诊断未发现Entrypoint异常,而不是非描述性且非常难以诊断AccessViolationException。哪些不一定要在C ++代码中引发,堆栈不平衡也可能会使您的C#代码崩溃。查找装饰的名称有点痛苦,但是通过要求链接器创建一个映射文件(/ MAP选项)来改善这一点。


I have created new Win32 project in my VS and have selected Dynamic Library ( *.dll ) for this aim.

I have defined some exporting function in the main file:

__declspec(dllexport)
int TestCall(void)
{
    int value = 4 / 2;
    std::cout << typeid(value).name() << std::endl;
    return value;
}

__declspec(dllexport)
void SwapMe(int *first, int *second)
{
    int tmp = *first;
    *first = *second;
    *second = tmp;
}

When I've looked at the dumpin /exports, I've got:

ordinal hint RVA      name

      1    0 00001010 ?SwapMe@@YAXPEAH0@Z
      2    1 00001270 ?TestCall@@YAHXZ

I'm calling in the C# version like this:

[DllImport(@"lib1.dll", EntryPoint = "?TestCall@@YAHXZ",
CallingConvention = CallingConvention.Cdecl)]
static extern int TestCall();

It's not the correct form of using exported methods. Where did I fail with generating such names for exporting-methods in the C++ dll project?

解决方案

This is normal, the C++ compiler applies name decoration to functions. The C++ language supports function overloading, much like C# does. So you could write a Foo(int) and a Foo(double) function. Clearly they cannot both be exported as a function named "Foo", the client code wouldn't know which one to call. So the extra characters encode the name so that it is unique for the overload.

You can turn that off by declaring the function extern "C", the C language doesn't support overloading so doesn't require the same kind of decoration.

But it is actually better if you don't. Because it is also an excellent way to catch mistakes. Like changing the function declaration in your C++ code but forgetting to modify the pinvoke declaration in your C# code. You will now get an easy to diagnose "Entrypoint not found" exception instead of a non-descriptive and very hard to diagnose AccessViolationException. Which doesn't necessarily have to be raised in the C++ code, the stack imbalance can also crash your C# code. Looking up the decorated name is a bit painful however, improve that by asking the linker to create a map file (/MAP option).

这篇关于Dumpbin显示奇怪的方法名称(在MS Visual C ++中生成导出函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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