BadImageFormatException when trying to call dll from C# - c#

I am writing a simple dll using opencv library.
The code for the dll is as follows :
Dll header file -
#pragma once
#include "stdafx.h"
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#include "opencv2\opencv.hpp"
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR int Add(int a, int b);
DECLDIR void Function(void);
}
#endif
Dll source file -
#define DLL_EXPORT
#include <iostream>
#include "ConsoleApplication3.h"
#include "opencv2\opencv.hpp"
extern "C"
{
DECLDIR int Add(int a, int b)
{
//cv::Mat c(100, 100, CV_8UC1);
return(a + b);
}
DECLDIR void Function(void)
{
std::cout << "DLL Called!" << std::endl;
}
}
C# script to call the method in the dll -
using System;
using System.Runtime.InteropServices;
class Sample
{
[DllImport ("ConsoleApplication3.dll")]
public static extern void Function();
public static void Main()
{
System.Console.WriteLine("hello world");
Function();
}
}
The problem is :-
Error :
Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at Sample.Function()
at Sample.Main() in c:\Users\supertramp\Documents\Projects\opencv_dll\opencv_dll\filedll.cs:line 13
1) When I generate a 64-bit dll in VS 2015 the error is thrown.
2) When I generate a 32-bit dll , the error is thrown when I uncomment the line in which I define a cv::Mat variable in the Add method.
I am not able to understand where is the problem. I read other posts related to this mention that the error is related to the 32 or 64 bit dll build, but in my case commenting the line where cv::Mat is defined makes the dll run fine.
What might be the issue ?

Related

Use Java DLL in c++ program

I created a simple DLL in Java with JNI . It contains one function the return a string "hello from java dll".
The dll works fine when i use "java " in the cmd.
Now I'm trying to load this DLL in another DLL that I wrote using c++ which already contains 2 working functions.
So I did this:
char* MyFunctions::HelloFromJava() {
HMODULE myDll = LoadLibrary(L"TestJavaDll.dll");
if (myDll != NULL) {
auto fun = (fun_ptr)GetProcAddress(myDll,"HelloFromJava");
if (fun != NULL)
return fun();
else
return "Can't find HelloFromJava";
FreeLibrary(myDll);
}
else {
return "Can't find TestJavaDll.dll";
return "GetLastError()=";
}
}
And in the header:
static __declspec(dllexport) char* HelloFromJava();
And the cpp and header files of the Java dll are:
#include <jni.h>
#include <stdio.h>
#include <windows.h>
#include "TestJavaDll.h"
JNIEXPORT jstring JNICALL
Java_TestJavaDll_HelloFromJava(JNIEnv *env, jobject obj)
{
return env->NewStringUTF("Hello From Java Dll");
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestJavaDll */
#ifndef _Included_TestJavaDll
#define _Included_TestJavaDll
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestJavaDll
* Method: HelloFromJava
* Signature: ()V
*/
JNIEXPORT jstring JNICALL Java_TestJavaDll_HelloFromJava
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
To test the c++ DLL i created a .net application that imports all methods in the c++ DLL. So, in this case, I am doing this:
[DllImport("HELLODLL3", EntryPoint = "?HelloFromJava#MyFunctions#HelloCPPLibrary##SAPADXZ")]
public static extern IntPtr HelloFromJava();
And then i print the message:
Console.WriteLine(Marshal.PtrToStringAnsi(HelloFromJava()));
But i get the error:
Unable to find an entry point named '?' in DLL 'HELLODLL3'
where HELLODLL3 is the name of the c++ DLL.
You do not have the correct mangled name for the DllImport:
?HelloFromJava#MyFunctions#HelloCPPLibrary##SAPADXZ
See here for details of how to get it.

Run a C# function from an unmanaged C++ dll through a managed C++ Wrapper

I am working on a C# project that uses some unmanaged C++ code encapsulated in a dll. I have to load that dll as a Reference (not DllImport), so I'm writing a managed C++ Wrapper as a bridge between C# and the dll.
I can easily call the dll functions from C#, just calling the equivalent functions in the wrapper from C#, and then calling the dll functions from the wrapper. However, there are some callback functions in the dll that should call C# functions, and that part doesn't work. I have studied several tutorials about this kind of interop (stackoverflow, microsoft, codeproject), but they are quite old and I can't make them work.
The idea is that the dll is processing real time data, and everytime the dll generates some results, it should call a function in Program.cs, which uses those results.
The code I'm working on is huge, so I've made a very simple scenario that includes everything that doesn't work.
In Program.cs I'm trying to tell the dll: "take this function Test and call it when you are done". Then, to test it, I force it to run the callback function. But I find several errors.
Although wrapper compiles and generates wrapper.dll, when I load it in the C# project, the function M_setCallback isn't there ¿?.
I wonder if somebody could give me a hand or show me a tutorial that explains how to do that (the tutorials I found don't have the 3 projects I must use, and I can't make them work). I have tried using delegates, but the program crashes.
Or maybe there is a simpler way to do this.
Thanks!
Here is the code, it's very simple:
First, I have the unmanaged dll:
C++ Project, Win32 Console Application, compiled into a dll
defs.h
namespace JJLibrary
{
typedef void (__stdcall *MYCALLBACK) (bool mybool, int myint);
}
unmanaged_dll.h
#ifndef JJAPI_H
#define JJAPI_H
#ifdef UNMANAGED_EXPORTS
#define JJ_API __declspec(dllexport)
#else
#define JJ_API __declspec(dllimport)
#endif
#include "defs.h"
namespace JJLibrary
{
JJ_API void U_helloWorld();
JJ_API void U_setCallback( MYCALLBACK theCallback );
JJ_API void U_runCallback();
}
#endif //JJAPI_H
unmanaged_dll.cpp
#include "unmanaged_dll.h"
#include <iostream>
using namespace std;
namespace JJLibrary
{
MYCALLBACK m_callback = 0;
JJ_API void U_helloWorld()
{
cout << "Unmanaged: Hello world" << endl;
}
JJ_API void U_setCallback( MYCALLBACK theCallback )
{
cout << "Unmanaged: Setting callback" << endl;
m_callback = theCallback;
}
JJ_API void U_runCallback()
{
bool b = true;
int i = 25;
cout << "Unmanaged: Running callback: " << b << " " << i << endl;
if (m_callback)
m_callback(b, i);
// In the final application b and i are generated here after processing data, I give them values for testing
// When they are generated, call the function in C#
}
}
Wrapper:
C++ CLR Class Project
wrapper.h
#include "defs.h"
#include <iostream>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace JJLibrary;
using namespace std;
namespace wrapper {
public ref class Wrapper
{
public:
void M_helloWorld();
void M_setCallback( MYCALLBACK theCallback );
void M_runCallback( bool mybool, int myint );
};
}
wrapper.cpp
#include "stdafx.h"
#include "wrapper.h"
#include "defs.h"
#include <iostream>
using namespace wrapper;
using namespace JJLibrary;
using namespace std;
void Wrapper::M_helloWorld()
{
cout << "Managed: Hello World" << endl;
U_helloWorld();
}
void Wrapper::M_setCallback( MYCALLBACK theCallback )
{
cout << "Managed: Setting callback" << endl;
U_setCallback( theCallback);
}
void Wrapper::M_runCallback(bool mybool, int myint)
{
cout << "Managed: Running callback" << endl;
U_runCallback(mybool, myint);
}
And finally, Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using wrapper;
namespace CS
{
class Program
{
void Test(bool mybool, int myint)
{
Console.WriteLine(!mybool);
Console.WriteLine(2*myint);
}
static void Main(string[] args)
{
Wrapper myWrapper = new Wrapper();
myWrapper.M_helloWorld();
myWrapper.M_setCallback(Test);
myWrapper.M_runCallback(true, 25); // --> false 50
string s = Console.ReadLine(); // Pause
}
}
}
Too much code to really understand, but my solution to a similar problem involved a delegate that looks like this.
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void UserFunctionCallback(IntPtr context, int nvalues,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]IntPtr[] values);
If that helps I can add some more code.

attempted to read or write protected memory - dllimport

