Sharing Data Between Different Instances of the Same Module (DLL or EXE)

Memory-mapped files are a great way to share data between processes (see Zoran's article Client/Server Interprocess Communication Via Shared Memory).  They're easy to create, and easy to work with 'cause you just end up with a regular-old pointer to your data.  But despite the not-too-bad syntax...

CreateFileMapping(); // or OpenFileMapping() if you're a "client"
pData = MapViewOfFile();
// do stuff with pData
UnmapViewOfFile();
CloseHandle();

...there's an even EASIER way to share data between different instances of the same application or DLL.

The Visual C++ compiler lets you name your own data segments.  You can pass an option to the linker that tells it to share one of these custom data segments, in which case all instances of your module (DLL or EXE) will map to the same instance of your custom data segment.

Put this in the code module where you want the "process-global" variables:

#pragma data_seg(".SHARED")

char		pg_pszShared[256] = {0}; // variables MUST be initialized,
					 // not sure why

#pragma data_seg()

#pragma comment(linker, "/section:.SHARED,RWS")

Note that the name ".SHARED" could be whatever you want.  And you can add almost any variables you want between the two "#pragma data_seg()" directives.  I've encountered two restrictions so far: pointers that point to data that's outside the shared segment don't work (for hopefully obvious reasons), and variables for classes that do their own memory-management (like MFC's CString) don't work.

The "data_seg" pragma tells the compiler to put all the variables following in the specified data segment (or rather, data section in Win32 speak).  Calling it the second time with no argument tells it to go back to using the default data section.

The "comment" pragma passes an option to the linker that tells it to actually share the specified data section.  You could put this in a .DEF file too. 

So anyway, as far as I'm concerned, this is MUCH cleaner than creating a memory mapped file for every piece of data you want to share.  You don't have to keep any handles around and worry about closing them.  You just declare a variable in your shared section and, voila!

Rumor has it that doing this causes the compiler to generate code that actually DOES create a memory-mapped file behind the scenes.  Whatever.  I don't care.   All I know is, it's a lot less to have to worry about.

Below is a little VCPP6 MFC program that demonstrates that this technique works.  Run a couple instances of it, type some data in the first one, and hit "Refresh" on the second.

Download demo application - 4 KB

Download demo project - 10 KB

Date Last Updated: February 14, 1999



Comments

  • Green city refutes a rumor sweet pastry does not turn meeting bark fleabane says the player is due professional accomplishment (2)

    Posted by objcgabuzh on 05/17/2013 06:26pm

    Green city refutes a rumor sweet pastry does not turn meeting bark fleabane says the player is due professional accomplishment Report from our correspondent should turn about fleabane of bark of green city header these days the message of the meeting uploads bubbling with noisely in the network, yesterday, green city club reiterates again, bark fleabane new sports season won't go. Current home turns the OK and current good player on meeting market is not much, and what like bark fleabane a such sports season can have 8 secondary attack of 8 goals is medium field player, it is difficult to do not want to make sweet bun, not long ago, fabulous Guangzhou rich force leaves with respect to Xiang Lucheng gave a high quoted price, and this also lets many green city fan have some of perturbed, bark a word of the life sighs with emotion on high small gain a few days ago, what was become to do drop out by fan is augural. "A lot of fan are on small gain @ I, have persuade to stay, still have a blessing. " meet about turning, wang Song says, the fact does not resemble outside hearsay in that way, "Before paragraph time wife gives birth to the child, now just just rejoin, restoring in what make physical ability, my where has time to be in charge of these. " of course, wang Song also says frankly, the high pay that should say rich force is not enchanted not actual also, but he expresses, one when serve as green city, should show the professional accomplishment with due player. "It is meddlesome that fan friends care green city team member, nevertheless this thing returns Wang Song is to hope everybody does not want again much Lv, club already excuse oneself from the quote of rich force. " yesterday, deputy total Bao Zhongliang tells green city club the reporter, "Wang Song is very important to green city, he and club still have two years of contracts, bilateral metropolis continues to fulfill. Bilateral metropolis continues to fulfill.. Not only Wang Song won't go, of green city brought aid to also have new [url=http://jordankicks.page4.me/]jordan sneakers for sale[/url] progress. "Of two foreign aids introduce, include Ma Zun to pull those who take a line to negotiate the way with good hold office at court develops. " Bao Zhongliang says. Wang Jiajun of our newspaper trainee Related news

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds