问题
A desktop app (made in Delphi) is started by User A. Let's call it "Instance A".
User A does a "switch user" and User B logs in.
User B runs the same application. Let's call it "Instance B"
What I want now, is a way for the Instance B to send messages to Instance A.
I tried the following: Instance A writes its handle in a file, so Instance B can open that file, read the handle, and use it to post a message to Instance A, but it doesn't work -- perhaps for security reasons Windows doesn't give one user access to handles of running processes of another user...
A "bad" way to do this would be to have Instance A check a particular file or registry location every few seconds, so Instance B can write something there and Instance A will get it... but this is obviously a burdensome and unelegant solution.
Instead what I need is a way for Instance B of user B to send a harmless message to Instance A of user A, after which Instance A wakes up and decides what to to about it.
Thanks for any suggestions!
回答1:
You cannot use SendMessage, PostMessage and similar functions because application instances from different user sessions are not available to your application.
What you can use are named pipes, semaphores etc. in the global namespace (i.e. having names prefixed by "Global\"). Then, create a separate thread in your application that will, for example, sleep until a "signal" from one of these arrives and notify the main window accordingly.
To save resources, use WTSRegisterSessionNotification to get notified when the session switch occurs and create the thread only at that point.
More information here: http://support.microsoft.com/kb/310153 and here: http://msdn.microsoft.com/en-us/library/ms997634.aspx
回答2:
Isn't this essentially the same problem as having a windows service run under System account and having a user instance of some application communicate with it? Then maybe you should google for interprocess communication (named pipes, etc.).
You might also use UDP or TCP/IP, but I guess that using named pipes is better for "local" communication (though I've never done it really).
Read here about named pipes on MSDN.
来源:https://stackoverflow.com/questions/1673507/sending-a-message-to-an-application-started-by-a-different-windows-user-session