![]() |
Interprocess Communication through WM_COPYDATA & Shared Memory
IntroductionHere I shall describe the methods of how data is exchanged between windows of different processes. Communication between windows means one window process send/post a message/data to the other window process. The Windows operating system provides the mechanism for communications and data sharing between applications. SendMessage() and PostMessage() are most commonly used method of communication. Passing Data with WM_COPYDATALet us consider a case when you need to send a complex data structure or a string to another window. In such scenario, you can send information to another application using the WM_COPYDATA message. This method requires cooperation between the sending application and the receiving application. The receiving application must know the format of the information and be able to identify the sender. In this method the receiving application receives the data in a COPYDATASTRUCT structure. The application sends WM_COPYDATA message to the other application using SendMessage () function with the following parameters.Code: Cpp
wParam - Handle to the window passing the data.lParam - Pointer to a COPYDATASTRUCT structure that contains the data to be passed. When you're ready to send some data to a window in another process, you must first initialize the COPYDATASTRUCT structure. COPYDATASTRUCT is a structure defined in WinUser.h, and it looks like this:Code: Cpp
dwData specifies data to be passed to the receiving application.The cbData member specifies the number of bytes that you want to transfer to the other process, and the lpData member points to the first byte of the data.When SendMessage sees that you are sending a WM_COPYDATA message, it creates memory-mapped file cbData bytes in size and copies the data from your address space to the memory-mapped file. It then sends the message to the destination window. When the receiving window procedure processes this message, the lParam parameter points to a COPYDATASTRUCT that exists in the address space of the receiving process. The lpData member of this structure points to the view of the shared memory-mapped file in the receiving process's address space.The following example demonstrates how to send information between two applications using the WM_COPYDATA message.1. Fills the COPYDATA structure. Code: Cpp
Code: Cpp
SendMessage () function.Code:
dhwnd – handle of the destination window.hwnd – handle of the sender window.cData – COPYDATA structure variable.The receiving application receives the data in the following way: 1. Declare the data variable. Code: Cpp
WM_COPYDATA message in the WndProc().Code: Cpp
Communication by FileMapping object (Shared Memory)Processes can communicate between them by a shared memory called File Mapping objects. File mapping is the association of a file's contents with a portion of the virtual address space of a process. The system creates a file-mapping objectto maintain this association. The creation of File Mapping is shown below: Code: Cpp
Code: Cpp
Check out Interprocess communication through Named pipes by Shabbir |
Re: Interprocess Communication through WM_COPYDATA & Shared Memory
Nominate this article for Article of the month - Oct 2009
|
Re: Interprocess Communication through WM_COPYDATA & Shared Memory
HaHA! I have already seen the great thing!!!
|
| All times are GMT +5.5. The time now is 15:47. |