Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to send a string to another application? (Win API)

Tags:

c

winapi

api

ipc

I have two Win32 API programs written in plain C: Program A and program B. I want program B to send the string "Hello World" to program A. So B needs to do the following:

1) Detect if program A is running.

2) If it is, send "Hello World" string to A.

3) If A is not running, B should show an error message.

Can someone point me to the API functions necessary to do this? How would I establish such a communication between two programs? I think the biggest problem is that the "Hello World" string is in the address space of program B and it somehow needs to move to the address space of A. So just passing a memory pointer is not sufficient... I need a different approach. Any ideas?

Thanks

like image 204
andy Avatar asked Jun 13 '11 10:06

andy


2 Answers

To find the other program's main window I recommend using FindWindow or EnumWindows. Which one you use depends on just what you know about the other process.

Once you have that the easiest way to send text data is via a WM_COPYDATA windows message.

To show an error message, use MessageBox.

like image 145
David Heffernan Avatar answered Nov 15 '22 00:11

David Heffernan


Google for WM_COPYDATA. Failing that, you could use a UDP message over the local IP stack or a pipe.

like image 29
Martin James Avatar answered Nov 15 '22 00:11

Martin James