[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sharing data in a 32-bit dll

Posted on 1999-01-14
2
Medium Priority
?
314 Views
Last Modified: 2013-11-20
I have converted a 16-bit dll to 32-bit. The old dll had been written to allow data to be shared between processes. I have modified the shared data to fit inside a #pragma data_seg("shared") section. However I have a question about how this works. If I write the code like:

typedef struct mesg_type
{
  UINT num_msg;
  char mesg[MAX_LENGTH];
} mesg_type_item;

#pragma data_seg("shared")
  mesg_type_item shared_list[MAX_ITEMS];
#pragma data_seg ()

the data does not become shared. But if I do this:

#pragma data_seg("shared")
  mesg_type_item shared_list[MAX_ITEMS] = {0,""};
#pragma data_seg ()

It appears to work fine.

Any explanations?
0
Comment
Question by:sdj_work
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
jrmcg earned 400 total points
ID: 1327679
You must initialize the data when using data_seg

Excerpt from...

HOWTO: Specify Shared and Nonshared Data in a DLL
Last reviewed: March 20, 1998
Article ID: Q100634

The system tries to load the shared memory block created by #pragma data_seg at the same address in each process. However, if the block cannot be loaded into the same memory address, it is mapped to a different address, but it is still shared.

NOTE: If the block contains pointers, this can be a problem. If the pointer holds the address of a variable not in the shared data segment then this address is valid only in one process space. If the address is in the shared data segment, it will be valid as long as the above relocation doesn’t occur. Since this is unreliable, you should not use pointers. You can use arrays in a shared data segment, but do so with caution. The array name is a pointer. Do not pass this value between processes. For example, if you have a string declared in the shared data segment as char Customer[20] = {0}, it’s okay for each process to use that variable name, as in strcpy(buf, Customer) or char FirstInitial = Customer[0]. However, do not pass the value of Customer to another process as in PostMessage(hwndNotMyWindow, WM_USER, 0, (LPARAM)Customer).

Below is a sample of how to define a named data section in your DLL. The first line directs the compiler to include all the data declared in this section in the .MYSEC data segment. This means that the iSharedVar variable would be considered part of the .MYSEC data segment. By default, data is nonshared.

Note that you must initialize all data in your named section. The data_seg pragma only applies to initialized data.

The third line below, "#pragma data_seg()", directs the compiler to reset allocation to the default data section.



Sample Code

   #pragma data_seg(".MYSEC")
   int iSharedVar = 0;
   #pragma data_seg()

You must also tell the linker that the variables in the section you defined are to be shared by modifying your .def file to include a SECTIONS section or by specifying /SECTION:.MYSEC,RWS in your link line. For example, aSECTIONS section could look like:

   SECTIONS
     .MYSEC   READ WRITE SHARED


0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1327680
This was taken from MSDN.

J.R.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question