Ask Your Question
0

mingw and c# dll

asked 2013-07-31 11:01:04 -0500

fred gravatar image

Hi, I would like to know if a dll can be created using mingw/opencv and accessed from c#? A nice example would be much appreciated. If possible to do so, will there be compatibility issues due to different compilers used. If not possible to do so, what would be the best way to go about communicating between mingw/opencv and c#?

I am thinking of communicating between c# and mingw/opencv by using the file system. But going this route seems rather clunky, inefficient, slow and prone to race condition if not protected by semaphores or by other means. Here's the process: Basically, the c# application will save too images to hard drive. Once the two image files are created, mingw/opencv will take over and process the images and spit out the answer to another harddrive file with the c# app taking over once again and displaying the answer on a gui screen. Using a ramdisk will make the process more efficient.

The above method is convoluted but I cannot think of any other way given the circumstances of unable to create a dll to communicate between c# and mingw/opencv.

edit retag flag offensive close merge delete

Comments

bit late today to hack a demo, but it's definitely possible (and less clunky than your file based approach) !

berak gravatar imageberak ( 2013-07-31 13:21:52 -0500 )edit

Please show me the way...thanks in advance.

fred gravatar imagefred ( 2013-07-31 22:07:24 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-08-01 05:12:00 -0500

berak gravatar image

updated 2013-08-01 05:14:52 -0500

shame , i deleted my mingw-opencv dlls, so i can't test on the real thing, but the general aproach would be like this:

1

say, you got a my.cpp with simple functions, like

const char * myfunc() {
    return "helo world";
}
void otherfunc( double z, int o ) {
}

make a dll from the c++ code:

gcc my.cpp -shared -o my.dll

ofc, link to the opencv libs as well

2

in your c# code you need the interop services, and dllimport any func from the dll you use:

using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("my.dll", CharSet = CharSet.Ansi)]
        private extern static string myfunc();
        [DllImport("my.dll", CharSet = CharSet.Ansi)]
        private extern static void otherfunc(double z,int o);

it has to find the dll as well, so it must be on the PATH or with your c# app.

then you can just call them like any other c# function

MessageBox.Show(myfunc());

3

caveats and downsides ;)

  • stick to simple functions and most 'atomic' types. marshalling classes isn't for the weak of heart (like me)
  • if you actually return pointers ( like, e.g pixels ) make sure, you don't return temporary objects, that get invalidated when leaving the function's scope. that might even require global vars or such.
  • you're dealing with inter-process communication here, you'll probably need CRITICALSECTION in the c++ code, and lock {} on the other side
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-07-31 11:01:04 -0500

Seen: 937 times

Last updated: Aug 01 '13