Why won't my DLL share memory?

Hi,

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];
} SEARCHSTRING;

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;
SEARCHSTRING searchStrings[MAXSTRING];
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.

HELP!!

-dj
dreadedjewAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
I beleive it is because the data is not statically initialized

continues.
0
 
dreadedjewAuthor Commented:
Adjusted points to 450
0
 
nietodCommented:
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:


   SECTIONS
   .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?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

SEARCHSTRING searchStrings[MAXSTRING] = {"ABC"};

(I'm not sure what a SEARCHSTRING is, but just give it a few initialized entries like this and it should be fine.
0
 
dreadedjewAuthor Commented:
Adjusted points to 500
0
 
dreadedjewAuthor Commented:
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.

-brett
-------
the jew with dreadlocks.



0
 
nietodCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.