向其他Windows用户会话启动的应用程序发送消息

问题描述:

用户A启动了一个桌面应用程序(由Delphi制造).我们将其称为实例A".

A desktop app (made in Delphi) is started by User A. Let's call it "Instance A".

用户A执行切换用户",用户B登录.

User A does a "switch user" and User B logs in.

用户B运行相同的应用程序.我们称之为实例B"

User B runs the same application. Let's call it "Instance B"

我现在想要的是实例B向实例A发送消息的一种方式.

What I want now, is a way for the Instance B to send messages to Instance A.

我尝试了以下操作:实例A将其句柄写入文件中,因此实例B可以打开该文件,读取该句柄,然后使用它向实例A发布消息,但它不起作用-也许对于安全原因Windows无法授予一个用户访问另一个用户正在运行的进程的句柄的权限...

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每隔几秒钟检查一次特定的文件或注册表位置,因此实例B可以在那里写一些东西,而实例A可以得到它……但这显然是很麻烦的和明确的解决方案.

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.

相反,我需要的是用户B的实例B向用户A的实例A发送无害消息的方法,此后实例A醒来并决定要怎么处理.

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.

谢谢您的建议!

您不能使用SendMessage,PostMessage和类似功能,因为来自不同用户会话的应用程序实例对您的应用程序不可用.

You cannot use SendMessage, PostMessage and similar functions because application instances from different user sessions are not available to your application.

您可以使用的是全局名称空间中的命名管道,信号灯等(即,名称以"Global \"作为前缀).然后,在您的应用程序中创建一个单独的线程,例如,将休眠直到其中一个线程发出信号",并相应地通知主窗口.

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.

要节省资源,请使用WTSRegisterSessionNotification在会话切换发生时得到通知,并仅在该点创建线程.

To save resources, use WTSRegisterSessionNotification to get notified when the session switch occurs and create the thread only at that point.

更多信息在这里: http://support.microsoft.com/kb/310153 和这里: http://msdn.microsoft.com/en-us/library/ms997634. aspx

More information here: http://support.microsoft.com/kb/310153 and here: http://msdn.microsoft.com/en-us/library/ms997634.aspx