I'm having a problem trying to import a c++ dll into c#
I always get the error "attempted to read or write protected memory" when I call the constructor of the dll class. I've been locking in other solutions for the same answer but I couldn't found solution.
I decided to use a simple function to discard that the error came from the c++ part but I'm having the same problem...
Here is my code:
main.cpp:
#include "main.h"
simple_dll::simple_dll(int num) : numero(num) {}
int simple_dll::getNumero() {
return this->numero;
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
// void DLL_EXPORT SomeFunction(const LPCSTR sometext);
class DLL_EXPORT simple_dll {
public:
DLL_EXPORT simple_dll(int num);
DLL_EXPORT int getNumero();
int numero;
};
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
And the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
namespace prueba_dll_VS
{
unsafe class Program
{
[System.Runtime.InteropServices.DllImport("simple_dll.dll", EntryPoint = "_ZN10simple_dllC2Ei")]
private static extern System.IntPtr simple_dll(int num);
[System.Runtime.InteropServices.DllImport("simple_dll.dll", EntryPoint = "_ZN10simple_dll9getNumeroEv")]
private static extern int getNumero(System.IntPtr hObject);
static void Main(string[] args)
{
System.IntPtr ptr_simple_dll = simple_dll(4); //HERE IS WHERE THE ERROR RAISES
int hora = getNumero(ptr_simple_dll);
Console.WriteLine(hora);
Console.ReadLine();
}
}
}
I'm getting mad, this can't be so difficult.
Thanks in advance.
The problem is the calling convention. In the current form, the functions are exported in thiscall, but the DllImport attribute defaults to stdcall (or cdecl on smartphones).

This simple C++ DLL is not working in C#

I have been spending the day working on come c++ code that I need to run in c#. I ran through this DLL tutorial and have been having trouble using it in my c# app. I will post all the code below.
I am getting this PInvokeStackImbalance error: 'A call to PInvoke function 'frmVideo::Add' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.'
Thanks as always,
Kevin
DLLTutorial.h
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR int Add( int a, int b );
DECLDIR void Function( void );
}
#endif
DLLTutorial.cpp
#include <iostream>
#define DLL_EXPORT
#include "DLLTutorial.h"
extern "C"
{
DECLDIR int Add( int a, int b )
{
return( a + b );
}
DECLDIR void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
}
C# code to use the DLL:
using System.Runtime.InteropServices;
[DllImport(#"C:\Users\kpenner\Desktop\DllTutorialProj.dll"]
public static extern int Add(int x, int y);
int x = 5;
int y = 10;
int z = Add(x, y);
You C++ code uses the cdecl calling convention and the C# code defaults to stdcall. This mismatch explains the message that you see.
Make the two sides of the interface match:
[DllImport(#"...", CallingConvention=CallingConvention.Cdecl]
public static extern int Add(int x, int y);
Alternatively you could use stdcall for your C++ exports:
DECLDIR __stdcall int Add( int a, int b );
It's up to you which of these two options you choose, but make sure that you only change one side of the interface and not both, for obvious reasons!

how to call a C++ dll exported function from c#

This is the first time I'm trying to mix c# an unmanaged C++ so this might be a very simple question , but I don't get it.
I need to call some functions from a C++ dll into C# code. Here is the code for the dll project:
the .h file :
#pragma once
#include <iostream>
#if defined FIRSTDLL_EXPORTS
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR int Add( int a, int b );
DECLDIR void Function( void );
}
the .cpp file
#include "stdafx.h"
#include "myFct.h"
#include <iostream>
extern "C"
{
DECLDIR int Add( int a, int b )
{
return( a + b );
}
DECLDIR void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
}
I compiled this for both the debug and releas and copied it in the debug folder of my C# project. Neither version worked.
Here is the c# code:
[DllImport("firstDLL.Dll")]
public static extern int Add(int a, int b);
var cyu = Add(3, 5);
And when I try to run this I get
"Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Program Files\Microsoft Office\Office14\WINWORD.EXE'.
Additional Information: A call to PInvoke function 'MyAddin!MyAddin.ThisAddIn::Add' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."
But as I see the signatures are the same. What am I missing??
Thanks!
The default calling convention for DLLImport is stdcall, but the default of your C++ code is cdecl. The error message you have seen is what is shown when the calling conventions don't match. The parameter stack cleanup requirements are different for these two calling conventions, and the P/Invoke marshaller detects and reports this.
The fix is to make your calling conventions match.
For example you could change your P/Invoke like so:
[DllImport("firstDLL.Dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b);
The other option is to change your C++:
#if defined FIRSTDLL_EXPORTS(returntype)
#define DECLDIR __declspec(dllexport) returntype __stdcall
#else
#define DECLDIR __declspec(dllimport) returntype __stdcall
#endif
Clearly you should only do one of these. If you change both C# and C++ you'll have the same problem in reverse!
If I were you I would leave the C++ code as cdecl and change the C# to match.

Categories

Resources