We help IT Professionals succeed at work.

Why won't my DLL share memory?

dreadedjew asked
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.


Watch Question


Adjusted points to 450
This one is on us!
(Get your first solution completely free - no credit card required)

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?

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.


Adjusted points to 500


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.

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.