I am trying to build a simple DLL in C++ and read back an integer value in C# program. However, I am getting the following error in C#
Exception thrown: 'System.BadImageFormatException' in test_bed.dll
An unhandled exception of type 'System.BadImageFormatException' occurred in test_bed.dll
An attempt was made to load a program with an incorrect format. (0x8007000B)
These are my code:
C++ DLL:
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
typedef void(__stdcall* PFN_MYCALLBACK)();
extern "C" __declspec(dllexport) int __stdcall MyUnmanagedApi(PFN_MYCALLBACK callback)
{
return 1;
}
extern "C" __declspec(dllexport) int __stdcall test_return_int()
{
return 11;
}
C# test bed:
using System;
using System.Runtime.InteropServices;
namespace test_bed
{
class Program
{
public delegate void MyCallback();
[DllImport("my_dll1.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void MyUnmanagedApi(MyCallback callback);
[DllImport("my_dll1.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int test_return_int();
static void Main(string[] args)
{
Console.WriteLine("Hello World! " + test_return_int());
/*
MyUnmanagedApi(
delegate ()
{
Console.WriteLine("Called back by unmanaged side");
}
);*/
}
}
}
Here are some questions you may have.
What have I done so far?
I ensured that that both the C++ dll and the C# test bed were built on the 64 bit platform as was the recommendation of a majority of posts on the internet. However, I am concerned the that my C# test bed is built on the dotnet core 3.1 framework.
C# test bed project settings:
C++ Project Settings:
What is my goal?
I want to have a duplex communication between my C++ dll and C# test bed, so that my C# program could track the progress of a time consuming task in C++, any advise on that could be greatly beneficial :)
User contributions licensed under CC BY-SA 3.0