Why won't my DLL share memory?

Posted on 2000-02-19
Medium Priority
Last Modified: 2008-02-01

I have written a dll which sets a WH_MESSAGE hook into all threads. My GetMsgProc quickly returns if the message is not WM_CHAR or the app is just peeking.

I am scanning all threads for certain keywords (one character at a time) so i've set up an array of arrays like so:

typedef struct
   char hunted[20];

Then i declare the array and put it into shared memory space like this:

#pragma data_seg(".shared")                                                  
SEARCHSTRING searchStrings[20];         // 20 character arrays
HHOOK g_hhook = NULL;
DWORD g_dwThreadId = 0;
HINSTANCE g_hinstDll = NULL;
#pragma data_seg()

#pragma comment(linker, "/SECTION:.shared,RWS")

The app that sets the windows hook first initializes the arrays to the values that i'm looking for and then sets the hook.

The GetMsgProc gets WM_CHAR messages from all system threads and for debug purposes i have it send the contents of the arrays to a file for me to inspect whenever it receives a WM_CHAR message. When the app that set the hook receives the WM_CHAR message it sends the content of the array to a file just fine, but when another app like word or netscape receives the WM_CHAR message the data in the array has vanashied!!! But I can give focus back to the app that set the hook, generate a WM_CHAR message (by hitting a key) and the contents of the array show up in the file. So they didn't vanish, they just aren't showing up.

I put another shared variable in there so now the pragma looks like this:

#pragma data_seg(".shared")                                                  
char target[MAXSTRING] = "shared mem test";
HHOOK g_hhook = NULL;
DWORD g_dwThreadId = 0;
HINSTANCE g_hinstDll = NULL;
#pragma data_seg()

And I can print the array target to a file from any app or thread so I know it's not a bug in MSVC 6.0 or win 98 and I know the syntax of my pragma is correct.


Question by:dreadedjew
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
  • 4
  • 3

Author Comment

ID: 2538695
Adjusted points to 450
LVL 22

Accepted Solution

nietod earned 2000 total points
ID: 2538736
I beleive it is because the data is not statically initialized

LVL 22

Expert Comment

ID: 2538745
According to the VC docs the data must be initalized (they mean statically initialized) to be included in the names data segment.  From the docs

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

Also you must indicate that this segment is to be shared.  You do this in the module defintions file or with a Pragma.  again from the docs


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:.shared,RWS in your link line. Here's an example SECTIONS section:

   .shared   READ WRITE SHARED

Alternatively, some compilers allow you to set the linker switch in your code so that if your file is ever copied to another project, the linker switch goes with it. To do this, include the following line in your code preferably near the #pragma data_seg(".shared") line:

   #pragma comment(linker, "/SECTION:.shared,RWS")


Are you making this segment shared?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 22

Expert Comment

ID: 2538747
I should mention, to statically initialize the array just do something like


(I'm not sure what a SEARCHSTRING is, but just give it a few initialized entries like this and it should be fine.

Author Comment

ID: 2538845
Adjusted points to 500

Author Comment

ID: 2538846
Ah ha!!!!

I wasn't statically initializing the shared data!!!

Almost 1000 points later and the answer has been found!

You ARE truely an expert!

Thank you for always answering my questions so promptly but more importantly for providing me correct answers.

the jew with dreadlocks.

LVL 22

Expert Comment

ID: 2538959
I don't know if I should mention it or not, but you really didn't need to spend the 2nd 500 points.  That certainly was "covered" under the original question.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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