Click here to Skip to main content
15,570,202 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
This may or may not be an easy question and I have been out of programming in C++ for a while, but I want to be able to "wake up" a console Win32 Console Application each time another application has updated variables in shared memory.

I have already created a shared memory dll (in c++) and I can access and update and get the data from the shared memory with "client applications"...but every time I update the data from the "clients" I would like to then somehow wake a "server" Win32 console and then run code in that console to check and work with the new values. I can make my client applications wake each other via the use of RegisterWindowMessage and PostWindowMessage calls but obviously a Win32 console is different and I'm not sure I can even try this way or if there is an obvious better way? Is it possible to simply call a function in the console application from my shared memory dll?

I hope this makes sense and I have asked the question in a general way so if someone could direct me to the safest, quickest method to do this then I can go away and try and figure it out myself if that is the best way?

Thanks

Mark
Posted

Get the console application to use the WaitForSingleObject[^] function to wait on an event[^] object which is signalled (see the SetEvent[^] function) by the client application when it wants to wake the server. The remaining issue is how to get HANDLEs to the event to the client and server processes. This should get you started. Feel free to ask some more specific questions on the problem after you've done a little research on this technique.
 
Share this answer
 
v4
Comments
Olivier Levrey 14-Feb-11 8:46am    
Good answer. See mine for sharing the event.
Mark Briscombe 14-Feb-11 13:12pm    
Thanks both of you, Oliver and Stephen, this certainly points me in the right direction and that is exactly what I was after, if I get totally stuck I can then ask further detail but hopefully I should be ok! Thanks again!
Mark Briscombe 21-Feb-11 6:13am    
Just to let you both know I got this working perfectly, thanks again!
In addition to Stephen's answer, I would suggest to use a named event: use CreateEvent in the server application and give a name to your event. Then in the client application, use OpenEvent with the same name. This allows several applications to share the same event object.

Don't forget to use threads for that, otherwise waiting for the event would just make your apps hang...
 
Share this answer
 
v2
Comments
Stephen Hewitt 14-Feb-11 8:48am    
Or you can use handle inheritence.
Olivier Levrey 14-Feb-11 9:17am    
Using handle inheritence implies you are creating the client processes from the server process. I am quite sure this is not the case here.
Stephen Hewitt 14-Feb-11 9:32am    
That's true. The OP didn't mention who creates who and such. I prefer not to use named objects if I can.
Mark Briscombe wrote:
Is it possible to simply call a function in the console application from my shared memory dll?


Yes (we call it callback), you have to pass the client function pointer to your DLL, then the latter can then call then function whenever is needed.

EnumWindows function documentation[^] may give you an insight into the callback mechanism.


:)
 
Share this answer
 
v3
Comments
Stephen Hewitt 14-Feb-11 8:25am    
The client and server are in different processes. A simple callback isn't possible in this scenario.
CPallini 14-Feb-11 8:27am    
Another process, a DLL? What are you saying, man!
Olivier Levrey 14-Feb-11 8:43am    
I think Stephen is right. OP said he has different applications. Even though all of them are using the same dll, they are still different processes. You may use a callback to communicate between client/dll or server/dll, but not directly client/server.
But voting 1 is harsh anyway...
Stephen Hewitt 14-Feb-11 8:47am    
I don't vote posts down to punish the poster but as a guide to the person who asked the question, and others who are interested in a solution. In short, helpful answers should be easily identified - people come here for help after all.
#realJSOP 14-Feb-11 8:51am    
OT: No they don't - the vast majority come here to have someone else do their legwork (or just work) for them. Maybe I'm just cynical, but the unmitigated laziness (not meaning this question) demonstrated here on an hourly basis is phenomenal...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900