[Mono-list] How to send a message from C++ (native) code to Mono on Linux (using Xlib)

Chris Howie cdhowie at gmail.com
Sun Nov 30 12:43:04 EST 2008


On Sat, Nov 29, 2008 at 6:22 PM, kemo Kemic <k.kemic at yahoo.com> wrote:
>  I am using .NET for user interface development. C++ is used for numerical
> computation. In some cases it required to send some notifications from C++
> to managed .NET code. On windows it is clear how to create it (and it is
> implemented: PostMessage). Could someone, please,  help  me how to solve
> this issue on Linux? I need the C++ part and also how  to catch the message
> on .NET side. I am also wondering if it is  possible to make .NET code once,
> without recompiling on Linux  (basically I will have two different C++
> binaries and one .NET for all  platforms).

The C# code can be written to compile once and run everywhere.
(Windows-compiled binaries run on Mono, and Mono-compiled binaries run
on MS.NET.)

You will have to compile the unmanaged library for both platforms.
You can use P/Invoke to make calls to this library, just make sure you
declare the functions you intend to call from managed land as extern
"C".  For example, you might do this in your library:

extern "C" {
    int some_function(const char *some_string);
}

Then in your C# library:

internal class NativeCall
{
    [DllImport("gluelibrary")]
    internal static extern int some_function(string some_string);
}

Just make sure that your library compiles as libgluelibrary.so on
Linux and gluelibrary.dll on Windows.  (Of course you should probably
call it something else, but the same basic concept applies.)

Does this answer your question?

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers


More information about the Mono-list mailing list