2

What would be the best way to pass information from a windows forms C# app to a MFC C++ app while they are running? I don't need to send much, just a small string.

Thanks, Jeff

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Yttrium
  • 2,057
  • 7
  • 25
  • 28

4 Answers4

4

Going down the list of IPC options:

  • Memory mapped files. Easy in C++, tough in C# without pointers, awkward handshaking
  • WM_COPYDATA. Easy in both, tricky to find the window handle you'll need
  • Clipboard. Easy in both, very awkward handshaking
  • COM. Out-of-proc is a beast, forget about it
  • Mailslots. Not suitable for one-to-one communication
  • Pipes. Easy in .NET 3.5, do-able in C++ but a bit tricky to get right
  • Sockets. Easy in both, hard to pass up.
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

Use named pipes, see this posting.

Community
  • 1
  • 1
James
  • 12,636
  • 12
  • 67
  • 104
0

If the C++ app has a main window, take a look at using the SendMessage function (via P/Invoke) in the C# app to send a WM_COPYDATA message to the C++ app.

http://msdn.microsoft.com/en-us/library/ms649011%28VS.85%29.aspx

TheSmurf
  • 15,337
  • 3
  • 40
  • 48
0

MailSlot api is small and simple but requires P/Invoke to use from c# and can go outside your local machine so needs care. see my answer here

Named pipes may well be more robust but this is an alternate.

Community
  • 1
  • 1
ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101