C# 接口 C++ DLL?
如何从 C++ DLL 调用函数?
C++ DLL 包含如下函数:
__declspec(dllexport) bool Setup () { return simulation.Setup (); }
C# 程序执行以下操作:
[DllImport("mydll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool Setup();
当 C# 程序尝试加载 (?) DLL 时,它崩溃并显示以下消息:
An unhandled exception of type 'System.BadImageFormatException' occurred in TestFrame.exe
Additional information: There was an attempt to load a file with a wrong format (exception from HRESULT: 0x8007000B)
The C++ DLL is a standard DLL (no MFC, no ATL).
How do I call functions from a C++ DLL?
The C++ DLL contains functions like this:
__declspec(dllexport) bool Setup () { return simulation.Setup (); }
The C# program does this:
[DllImport("mydll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool Setup();
The C# program crashes with the following message when it tries to load (?) the DLL:
An unhandled exception of type 'System.BadImageFormatException' occurred in TestFrame.exe
Additional information: There was an attempt to load a file with a wrong format (exception from HRESULT: 0x8007000B)
The C++ DLL is a standard DLL (no MFC, no ATL).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您尝试将 32 位 DLL 加载到 64 位进程中时,会出现此错误。 (或反之亦然)
在 VS2010 之前,C# 项目默认针对任何 CPU,并且将在 64 位操作系统上以 64 位运行。
您需要转到“项目属性”中的“生成”选项卡,并将 C# 项目设置为仅 x86。
This error occurs when you try to load a 32-bit DLL into a 64-bit process. (Or vice-versa)
Until VS2010, C# projects are target any CPU by default and will run as 64-bit on a 64-bit OS.
You need to go to the Build tab in Project Properties and set the C# project to x86 only.
您的exe和dll是否有可能具有不同的位数(即一个是64,另一个是32)?
Is it possible that your exe and dll have different bitness (i.e. one is 64 and the other 32)?
您是否尝试过为
x86
平台编译代码?有关说明,请参阅此博文。
Have you tried compiling the code for
x86
platform?See this blogpost for instructions.
为程序集设置 CPU 体系结构(平台目标)可能不适合您,但您可以指定备用路径来查找要加载的正确 DLL。
如何使用根据CPU架构纠正非托管DLL文件? (32 / 64 位)
It may not be appropriate for you to set the CPU architecture (platform target) for the assembly, but instead you can specify an alternate path to find the correct DLL to load.
How to use the correct unmanaged DLL file according CPU architecture? (32 / 64 bits)