Inter-process communication (I)

Source: Internet
Author: User

The simplest way to communicate between processes is to send the wm_copydata message.

 

Send the wm_copydata message:

Sendmessage (receiving window handle, wm_copydata,
(Wparam) Sending window handle (lparam) & copydata );

 

Copydata is the copydatastruct structure type, which is defined as follows:

Typedef struct tagcopydatastruct {

During development, data is sometimes required to be transmitted between processes. For example, if a program only allows a single instance to run, open the program again when an existing instance is running, you may need to pass information to the currently running instance for special processing. When a small amount of data is transmitted, the simplest thing is to use sendmessage to send the wm_copydata message. The wparam and lparam parameters can carry the relevant data. Because sendmessage is blocked, it will not be returned before the receiving data process completes data processing, and the sender will not delete or modify data. Therefore, this method is simple and secure, but the data volume cannot be too large, otherwise, the processing time is too long, leading to blocking and false positives.

   Use sendmessage to send wm_copydata as follows:

   Lresult=Sendmessage (     //ReturnsLresultInLresult
      (Hwnd) hwndcontrol,     // Handle to destination control
      (Uint) wm_copydata,     // Message ID
     (Wparam)Wparam,     //=(Wparam)()Wparam;
      (Lparam)Lparam     //=(Lparam)()Lparam;

   );

   

 

   Wparam is the window handle of the sender, and lparam is the pointer to a copydatastruct type struct, which contains transmitted data information. Copydatastruct is defined as follows:

   Typedef struct tagcopydatastruct{
       Ulong_ptrDwdata;
       DWORDCbdata;
       PvoidLpdata;
   } Copydatastruct, * pcopydatastruct;

   Where, dwdata is the custom data, cbdata specifies the size of lpdata pointing to the data, and lpdata is the pointer to the data. As mentioned above, when using wm_copydata, you must ensure the read-only attribute of the data, that is, there cannot be other threads of the sender to rewrite the transmitted data. (This also explains why postmessage is not allowed to send wm_copydata, because the postmessage function is asynchronous. Note that because sendmessage is blocked, it is easy to cause a deadlock. You can use sendmessagetimeout instead .) In addition, if the transmitted data involves objects or system resources, you must ensure that the receiver can process them. resources such as HDC and hbitmap are invalid and belong to different processes.

   In use, use findwindow and other APIs to find the receiver's window handle. In the receiver's program, add a response to the wm_copydata message.

   

   A program written a few days ago used wm_copydata for inter-process communication, but the receiver could not receive messages. There is no problem with the window handle found during debugging, and there is no prompt to check msdn, which cannot be solved.

   After reading some sample code, I found that the wparam and lparam parameters in my sendmessage call are both 0, because I just need to notify the receiving program through the wm_copydata message, no data is required. Try to change the two parameters to a non-empty value so that the receiver can receive the message. Conclusion: whether the wparam parameter is 0 has no effect, but the lparam parameter must be non-empty, that is, it must point to a valid copydatastruct struct.

   Why? After checking some data, we found that the bottom layer of sendmessage (wm_copydata) is completed through file mapping. The general process is that the sender thread transmits data information according to the copydatastruct structure, data is replicated in the shared memory, and the receiver thread reads data from the shared memory for processing. Therefore, if the pointer to the copydatastruct structure is null, the process cannot be performed, so the receiver cannot receive the message.

 

The previous section is reprinted. I recently went deep into core programming again. The example here is also a way to communicate between the parent process and the child process:

Parent process:

# Include <windows. h>
# Include <iostream>
# Include <sstream>
# Include <string>
# Include <iomanip>
# Include "cstring. H"

Using namespace STD;

Void main ()
{
Startupinfo Si = {sizeof (startupinfo )};
Security_attributes saprocess, sathread;
Process_information poprocesschild;
 
DWORD dprocessid = getcurrentprocessid ();
 
Tchar STR [100];

Wsprintf (STR, text ("% S % lD"), text ("e :\\ test program \ 2012-8-5 \ recvprocess \ debug \ recvprocess.exe "), dprocessid );

Sathread. lpsecuritydescriptor = NULL;
Sathread. binherithandle = true;
Sathread. nlength = sizeof (security_attributes );

Saprocess. lpsecuritydescriptor = NULL;
Saprocess. binherithandle = true;
Saprocess. nlength = sizeof (security_attributes );

Bool bcreate = CreateProcess (null, STR, & saprocess, & sathread, true, create_suincluded, null, null, & Si, & poprocesschild );
Hwnd = findwindow (null, text ("recvprocess "));
 
Resumethread (poprocesschild. hthread );
Copydatastruct CPD;/* assign a value to the copydatastruct structure */
CPD. dwdata = 0;
CPD. cbdata = dprocessid;
CPD. lpdata = NULL;

Sendmessage (hwnd, wm_copydata, null, (lparam) & CPD );
Sleep (1000 );
}

Acceptor:

DWORD processid = pcopydatastruct-> cbdata;

Or use the command line:

Cstring strcommandline = getcommandline ();
MessageBox (strcommandline );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.