5

I have been checking on the site for a way to transfer data from a process to another one in C++. I found the method SendMessage() but it does not seem to be able to take a byte array.

To explain a little bit the context here, I have an application that send data to another one. We have several objects with different IDs. The app that receives creates a tab for every different objects. If the app that is receiving the data is closed, we start a new process and show the data to the user. If we send a second time, we need to check with IDs to see if we already have one of the objects, if yes replace it. Otherwise add new tabs for the new objects.

We use protocol buffers from google and they work with byte arrays for transportation and serialization, so that's why I need to find a way to send a byte array from a process to another.

I was able to get the HWND of the process but I don't know where to go from now.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • added windows tag, because you mentioned you are using a HWND (this is a plattform-specific problem) – Philipp Nov 19 '12 at 14:12
  • You don't want to use windows messages for this. You can use named pipes, local sockets, a file, shared memory, or pretty much any other communications mechanism you like. See [this article](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx) for some more options. (If you really want to use windows messages [here's how](http://msdn.microsoft.com/en-us/library/windows/desktop/ms649009%28v=vs.85%29.aspx).) – David Schwartz Nov 19 '12 at 14:13
  • You might consider using socket IO. For instance, with Berkeley sockets in *nix (I think Windows implements a variant of these?). – John Doucette Nov 19 '12 at 14:15

2 Answers2

3

Interprocess communication is a plattform-specific thing. In Windows, there are many ways to do that. This MSDN article describes several methods and their pros, cons and use-cases:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

When you control both applications, and support for 3rd party applications is no concern for you, then sending the WM_COPYDATA message through SendMessage() to the other process might be a good approach.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • IPC can be relatively crossplatform if you use sockets. – Robert Mason Nov 19 '12 at 14:16
  • 1
    Also, a SO question that gives an example of how to use WM_COPYDATA: http://stackoverflow.com/questions/2451103/use-wm-copydata-to-send-data-between-processes – Robert Mason Nov 19 '12 at 14:18
  • Thanks guys for the lightning fast replies, that answers my question. – Jean-François Deschênes Nov 19 '12 at 14:24
  • 1
    @Robert Mason: Abusing the TCP/IP stack for inter-process communication inside the host has always seemed pretty hackish to me. There are the security implications you have to keep in mind - you don't want anyone on the network to send data to your application. There are also some internet security suits which will notify the user when an application tries to open a server socket - have fun with people claiming on your support forum that ZoneAlarm has identified your product as a trojan. Also, sockets aren't cross-plattform at all on C++, unless you use a 3rd party library like boost.asio. – Philipp Nov 19 '12 at 15:41
  • @Philipp: On unix the AF_UNIX address family exists expressly for this purpose. Also, all you need to make sockets cross-platform is a small header file that will handle all the platform-dependent headers and typedef int SOCKET. Then, you just need to provide some stub initialization functions for windows. Maybe 20 lines of code. boost.asio is great too. And if you ever want to scale your application across a cluster, this process is *much* easier if you use separate processes and sockets instead of threads and local IPC mechanisms. Though that is a holy war for another day. – Robert Mason Nov 19 '12 at 21:30
2

If you want to use SendMessage you can use the WM_COPYDATA message to send a block of data although it's just a block of bytes, not an object.

jcoder
  • 29,554
  • 19
  • 87
  • 130