DLL shared data segment to achieve multi-process data sharing Summary
Some time ago, wm_copydata messages were used to implement process data communication. I want to learn about this software upgrade. dll shared data segment application, because it is easier than shared memory, I have consulted many vckbase predecessors, finally, I understood the following:
Problem description
Imagine using DLL to share process data?
Implementation
Step 1: Define the shared data segment in the DLL. cpp File
# Pragma data_seg ("shared_opcdataset ")
/* Volatile */opcdataset m_gopcdataset [opcdataset_max_len] = {0 };
# Pragma data_seg ()
# Pragma comment (linker, "/section: shared_opcdataset, RWS ")
/*
If no Initialization is available, perform the following operations:
_ Declspec (allocate ("shared_opcdataset") opcdataset m_gopcdataset [opcdataset_max_len];
*/
Step 2: Export shared segment Variables
Extern "C"
{
// Export the shared Block Variable
Opcdataset_api opcdataset extern m_gopcdataset [opcdataset_max_len];
}
Step 3: Call
# Include "opcdataset. H"
# Pragma comment (Lib, "copcdataset. lib ")
// Process A: write data to the shared segment
Char opcdatabuffer [56] = {0 };
M_gopcdataset [0]. dval = formatvarval (pvar, opcdatabuffer );
File * fp = fopen ("D: // set.txt", "");
Fprintf (FP, "% F", m_gopcdataset [0]. dval );
Fclose (FP );
// Process B: read data from the shared segment
File * fp = fopen ("D: // get.txt", "");
Fprintf (FP, "% F", m_gopcdataset [0]. dval );
Fclose (FP );
Note: It is best to place these processes in the same directory as your DLL or put your DLL in the System32 directory.
Highlights guide:
Reply to: vc_student (entry) 9:45:56
You can use DLL to export a function to operate the shared memory!
Only one process writes to this space. Other processes are read-only. Are you still using synchronization?
Reply to: vc_student (entry) 9:50:28
What should I do if I write it to half of others?
Reply: Happy ghost (bookboy) 10:03:57
Synchronization is required, but key code segments cannot be cross-process !!!
Reply to: vc_student (entry) 10:04:29
Haha, remember, the key code segment does not seem to support multi-process?
Reply to: vc_student (entry) 10:09:46
You can use mutex objects and event objects.
Reply to: folklore (entry) 10:34:26
Because you have not actually shared it. To share it, use MMAP. Thank you.
Reply: Happy ghost (bookboy) 10:39:59
This kind of software design is strong
Access to shared segments should be encapsulated in the DLL, which is a good design.