C# DllImport MFC Extension DLL & Name Mangling

2019-07-11 18:34发布

问题:

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();
}

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

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.

回答1:

Having this declaration in the header:

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

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

Another alternative is to use a module definition file:

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

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

And finally import them:

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

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


回答2:

An MFC extension DLL expect a CWinApp object in the caller. You don't have one in C#. Wrap the DLL using an MFC regular DLL, which has a CWinApp object.