[英]C# DllImport MFC Extension DLL & Name Mangling
I have a MFC extension DLL which I want to use in a C# application. 我有一个MFC扩展DLL,我想在C#应用程序中使用它。 The functions I'm exposing are C functions, ie I'm exporting them like this
我公开的函数是C函数,即我正在这样导出它们
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. 函数内部使用MFC类,因此我需要做什么才能在C#中使用P / Invoke来使用DLL。
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. 其次,我想使用函数重载,但据我所知,C不支持函数重载,如果我导出C ++函数,它们将被破坏。 So what can I do remedy this problem?
那么我能解决这个问题呢? Can we import C++ mangled functions using DllImport .
我们可以使用DllImport导入C ++受损函数吗?
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来获取函数的确切名称:
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: 在这种情况下,您不再需要使用
__declspec(dllexport)
并且您的头文件可能如下所示:
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);
An MFC extension DLL expect a CWinApp object in the caller. MFC扩展DLL期望调用者中的CWinApp对象。 You don't have one in C#.
你在C#中没有一个。 Wrap the DLL using an MFC regular DLL, which has a CWinApp object.
使用MFC常规DLL包装DLL,该DLL具有CWinApp对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.