Solved

Why won't my DLL share memory?

Posted on 2000-02-19
7
446 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:dreadedjew
  • 4
  • 3
7 Comments
 

Author Comment

by:dreadedjew
ID: 2538695
Adjusted points to 450
0
 
LVL 22

Accepted Solution

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

continues.
0
 
LVL 22

Expert Comment

by:nietod
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:


   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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 22

Expert Comment

by:nietod
ID: 2538747
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
 

Author Comment

by:dreadedjew
ID: 2538845
Adjusted points to 500
0
 

Author Comment

by:dreadedjew
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.

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



0
 
LVL 22

Expert Comment

by:nietod
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now