向由不同 Windows 用户会话启动的应用程序发送消息 [英] Sending a message to an application started by a different windows' user session

查看:28
本文介绍了向由不同 Windows 用户会话启动的应用程序发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个桌面应用程序(用 Delphi 制作)由用户 A 启动.我们称之为实例 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

这篇关于向由不同 Windows 用户会话启动的应用程序发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