Why won't my DLL share memory?

Posted on 2000-02-19
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 500 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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 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