C#的DllImport MFC扩展DLL和放大器;名称重整 [英] C# DllImport MFC Extension DLL & Name Mangling

查看:345
本文介绍了C#的DllImport MFC扩展DLL和放大器;名称重整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想在C#应用程序使用MFC扩展DLL。我揭露的功能是C函数,也就是说我把它们导出这样

I have a MFC extension DLL which I want to use in a C# application. The functions I'm exposing are C functions, i.e. I'm exporting them like this

extern "C"
{
 __declspec(dllexport) bool Initialize();
}



该功能在内部使用MFC类,所以我有什么做用,使用p C#中的DLL / Invoke的。

The functions internally uses MFC classes, so what do I have to do to use the DLL in C# using P/Invoke.

其次,我想用函数重载,但据我所知,C不支持函数重载,如果我出口C ++函数,他们将被截断。所以,我能做些什么解决这个问题?利用我们可以导入C ++错位功能的的DllImport

Secondly, I want to use function overloading, but as far as I know C doesn't supports function overloading and if I export C++ functions they will be mangled. So what can I do remedy this problem? Can we import C++ mangled functions using DllImport.

推荐答案

有了这个声明在标题:

__declspec(dllexport) int fnunmanaged(void);
__declspec(dllexport) int fnunmanaged(int);

您可以使用的dumpbin.exe 来获取函数的确切名称:

You could use dumpbin.exe to get the exact name of the function:

dumpbin.exe /exports unmanaged.dll

Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file unmanaged.dll

File Type: DLL

  Section contains the following exports for unmanaged.dll

    00000000 characteristics
    4B0546C3 time date stamp Thu Nov 19 14:23:15 2009
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 0001106E ?fnunmanaged@@YAHH@Z = @ILT+105(?fnunmanaged@@YAHH@Z)
          2    1 00011159 ?fnunmanaged@@YAHXZ = @ILT+340(?fnunmanaged@@YAHXZ)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

和声明函数时使用此名称:

And use this name when declaring the function:

[DllImport(@"D:\work\unmanaged.dll",
    EntryPoint = "?fnunmanaged@@YAHH@Z",
    ExactSpelling = true)]
static extern int fnunmanaged();

[DllImport(@"D:\work\unmanaged.dll",
    EntryPoint = "?fnunmanaged@@YAHXZ",
    ExactSpelling = true)]
static extern int fnunmanaged(int a);





另一种方法是使用模块定义文件

LIBRARY "unmanaged"
EXPORTS 
  fn1=?fnunmanaged@@YAHH@Z
  fn2=?fnunmanaged@@YAHXZ

在这种情况下,您不再需要使用 __ declspec(dllexport)的和你的头文件可能是这样的:

In this case you no longer need to use __declspec(dllexport) and your header file might look like this:

int fnunmanaged(void);
int fnunmanaged(int);

和最后导入它们:

[DllImport(@"D:\work\unmanaged.dll")]
static extern int fn1();

[DllImport(@"D:\work\unmanaged.dll")]
static extern int fn2(int a);

这篇关于C#的DllImport MFC扩展DLL和放大器;名称重整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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