message-queue is a simple example about using message queue to send and receive data cross process, in current version I just support windows, I will write a demo for linux if I have free time. This project solve a simple math: we have two processes p1 and p2, p1 will open a file to read math expression in each line then send to p2 to solve this expression. After that, p2 response result to p1, p1 will write the result to another file.
Current projects has been developed on Visual Studio, include three projects are:
- sender: This is process one(p1), open file then read each line(math expression) to send to receiver, wait for result from receiver process to write result to another file.
- receiver: This is a process two(p2), wait for a math expression from sender process. When this process received the expression, solve it and response result to sender process.
- shared: A library project to share some common code and define.
P1 and P2 will using the message queue to receive message from other, to send a message to other we using SendMessage function(in win32).
In win32, a message queue was created by create a Window, then we using WinProc to receive message from other and a message loop to keep window alive. To send message to other we will using SendMessage function with a window handle, especially, you must use WM_COPYDATA and a COPYDATASTRUCT to pass your data from a process to another.
So easy! just download project then open with Visual Studio then rebuild shared project first, after that you just press F5 to run receiver and sender to see result.
Note 1: You must change two define to your own file paths before run sender and receiver
#define INPUT_FILENAME "in.txt" /* input file which contains math expressions */
#define OUTPUT_FILENAME "out.txt" /* output file which will contain results */
Note 2: You have to start receiver before start sender because receiver must be initialized to take a message queue then wait for data from sender, after that sender will auto find receiver's window handle to send message to it.
This is some screenshots from sender, receiver and files:
Sender console
Receiver console
Input and ouput files
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
##Dependencies This project not required the dependencies.
Version 1.0.0 is just a simple message queue for win32
Developed by sontx/noem, some useful information:
- Home: www.code4bugs.com
- My blog: www.sontx.blogspot.com
- Email: xuanson33bk@gmail.com
- Facebook: No Em
Copyright 2016 Tran Xuan Son