) Process Communication (user-defined message, user-registered message, Windows clipboard, wm_copy, memory ing, memory for target process)

Source: Internet
Author: User
Sender:
Create a basic dialog box project, add six text box controls, and associate the control variables (cstring type ):
M_strcopydata, m_strfilemap, m_strmem, m_strregmsg, m_strusermsg, m_strclipboard

Then, define the following in the CPP file:

# Define buffer_size 0x100 // The maximum memory allocated when communicating with the memory address.
# Define wm_comm wm_user ++ 100
Const uint wm_nregmsg = registerwindowmessage ("reg_data ");
Const uint wm_nmemmsg = registerwindowmessage ("mem_data ");

Add 6 buttons to writeCode

// Send in wm_copy Mode
Void cdatasenddlg: onsendcopydata ()
{
Updatedata (); // update data.
Cwnd * pwnd = cwnd: findwindow (null, _ T ("datarecv"); // find the datarecv process.
If (pwnd = NULL ){
Afxmessagebox (text ("unable to find datarecv ."));
Return;
}

Copydatastruct CPD; // assign a value to the copydatastruct structure.
CPD. dwdata = 0;
CPD. cbdata = m_strcopydata.getlength ();
CPD. lpdata = (void *) m_strcopydata.getbuffer (CPD. cbdata );
Pwnd-> sendmessage (wm_copydata, null, (lparam) & CPD); // send.
}

// Send a custom message
Void cdatasenddlg: onsendusermsg ()
{
Updatedata (); // update data.
Cwnd * pwnd = cwnd: findwindow (null, _ T ("datarecv"); // find the datarecv process.
If (pwnd = NULL ){
Afxmessagebox (text ("unable to find datarecv ."));
Return;
}

Uint umsg;
Umsg = atoi (m_strusermsg );
Pwnd-> sendmessage (wm_comm, null, (lparam) umsg); // send.
}

// Send messages by user registration
Void cdatasenddlg: onsendregmsg ()
{
Updatedata (); // update data.
Cwnd * pwnd = cwnd: findwindow (null, _ T ("datarecv"); // find the datarecv process.
If (pwnd = NULL ){
Afxmessagebox ("unable to find datarecv .");
Return;
}
Uint umsg;
Umsg = atoi (m_strregmsg );
Pwnd-> sendmessage (wm_nregmsg, null, (lparam) umsg); // send.
}

// Use the access method for the target process memory to send
Void cdatasenddlg: onsendmem ()
{
Updatedata (); // update data.

Cwnd * pwnd = cwnd: findwindow (null, _ T ("datarecv"); // find the datarecv process.
If (pwnd = NULL ){
Afxmessagebox ("unable to find datarecv .");
Return;
}

// Obtain the process ID.
Dword pid;
Getwindowthreadprocessid (pwnd-> m_hwnd, (DWORD *) & pid );
Handle hprocess = OpenProcess (process_all_access, false, pid );

// Allocate virtual memory.
Lpvoid lpbaseaddress;
Lpbaseaddress = virtualallocex (hprocess, 0, buffer_size,
Mem_commit, page_readwrite );

Char data [buffer_size];
Strcpy (data, m_strmem );

// Write the string to the memory of the hprocess.
Writeprocessmemory (hprocess, lpbaseaddress, Data, buffer_size, null );

// Send the base address to the datarecv process.
Pwnd-> sendmessage (wm_nmemmsg, null, (lparam) lpbaseaddress );

// Wait for receivingProgramReceive data.
Sleep (100 );

// Release the virtual memory.
Virtualfreeex (hprocess, lpbaseaddress, 0, mem_release );
}

// Use memory ing for sending
Void cdatasenddlg: onsendfilemap ()
{
Updatedata (); // update data.

// create a memory image object.
handle hmapping;
lpstr lpdata;
hmapping = createfilemapping (handle) 0 xffffffff, null,
page_readwrite, 0, buffer_size, "myshare");
If (hmapping = NULL)
{< br> afxmessagebox ("createfilemapping () failed. ");
return;
}

// Map the view of the file to the address space of a process and return the memory pointer of the lpvoid type.
Lpdata = (lpstr) mapviewoffile (hmapping, file_map_all_access, 0, 0 );
If (lpdata = NULL)
{
Afxmessagebox ("mapviewoffile () failed .");
Return;
}

// Write data to the memory of this image.
Sprintf (lpdata, m_strfilemap );

// Release the image memory.
Unmapviewoffile (lpdata );
}

// Use the Windows clipboard for sending
Void cdatasenddlg: onsendclipboard ()
{
Updatedata (); // update data.
Cstring strdata = m_strclipboard; // obtain data.

// Open the system clipboard.
If (! Openclipboard () return;

// Clear the system clipboard before use.
Emptyclipboard ();

// Allocate a memory. The size is equal to the size of the string to be copied. The returned memory control handle.
Hglobal hclipboarddata;
Hclipboarddata = globalalloc (gmem_ddeshare, strdata. getlength () + 1 );

// Lock the memory control handle. The returned value is a pointer to the specific data format of the memory control handle.
Char * pchdata;
Pchdata = (char *) globallock (hclipboarddata );

// Assign the value of the local variable to the global memory.
Strcpy (pchdata, lpcstr (strdata ));

// Unlock the locked global memory control handle.
Globalunlock (hclipboarddata );

// Place the data to be copied to the clipboard using the global memory handle.
Setclipboarddata (cf_text, hclipboarddata );

// Close the clipboard after use.
Closeclipboard ();
}

This completes the sender

Acceptor:
Create a basic dialog Project (datarecv)
Add 6 text box controls and associate them with the control variables (cstring type ):
M_strcopydata,M_strfilemap,M_strmem,M_strusermsg,M_strregmsg,M_strclipboard

Then, define the following in the CPP file:

# Define buffer_size 0x100 // The maximum memory allocated when communicating with the memory address.
# Define wm_comm wm_user ++ 100
Const uint wm_nregmsg = registerwindowmessage ("reg_data ");
Const uint wm_nmemmsg = registerwindowmessage ("mem_data ");

Add two buttons and write the code:

// Set the Control ID to idc_recv_clipboard and assign the obtained value to m_strfilemap.
// Use the Windows clipboard for receiving
Void cdatarecvdlg: onbnclickedrecvclipboard ()
{
// Todo: add the control notification handler code here
// Open the system clipboard.
If (! Openclipboard () return;

// Determine whether the data on the clipboard is in the specified data format.
If (isclipboardformatavailable (cf_text) | isclipboardformatavailable (cf_oemtext ))
{
// Obtain data from the clipboard.
Handle hclipboarddata = getclipboarddata (cf_text );

// Lock the memory handle to obtain the pointer to the data in the specified format.
Char * pchdata = (char *) globallock (hclipboarddata );

// Obtain data from local variables.
M_strclipboard = pchdata;

// Unlock the memory handle.
Globalunlock (hclipboarddata );
}
Else
{
Afxmessagebox ("there is no text (ANSI) data on the clipboard .");
}

// Close the clipboard after use.
Closeclipboard ();

// Update data.
Updatedata (false );
}

// The Control ID is idc_recv_filemap, assign the obtained value to m_strclipboard
// use the memory ing method receive
void cdatarecvdlg:: onbnclickedrecvfilemap ()
{< br> // todo: add the control notification handler code here
// create a memory image object.
handle hmapping;
lpstr lpdata;
hmapping = createfilemapping (handle) 0 xffffffff,
null, page_readwrite, 0, 0, 0, 100, "myshare");
If (hmapping = NULL)
{< br> afxmessagebox ("createfilemapping () failed. ");
return;
}

// Map the view of the file to the address space of a process and return the memory pointer of the lpvoid type.
Lpdata = (lpstr) mapviewoffile (hmapping, file_map_all_access, 0, 0 );
If (lpdata = NULL)
{
Afxmessagebox ("mapviewoffile () failed .");
Return;
}

// Assign the memory data of this image to the local variable.
M_strfilemap.format ("% s", lpdata );

// Release the image memory.
Unmapviewoffile (lpdata );

// Update data.
Updatedata (false );
}

then in datarecvdlg. add message ing in CPP
begin_message_map (cdatarecvdlg, cdialog)
//}} afx_msg_map
on_bn_clicked (idc_recv_clipboard, onbnclickedrecvclipboard)
on_bn_clicked (idc_recv_filemap, onbnclickedrecvfilemap)
on_wm_copydata ()
on_message (wm_comm, onuserreceivemsg)
messages (wm_nregmsg, onregreceivemsg)
messages (wm_nmemmsg, onregmemmsg)
end_message_map ()

Add Response Message in datarecvdlg. h
Afx_msg bool oncopydata (cwnd * pwnd, copydatastruct * pcopydatastruct );
Afx_msg lresult onuserreceivemsg (wparam, lparam );
Afx_msg lresult onregreceivemsg (wparam, lparam );
Afx_msg lresult onregmemmsg (wparam, lparam );

Finally, complete the corresponding message function in datarecvdlg. cpp.

// Use wm_copyReceive
Bool cdatarecvdlg: oncopydata (cwnd * pwnd, copydatastruct * pcopydatastruct)
{
M_strcopydata = (lpstr) pcopydatastruct-> lpdata;

// Obtain the string of the actual length.
M_strcopydata = m_strcopydata.left (pcopydatastruct-> cbdata );

// Update data.
Updatedata (false );
Return cdialog: oncopydata (pwnd, pcopydatastruct );
}

// Use custom messagesReceive
Lresult cdatarecvdlg: onuserreceivemsg (wparam, lparam)
{
M_strusermsg.format ("% d/N", INT (lparam ));
// Update data.
Updatedata (false );
Return true;
}

// Use the message registration method.Receive
Lresult cdatarecvdlg: onregreceivemsg (wparam, lparam)
{
M_strregmsg.format ("% d/N", INT (lparam ));
// Update data.
Updatedata (false );
Return true;
}

// Access the memory of the target processReceive
Lresult cdatarecvdlg: onregmemmsg (wparam, lparam)
{
Lpvoid lpbaseaddress = (lpvoid) lparam;

// Write the string to the memory of the hprocess.
Handle hprocess = getcurrentprocess ();

Char data [buffer_size];
Readprocessmemory (hprocess, lpbaseaddress, Data, buffer_size, null );
M_strmem = data;

// Update data.
Updatedata (false );

Return true;
}

Now, the acceptor is complete.

note that the acceptor must run first. Custom messages and registered messages can only send long integer data, other methods can send string data. use memory ing and Windows clipboard to receive data, you must click a button to receive the data. Otherwise, the data is automatically received.

Related Article

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.