Here is the relevent C# bits
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();
}
Here is the (relevant) C++ code:
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();
I am very much aware that System.BadImageFormatException can be thrown by DllImport if there's a build format mismatch (ie calling a 32 bit dll from a 64 bit application). However, this is not the case. Both are built and target the same CPU.
After doing some troubleshooting, I found that it's caused solely by the iostream and vector includes. By removing those includes, the error goes away and the calls work. In fact, I didn't have any problems until I started implementing code that needed those includes. However, I need those includes, and the better part of a day's worth of research has turned up no documentation or explanation for this bizarre behavior, and many examples in fact use iostream.
I'm using both Jetbrains Rider and Clion, if that is relevent.