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
#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 ); 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
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