尝试将 C++ dll 导入 C# 时异常 System.BadImageFormatException
Bizarre System.BadImageFormatException when trying to import C++ dll into C#
这是相关的 C# 位
KrautVK.cs
internal static class KrautVK{
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "init")]
internal static extern int Init(int width, int height, string title, bool fullscreen);
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "windowShouldClose")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool WindowShouldClose();
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "pollEvents")]
internal static extern void PollEvents();
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "terminate")]
internal static extern void Terminate();
}
这是(相关的)C++ 代码:
KrautVK.h
#ifndef KRAUTVK_H_
#define KRAUTVK_H_
#include <cstdio>
#include <vector>
#include <iostream>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define EXPORT extern "C" __declspec(dllexport)
...
EXPORT int init(int w, int h, char *title, int f);
EXPORT int windowShouldClose();
EXPORT void pollEvents();
EXPORT void terminate();
我非常清楚如果构建格式不匹配(即从 64 位应用程序调用 32 位 dll),DllImport 可能会抛出 System.BadImageFormatException
。然而,这种情况并非如此。两者的构建和目标相同 CPU.
在进行一些故障排除后,我发现它完全是由 iostream
和 vector
包含引起的。通过删除这些包含,错误消失并且调用工作。事实上,在我开始实现需要这些包含的代码之前,我没有遇到任何问题。但是,我需要那些包括在内,一天的大部分研究都没有找到关于这种奇怪行为的文档或解释,实际上许多例子都使用 iostream
.
如果相关的话,我同时使用 Jetbrains Rider 和 Clion。
使用 Dependency Walker,我缺少 3 个 dll:
- libstdc++-6.dll
- libgcc_s_seh-1.dll
- 和 libwinpthread-1.dll
如果您使用 MingW 环境,它们是必需的运行时库。将它们放到可执行文件夹中就可以了。它们可以在 {Your MingW/MingW64 installation folder}\bin
中找到
这是相关的 C# 位
KrautVK.cs
internal static class KrautVK{
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "init")]
internal static extern int Init(int width, int height, string title, bool fullscreen);
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "windowShouldClose")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool WindowShouldClose();
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "pollEvents")]
internal static extern void PollEvents();
[DllImport("lib\krautvk", CallingConvention = CallingConvention.Cdecl, EntryPoint = "terminate")]
internal static extern void Terminate();
}
这是(相关的)C++ 代码:
KrautVK.h
#ifndef KRAUTVK_H_
#define KRAUTVK_H_
#include <cstdio>
#include <vector>
#include <iostream>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define EXPORT extern "C" __declspec(dllexport)
...
EXPORT int init(int w, int h, char *title, int f);
EXPORT int windowShouldClose();
EXPORT void pollEvents();
EXPORT void terminate();
我非常清楚如果构建格式不匹配(即从 64 位应用程序调用 32 位 dll),DllImport 可能会抛出 System.BadImageFormatException
。然而,这种情况并非如此。两者的构建和目标相同 CPU.
在进行一些故障排除后,我发现它完全是由 iostream
和 vector
包含引起的。通过删除这些包含,错误消失并且调用工作。事实上,在我开始实现需要这些包含的代码之前,我没有遇到任何问题。但是,我需要那些包括在内,一天的大部分研究都没有找到关于这种奇怪行为的文档或解释,实际上许多例子都使用 iostream
.
如果相关的话,我同时使用 Jetbrains Rider 和 Clion。
使用 Dependency Walker,我缺少 3 个 dll:
- libstdc++-6.dll
- libgcc_s_seh-1.dll
- 和 libwinpthread-1.dll
如果您使用 MingW 环境,它们是必需的运行时库。将它们放到可执行文件夹中就可以了。它们可以在 {Your MingW/MingW64 installation folder}\bin