dumpbin发现没有入口函数_Dumpbin显示奇怪的方法名称(在MS Visual C ++中生成导出函数)...

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值