Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone,

I am stdying on running c++ codes with a c# interace. I have tried something and now a can return for example "52" to c# interface and write "52 "to a textbox. My problem occurs when i want to show an image that read by opencv library functions.

That is the NativeLib.c for printing something and showing image on a window
C++
#include "stdafx.h"
#include "NativeLib.h"
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include <string.h>
using namespace cv;
using namespace std;

MYAPI int print_line(const char* str) {
	return 52;
}
MYAPI void ShowImage()
{
    Mat image;
    image = imread("aile.jpg", CV_LOAD_IMAGE_COLOR); 
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );   
}


That is NativeLib.h
#ifndef _NATIVELIB_H_
#define _NATIVELIB_H_

#ifndef MYAPI
#define MYAPI
#endif

#ifdef __cplusplus
extern "C"{
#endif


MYAPI int print_line(const char* str);
MYAPI void ShowImage();

#ifdef __cplusplus
}
#endif

#endif // _NATIVELIB_H_

That is c# interface
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace InterfaceForServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("bitirme2.dll", EntryPoint = "print_line")]
        private static extern int print_line(string str);

        [DllImport("bitirme2.dll", EntryPoint = "ShowImage")]
        public static extern void ShowImage();

        public void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = print_line("Hello, PInvoke!").ToString();
            ShowImage();
        }
    }
}

i can see "52" in the textBox, but while ShowImage is running it gives

An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in InterfaceForServer.exe

Additional information: External component has thrown an exception.

I have googled but everyone says there could be and error about dll. But as i told above print function is running well so i am not sure the problem is at dll. And also dll is generated after every rebuilt at the output directory, i determined.

The problem is occured in line

imshow( "Display window", image );

If there is nothing clear please post

Thanks
Posted
Updated 1-Dec-14 5:30am
v3
Comments
BillWoodruff 30-Nov-14 15:43pm    
Isn't there a .NET wrapper for OpenCV that you can use ?
denegabze 30-Nov-14 16:22pm    
I am not sure but i have develop lots of functions on c++, i want to make an interface for those functions. It will take much more time to change all codes to .Net
KarstenK 1-Dec-14 11:40am    
your pic should have a greater scope than the function ShowImage :-O
[no name] 1-Dec-14 19:18pm    
Why bother? http://www.emgu.com/wiki/index.php/Main_Page

System.Runtime.InteropServices.SEHException indicates an error in the un-managed (C++) code that isn't being handled.

I haven't used OpenCV in C++, so I am not sure what the problem would be, but if you wrap your function in a try/catch and write the error out to a text file, it should help you better understand what error is occurring when it is hitting imshow().

The most common cause is a problem with imshow is a problem with loading the image, make sure the image is in the correct location.

MYAPI void ShowImage()
{
try{
    Mat image;
    image = imread("aile.jpg", CV_LOAD_IMAGE_COLOR); 
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );   
}
catch(int i)
{
//write out to a log file.
}
}
 
Share this answer
 
Thanks a lot to everyone.
The error was about path that will show on window. I copied the jpg file to "Debug" directory of c# Interface project and, it worked clearly
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900