Sharing issues between different processes has always been a programming personnel often need to face, but it is not easy to solve. I am deeply harmful in the project of writing procedures, so I think that I have a summary of the situation I have encountered, and it has also contributed to this article. The article focuses on the sharing of data and core sectors in the article. Disclaimer: Many technologies in the article are not invented, but in many books and websites, I can see that the work I have done is just a summary in using the project. - Feng Xiao Yun --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- A technique that often needs to face during the process, however, some work we have encountered is not only limited in a process, and the sharing between different processes also needs us to face. This article focuses on the sharing of data and kernel objects. Data Sharing Different Processes Shared Data is often implemented by setting up a shared memory in a DLL file. The structure is as follows: EXE1 EXE2 ------------- Dateseg DLL specific implementation method is A shared data segment is set in the DLL. The data in this data segment can be provided to different processes to achieve the purpose. The shared data DLL allows the process to access read / write data in a way similar to Windows 3.1 DLL sharing data, and multiple processes can perform data operations on the shared data DLL to achieve the purpose of sharing data. In Win32, for the establishment of shared memory, you must perform the following steps: first create a famous data area. This is used in Visual C to use Data_SEG PRAGMA macros. Using DATA_SEG PRAGMA Macro must pay attention to the initialization of data: #pragma data_seg ("mysec") char mysharedData [4096] = {0}; # pragma data_seg () The shared property is set in the user's DEF file. Library TestData Read WriteseSeses.Mysec Read Write Shared This will receive a DLL process will receive your own data copy, and a process of data changes are not reflected in the data of other processes. The data is appropriately output in the DEF file. The following DEF file item illustrates how to output mysharedData in the form of a constant variable. ExportsMysharedData Constant Finally, in the application (process), the shared data is referenced by external variables. EXTERN _EXPORT "C" {char * mysharedData [];} The variable should be used to pay attention to indirect references. m_pstatic = (CEDIT *) getDLGITEM (IDC_Shared); m_pstatic-> getLine (0, * mysharedData, 80); Under the sharing of kernel objects, threads running in different processes require sharing kernel objects. Here's why you need sharing: • File mapping objects allow you to share data blocks between two processes running on the same machine. • Email and specified pipes allow applications to send data blocks between processes running on the connected network.
• Mutually exclusive objects, beacons and events make threads in different processes to synchronize their continuous operation, which is the same as one of the applications need to notify another application when completing an application. Generally speaking, for the kernel object handle of the operating system, such as threaded sessions, such as threaded sessions, etc., that is, these handles can only belong to the process, the handle itself is just a 32-bit value, and the different handles have different The meaning of Hbrush, Hinstance, HRESULT is a handle, but its meaning is large, but in general, the handle is identified (HRESULT is not), so the general handle is the memory space related, and different EXEs. The space is independent, so the value of the shared handle is meaningless. The value of these handles that is actually returned is the index value in the process handles. So how do you share a kernel object handle between different processes? In general, the system provides us with three methods: 1 Using the inheritance of the object handle is only the inheritance of the object handle only when the process has a parent child relationship. In this case, the parent process can use one or more kernel object handles, and the parent process can decide to generate a child process to give the child process to the access to the kernel object of the parent process. . To make this type of inheritance can be implemented, the parent process must perform several steps. First, when the parent process creates a kernel object, it must be indicated to the system, and it hopes that the handle of the object is an inherited handle. This requires that the process must specify a S-E C U R I T Y _ At T R I B U T E S structure and initialize it, and then transmit the address of the structure to a specific C R E A T E function. Then, let the parent process generate a child process. This is done using the C R E A T EP R O C E S S function. Bool createprocess
LPCTSTR LPAPPLICATIONNAME, // Name of Executable Module
LPTSTR LPCOMMANDLINE, // Command Line String
LPSecurity_attributes lpprocessattributes, // sd
LPSecurity_attributes lpthreadattributes, // sd
Bool BinheritHandles, // Handle Inheritance Option
DWORD dwcreationFlags, // Creation Flags
LPVOID LPENVIRONMENT, // NEW ENVIRONMENT Block
LPCTSTR LPCURRENTDIRECTORY, / / CURRENT DIRECTORY NAME
LPStartupinfo LPStartupinfo, // Startup Information
LPPROCESS_INFORMATION LPPROCESSINFORMATION // Process Information
);
It can be implemented by setting BinheritHandles to TRUE. Next, the sub-process generated by this method can have the inheritance of the core sector of the parent process. 2 Named Object Sharing The second method of the kernel object across the process boundary is to name the object. Many (although not all) kernel objects are named. However, when creating an unnamed object, you can share the object of the process by using inheritance (as described in the previous method) or D u P L I c a t e h a n d L e (next method will be described). To share objects by name, you must give an object to an object. 3 Copying object handles Sharing the last method of kernel objects across process boundaries is to use D u p L i c a t e h a n d l E function BOOL duplicateHandle
Handle HsourceProcessHandle, // Handle To Source Process
Handle HsourceHandle, // Handle To Duplicate
Handle HtargetProcessHandle, // Handle to Target Process
LPHANDLE LPTARGETHANDLE, / / DUPLICATE HANDLE
DWORD DWDESIREDACCESS, // Requested Access
Bool BinheritHandle, // Handle Inheritance Option
DWORD DWOPTIONS // Optional Action
);