[英]C# DllImport library written on C++
I'm pretty new in C#. 我在C#中很新。 I have Dll written on C++ by myself and I'm going to use functions from that dll in C# app. 我自己用C ++编写了Dll,我将在C#应用程序中使用该dll中的函数。
So, I do the following when declare functions in C++ project: 因此,在C ++项目中声明函数时,我将执行以下操作:
public static __declspec(dllexport) int captureCamera(int& captureId);
Then I'm trying to import this method in C# app: 然后,我尝试在C#应用程序中导入此方法:
[DllImport("MyLib.dll")]
public static extern int captureCamera(ref int captureId);
But I have the exception: 但我有一个例外:
Unable to find an entry point named 'captureCamera' in DLL 'MyLib.dll'.
The task is to do dllimport without specifying EntryPoint parameter. 任务是在不指定EntryPoint参数的情况下执行dllimport。 Could anyone help me? 有人可以帮我吗?
public static __declspec(dllexport) int captureCamera(int& captureId);
is that method? 那是方法吗? If it is function, it cannot be static, as static
and dllexport
are mutually exclusive. 如果它是函数,则它不能是静态的,因为static
和dllexport
是互斥的。
And the name is mangled. 和名字被改头换面。 See http://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B_Name_Mangling . 请参阅http://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B_Name_Mangling 。 If you can get the mangled name, and then provide the DllImport
with it ( EntryPoint=MANGLED_NAME
), it should work. 如果您可以获取错误的名称,然后为其提供DllImport
( EntryPoint=MANGLED_NAME
),则它应该可以工作。
You can provide linker with .def
file, containing definition of exported functions, and their names aren't mangled then: 您可以为链接器提供.def
文件,其中包含导出函数的定义,然后它们的名称就不会被篡改:
Project.def: Project.def:
EXPORTS
captureCamera @1
Are you declaring 你在申报吗
extern "C" {
__declspec(dllexport) int captureCamera(int& captureId);
}
inside your c++ code - C# can only access C, not C++ functions. 在您的C ++代码中-C#仅能访问C,而不能访问C ++函数。
You are defining a C++ function without an extern "C" block. 您正在定义不带外部“ C”块的C ++函数。 As C++ allows you to overload functions (ie create many captureCamera() functions with different sets of arguments), the actual function name inside the DLL will be different. 由于C ++允许您重载函数(即使用不同的参数集创建许多captureCamera()函数),因此DLL中的实际函数名称将有所不同。 You can check it by opening Visual Studio Command prompt, going to your binary directory and running this: 您可以通过打开Visual Studio命令提示符,转到二进制目录并运行以下命令进行检查:
dumpbin /exports YourDll.dll
You will get something like this: 您将获得如下内容:
Dump of file debug\dll1.dll
File Type: DLL
Section contains the following exports for dll1.dll
00000000 characteristics
4FE8581B time date stamp Mon Jun 25 14:22:51 2012
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00011087 ?captureCamera@@YAHAAH@Z = @ILT+130(?captureCamera@@YAHAAH@Z)
Summary
1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
4000 .text
10000 .textbss
The ?captureCamera@@YAHAAH@Z is the mangled name that actually encodes the arguments you've specified. ?captureCamera @@ YAHAAH @ Z是错误的名称,实际上对您指定的参数进行编码。
As mentioned in other answers, simply add extern "C" to your declaration: 如其他答案所述,只需将extern“ C”添加到声明中:
extern "C" __declspec(dllexport) int captureCamera(int& captureId)
{
...
}
You can recheck that the name is correct by rerunning dumpbin: 您可以通过重新运行dumpbin来重新检查名称是否正确:
Dump of file debug\dll1.dll
File Type: DLL
Section contains the following exports for dll1.dll
00000000 characteristics
4FE858FC time date stamp Mon Jun 25 14:26:36 2012
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 000110B4 captureCamera = @ILT+175(_captureCamera)
Summary
1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
4000 .text
10000 .textbss
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.