Shared Memory for Windows inter-process communication

Source: Internet
Author: User

A previous project involves inter-process communication. I use the socket method. I asked why I didn't use other methods during the interview. Well, we found that shared memory is more convenient. So I wrote it myself and made a test interface.

When a program starts, it obtains its own window handle. You can also enter a window handle for Windows message communication between processes. File mapping name is actually the kernel object name, which is used to access shared memory between processes.

In Windows, shared memory is actually implemented by file ing. You can use createfilemapping to create a memory file ing object for ing files to memory. This function returns the file mapping object handle.

 

CreateFileMappingA(    __in     HANDLE hFile,    __in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,    __in     DWORD flProtect,    __in     DWORD dwMaximumSizeHigh,    __in     DWORD dwMaximumSizeLow,    __in_opt LPCSTR lpName    );

 

1) because you do not need to create an actual file, set hfile to 0 xffffffff (invalid_handle_value.

2) Windows supports 64-bit files, but generally the memory will not exceed 4 GB, that is, 32-bit files are enough. Therefore, among the above parameters, dwmaximumsizehigh is 0, dwmaximumsizelow is set to the memory size to be opened.

3) lpname is used to indicate the memory, and the memory is accessed between processes.

Then, mapviewoffile () is called to map to the virtual address of the current process. If the function is called successfully, the start address of the ing file is returned. If a failure occurs, it is null.

 

MapViewOfFile(    __in HANDLE hFileMappingObject,    __in DWORD dwDesiredAccess,    __in DWORD dwFileOffsetHigh,    __in DWORD dwFileOffsetLow,    __in SIZE_T dwNumberOfBytesToMap    );

1) hfilemappingobject is the handle returned by calling createfilemapping.

 

2) dwdesiredaccess is set to file_map_all_access.

3) dwnumberofbytestomap: the number of bytes to be mapped in the file, that is, the size of the shared memory.

This is how my program calls it.

 

int Cp1Dlg::MapFile(){CString str;m_edit7.GetWindowTextA(str);if(str.GetLength() == 0){AfxMessageBox("File Mapping Name can't be NULL");return -1;}m_hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,BUF_SIZE,str.GetBuffer());if(m_hMapFile == NULL){AfxMessageBox("CreateFileMapping failed!");return 1;}m_pBuf = static_cast<LPTSTR>(MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,0,0,BUF_SIZE));if(m_pBuf == NULL){AfxMessageBox("MapViewOfFile failed!");CloseHandle(m_hMapFile);return 1;}return 0;}

Then you can write or read data to m_pbuf.

 

Write Data:

 

m_edit6.GetWindowTextA(str);memcpy((void*)m_pBuf, str.GetBuffer(), str.GetLength());

Read data:

 

 

Lresult cp1dlg: onmsg3 (wparam, lparam) {cstring STR; Str. format ("R: \ t % s", m_pbuf); m_list2.addstring (STR); memset (void *) m_pbuf, 0, buf_size ); // after reading, the memory is cleared and return 0 ;}

I use message processing functions to read data. Because when a process finishes data on the memory, I will send a Windows message to notify another process. After receiving this message, it will call the function to read the data.

 

Compared with socket, it is indeed quite convenient. But what if two processes write data to the memory at the same time? This involves the process synchronization technology. But I did not do this.

Package and upload resources.

 

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.