DLL shared data segment to achieve multi-process data sharing Summary

Source: Internet
Author: User


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?


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.


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.