2

I am looking for some information on how to achieve something with libraries in C++ and c#. What I would like to know is how to approach the following problem:

  • C# application: -has a window
  • C++ library: -has a function called create_button(x,y), when
    invoked, it will create a button on
    the c# application's window. (if the C# application is not running,
    nothing will happen)
  • C++ application: -dynamicaly links to the C++ library and calls the create_button() function.

How would I approach this problem, I would be glad to hear some of your ideas. The platform is windows. My question is, how would I let the C++ library communicate to the c# application to create a new button? Is it linked, sockets, ... I'm particulary thinking of GTK+ in linux, you link to the gtk+ library, but how does the library interface with GNOME to create a new window etc, something like that. I'm not interested in writing dlls and linking those to a c# application, I'm interested in creating an in-between library.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • So your windows form which is c# runs as a different process? – dexter Nov 30 '10 at 20:32
  • Does the C# application know this is going to happen and can you place code in it to help or are you intending to do this to arbitrary c# applications? – Len Holgate Nov 30 '10 at 20:44
  • @max: indeed the c# application is an application maintained by myself. @Len: yes, since I will be creating the c# application – Nick Ortego Nov 30 '10 at 20:50

4 Answers4

2

I can't think of any sane way to do what you want to do. What I believe you should be doing is creating functions to do the drawing in the C# app and then exposing some messaging interface, such as a socket, that allows external apps to send messages that command the C# app to do what you tell it. When the C# app receives messages of with message type DRAW_BUTTON, it draws the button, with whatever parameters were specified in the message it received.

frankc
  • 11,290
  • 4
  • 32
  • 49
  • Hi frankc, indeed i was thinking about doing smething like this. You suggest I utilise the messaging system in windows? – Nick Ortego Nov 30 '10 at 20:53
0

You're going to run into problems here since C# is managed and C++ is native. As far as I know, the only way of calling native code from managed in the CLI is by using the P/Invoke layer, in which case you would need to import a DLL, write a prototype, etc.

In addition, I believe that the P/Invoke calls are to C functions, and not C++, although you can get past this by adding a C library to call into which in turn calls your C++ library.

  • Indeed, I would do this by exposing a C library, no problem there. But this is not where my problem lies, I want to create a library for my c++ applications that will actually communicate with my single instance c# app. Now as suggested by frankc below, I think I should use windows messages for that. – Nick Ortego Nov 30 '10 at 20:54
  • P/Invoke is not the only interop available. There's IJW (C++/CLI interop), take a look at http://stackoverflow.com/questions/2211867/how-do-i-call-native-c-from-c . As for inter-process communication, you can use named pipes, sockets, COM, or shared memory. – Tamas Demjen Nov 30 '10 at 21:31
0

If you can create a small but proper HWND in C# (don't know the widget), you can use that and create a c++ window using the C#'s window as parent.

We did exactly this a few years ago for a java/c++ process pair. However, the Java app could report the HWND value to the c++ app over RPC, so it wasn't that hard to setup.

Macke
  • 24,812
  • 7
  • 82
  • 118
0

Given that you control the code of the C# application and you control the code of the C++ application and, presumably, the 'in between library'. I find myself asking "why are you doing it like this?"

If you want the C++ app to be able to cause the addition of a given button on the C# app and for the press of that button to be able to communicate with the C++ app then, personally, I'd use standard IPC and have a communications channel between the C++ app and the C# app and simply have the C++ app ask the C# app, via IPC, to display the button and then have the C# app send whatever detail (most probably the fact that the button was pressed) to the C++ app via the same IPC channel.

If this route wont work then I think you need to clarify the problem that you're trying to solve as at present I think your current 'solution' is on the wrong track and so an answer telling you how to achieve your current 'solution' would be misguided.

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
  • Hi Len, my goal is as follows: I want to provide a library for programmer to utilize a special interface, and don't want to expose ipc with my application (not for security reasons, or any other for that, but tto provide a transparent layer to the programmer). After reading a lot of great reactions, I think my approach will be: Create a C library that exposes a few functions and the library itself will do the IPC to the c# application. Do you have any tips on the IPC method to use? I was thinking about either MemoryMapped Files or Sockets. Thank you for your answer! – Nick Ortego Dec 01 '10 at 21:37
  • All this has been done before... Why not simply expose the functionality that you need via COM from the C# app? – Len Holgate Dec 02 '10 at 09:30