Methods for Windows interprocess communication

Source: Internet
Author: User

Copyright notice

Please respect original works. Reprint please maintain the integrity of the article, and in the form of hyperlinks to the original author "Tingsking18" and the main site address, so that other friends to ask questions and correct.

1. Using Shared memory

The code is as follows:

[CPP]View Plaincopy
  1. void filemapping (void)
  2. {
  3. //Open the shared file object.
  4. M_hmapfile =:: OpenFileMapping (File_map_all_access, false,_t ("Testfilemap"));
  5. if (m_hmapfile)
  6. {
  7. //Display shared file data.
  8. LPTSTR lpmapaddr = (LPTSTR) mapviewoffile (m_hmapfile,file_map_all_access,0,0,0);
  9. OutputDebugString (LPMAPADDR);
  10. }
  11. Else
  12. {
  13. //Create a shared file.
  14. M_hmapfile =:: CreateFileMapping ((HANDLE) 0xFFFFFFFF, NULL, page_readwrite, 0, 1024x768, "Testfilemap");
  15. //Copy data to a shared file.
  16. LPTSTR lpmapaddr = (LPTSTR) mapviewoffile (m_hmapfile,file_map_all_access,0,0,0);
  17. StrCpy (lpmapaddr,"Testfilemap");
  18. Flushviewoffile (lpmapaddr,12+1);
  19. }
  20. }

Note: After using the shared memory, you will have to delete the shared memory, otherwise a lot of temporary files are generated.

UnmapViewOfFile (M_hmapfile)

2. Using DLLs to share memory between processes

#pragma data_seg (". Idleui ")//must define as SHARED in. def
Hhook g_hhookkbd = NULL; One instance for all processes
Hhook g_hhookmouse = NULL; One instance for all processes
DWORD G_dwlastinputtick = 0; Tick time of last input event
#pragma data_seg ()
It is then defined in the Def file: SECTIONS. Idleui READ WRITE SHARED

Note: Shared data must be initialized, otherwise the Microsoft compiler will put the data that is not initialized. BSS segment, resulting in shared behavior between multiple processes failing.

3. Use Wm_copydata to pass messages between form programs.

The code for sending is as follows: The primary purpose of the WM_COPYDATA message is to allow read-only data to be passed between processes. The SDK documentation recommends that the user use the SendMessage function, and the recipient does not return until the data copy is complete, so that the sender cannot delete and modify the data:

[CPP]View Plaincopy
  1. HWND Hwnd=::findwindow (NULL,"B");
  2. if (hwnd!=null)
  3. {
  4. Copydatastruct CPD; / * Assign a value to the COPYDATASTRUCT structure * /
  5. Cpd.dwdata = 0;
  6. Cpd.cbdata = strlen ("string");
  7. Cpd.lpdata = (void*)"string";
  8. :: SendMessage (Hwnd,wm_copydata,null, (LPARAM) &CPD);            Send!
  9. }

When receiving:

afx_msg BOOL oncopydata (cwnd* pWnd, copydatastruct* pcopydatastruct);


BOOL Cmydlg::oncopydata (cwnd* pWnd, copydatastruct* pcopydatastruct)
{
Todo:add your message handler code here and/or call default
AfxMessageBox ((LPCSTR) (pcopydatastruct->lpdata));/* Use a dialog box to receive a message */
Return Cdialog::oncopydata (PWnd, pcopydatastruct);
}

4. Call readprocessmemory and the WriteProcessMemory function.

Call ReadProcessMemory and WriteProcessMemory function The user allocates a chunk of memory in the sending process to hold the data, invoking the GlobalAlloc or VirtualAlloc function implementation:
Papp->m_hglobalhandle=globalalloc (gmem_share,1024);
You can get the pointer address:
Papp->mpszglobalhandleptr= (LPSTR) GlobalLock
(Papp->m_hglobalhandle);
An open handle to the process that the user wants to affect is used in the receive process. To read and write to another process, call the OpenProcess function as follows:
HANDLE htargetprocess=openprocess (
standard_rights_required|
process_vm_reda|
process_vm_write|
process_vm_operation,//access Rights
false,//Inheritance Relationship
DWPROCESSID);//Process ID
To ensure that the OpenProcess function call succeeds, the process that the user affects must be created by the above flags.
When the user obtains a valid handle to a process, it can call the ReadProcessMemory function to read the memory of the process:
BOOL ReadProcessMemory (
HANDLE hprocess,//process pointers
Lpcvoid lpbaseaddress,//Data block's first address
LPVOID lpbuffer,//buffer required to read data
DWORD cbread,//number of bytes to read
Lpdword Lpnumberofbytesread
);
The memory of the process can also be written using the same handle:
BOOL WriteProcessMemory (
HANDLE hprocess,//process pointers
LPVOID lpbaseaddress,//first address to write
LPVOID lpbuffer,//buffer address
DWORD Cbwrite,//number of bytes to write
Lpdword Lpnumberofbyteswritten
);

Methods for Windows interprocess communication

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.