I'm trying to load an un-managed DLL
written in C++
with C
linkage( that is extern "C"{}
block which exposes some functionalities).
Looking at the DLL
s exports below, I can see the functions are indeed exported properly and should be accessible.
However, trying to use any of such exported functions (e.g. Add (which just adds two numbers here), results in the infamous System.BadImageFormatException
exception :
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
The DLL
is compiled both in x64
and x86
along with the C#
client app, so this is not caused by the mismatched bitness I believe.
The C# Client Code looks like this :
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestDLL_CWraper
{
public partial class frmMain : Form
{
[DllImport(@"Core_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int x, int y);
[DllImport(@"Core_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Initialize(bool showFeed);
[DllImport(@"Core_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Start(bool async);
[DllImport(@"Core_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Stop();
[DllImport(@"Core_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetCpuAffinity(int mask);
public frmMain()
{
InitializeComponent();
}
private void btnRunService_Click(object sender, EventArgs e)
{
MessageBox.Show("test", Add(5, 10).ToString());
//Initialize(false);
//Start(this.chkboxAsync.Checked);
}
}
}
and in case the header file is of any use, here it is :
#ifndef CORE_H
#define CORE_H
/* If we are we on Windows, we want a single define for it.*/
#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#define _WIN32
#endif /* _WIN32 */
#if defined(_WIN32) && defined(_CORE_BUILD_DLL)
/* We are building CORE as a Win32 DLL */
#define CORE_API __declspec(dllexport)
#elif defined(_WIN32) && defined(CORE_DLL)
/* We are calling CORE as a Win32 DLL */
#define CORE_API __declspec(dllimport)
#elif defined(__GNUC__) && defined(_CORE_BUILD_DLL)
/* We are building CORE as a shared / dynamic library */
#define CORE_API __attribute__((visibility("default")))
#else
/* We are building or calling CORE as a static library */
#define CORE_API
#endif
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <stdexcept>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
namespace py = pybind11;
using namespace py::literals;
typedef void(*CallbackFn)(bool, std::string, py::array_t<uint8_t>&);
typedef std::function<void(std::string)> LogFunction;
typedef void * HANDLE;
class CORE_API Core
{
private:
// start the interpreter and keep it alive currently only one instance
// can be created as interpreter is a singleton class.
py::scoped_interpreter guard{};
py::object cls;
py::object obj;
py::object startFunc;
py::object startFuncAsync;
py::object stopFunc;
py::object setCpuAffinityFunc;
public:
Core();
Core(bool showFeed);
void Start(bool async= false);
void Stop(void);
void SetCpuAffinity(int mask);
};
extern "C"
{
CORE_API int Initialize(bool showFeed);
CORE_API void Start(bool async);
CORE_API void Stop(void);
CORE_API void SetCpuAffinity(int mask);
CORE_API void* CreateHandle(bool showFeed);
CORE_API int Add(int x, int y);
}
#endif // !CORE_H
and in Core.cpp:
#include "Core.h"
...
extern "C"
{
CORE_API int Add(int x, int y)
{
return x + y;
}
}
What am I missing here? I tested with both debug and release builds for the DLL and the C# Client.
I also tried placing the needed DLLs on which this DLL depends such as python36.dll
and python3.dll
to no avail. I'm out of ideas what could be the cause of this exception.
User contributions licensed under CC BY-SA 3.0