BadImageFormatException when trying to call dll from C#

0

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 ?

c#
c++
.net
opencv
dll
asked on Stack Overflow Sep 5, 2016 by formatkaka

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0