String in a shared segment of a hook

Hail everybody!
I'm trying to write a keyboard hook with VC++ 5.0 under Win NT.
Now I've found the source of all my problems: a string in the shared segment. I'd like any suggestion that will help solving this nasty problem. I'll try to explain you....
I've 3 possibility :
1- I declare the string in the shared segment initializing it to NULL. Then, when my hook try to assign a value to it, my foreground
application (whatever it is) crashes giving this message (The
instruction in "a location" referenced to the location "0x00000000". The memory couldn't be "written"). This happens also when the string is initialized to a string long d characters and after my hook assigns to this string longer than d to this string.
2- I initialize the string to a very long string. Then the foreground applications have never crashed, but the hook "sometimes" (when it uses predefinited functions like strcpy or others) stops working. It ignores keyboard messages.
3- I do NOT put the string in the shared segment, then my hook doesn't stop, the applications don't crash, but obviously the hook doesn't work well 'cause it sometimes loses the string value.
I'm sorry for being so verbose, but I want the problem very clear to you. As previously said any help will be really welcomed.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I assume you mean a string class, not a C string literal.  i.e. not

char MyString[] = "asfasfasf asdfsa f";

but instead

string MyString;


CString MyString;

Sorry.  But that can't be made to work.   details follow.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The problem is that although the string object itself is stored in alocation that is shared among processes, the string object must allocate memory to store the actual string.  This memory (by default) comes from the C++ heap whic is not shared (each program that uses the DLL has its own heap).  So when the first program stores something in the string, it is placed in its heap.  When the second program goes to access it or change it, if fails becase the pointer stored in the string object is pointing to a location that is defined in the first process, not the second process.  

Does that make sense?
Now, I said it couldn't be made to work, but that's not strickly true.  It can be made to work, but it would be a pain.

The string tempalte class has a template parameter that is an allocator object, this is used by the string object when it needs to allocate space to store the actual string (or free space no longer needed).  You are almost certainly using the default allocator (everyone does) which allocates in the C/C++ heap.  However, you could write your own allocator and specify it instead.  That part is easy.  The hard part is that this allocator woudl have to allocate and free from a heap that you place in the shared data segment (and you would have to write the procedures to manage the heap.  That isn't much fun!)

Let me know if yoiu have questions.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

BalkanAuthor Commented:
All right, I've understood.
What about declaring the string like a char array?
char pippo[100]="";
In this manner it is really shared, isn't it?
Yes, but that sort of string (character array) is not very powerful.  You loose all the fancy features of the string class.  But you do gaint he 1 feature "that it works" which can't be undervalues.
BalkanAuthor Commented:
Ok, what about lists?
I need a pointer to a list in the shared data. From what you said this is not possibile, is it?
Any suggestion?
Again you ned to make sure all the "peices" of the list are in the shared data segment.  By default they won't be.  You would have to write your own code to take care of this.
BalkanAuthor Commented:
Oh yes, but I think this is above my programming ability.
BalkanAuthor Commented:
I think the best solution would be to make all this job in the main application leaving my hook only the job to hook the messages and to send significative data to the main application. Do you agree?
I don't know enough about what you are trying to do to say for sure, but that sounds reasonable.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.