Windows 服务的执行上下文

发布于 2024-08-12 00:20:58 字数 465 浏览 1 评论 0原文

我用 C# 编写了一个 Windows 服务,它本质上是侦听特定端口上的命令,并且在大多数情况下将创建一个新的 Process 类,并且 shell 执行所述任务。

我主要想用它来启动媒体。例如,我有一个名为 c:\media.avi 的媒体文件 ->我从远程客户端发送“-launch c:\media.avi”,服务器在进程类中启动它。简单的!

我遇到的问题是,当服务在 localsystem 上下文下运行时,avi 将在其本机应用程序(例如 Windows Media Player)中运行,但是根本没有声音。

在我的用户帐户上下文下尝试相同的过程[提供 UN 和 PW 服务] 运行带有声音的 .avi 文件,但我根本看不到该窗口。

有谁知道如何让该服务在当前登录的用户的上下文中运行 - 以便我可以获得声音和视频?

I have written a windows service in C# that essentially listens for commands on a specific port and in most cases will create a new Process class and shell execute said task.

I primarily want to use it to launch media. E.g. I have a media file called c:\media.avi -> from a remote client I send "-launch c:\media.avi" and the server launches it in a process class. Easy!

The problem that I am having is that when the service runs under the localsystem context, the avi will run in its native application, say, windows media player BUT, there is no sound at all.

Trying the same process under my user account context [provided UN and PW to service] runs the .avi file WITH sound but I cant see the window at all.

Does anyone know how I can get the service to run under the context of the user that is currently logged on - so that I can get sounds and video?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-08-19 00:20:58

您正在描述一个应用程序。服务和应用程序之间的根本区别在于服务不在用户会话下运行,也不应与其交互。如果您的目标是在用户会话中启动一个进程,为什么您首先选择创建服务?

服务与会话交互的推荐方法是拥有一个专用的交互式应用程序,该应用程序从用户会话运行并通过 IPC 协议(命名管道、共享内存、套接字等)与服务交互。您可以让您的服务侦听命令,并在收到命令时将所述命令分派到从连接到该服务的感兴趣会话运行的交互式应用程序。

You are describing an application. The fundamental difference between a service and an application is that services don't run under a user session and should not interact with it. Why did you choose to create a service in the first place, if your goal was to start a process in the user session?

The recommended way for services to interact with the session is to have a dedicate interactive application that runs from the user session and interacts with the service via an IPC protocol (named pipes, shared memory, sockets etc). You could have your service listen for commands and, when received, dispatch the said commands to the interactive applications running from the interested sessions that are connected to the service.

合久必婚 2024-08-19 00:20:58

尝试更改服务设置以启用“允许服务与桌面交互”或适用于您的操作系统版本的等效设置。但请注意,这是不好的做法;服务通常不应该与 GUI 交互,而且这种能力(理论上)将在 Windows 的未来版本中消失。

还有其他选择; 本文介绍了一种更简洁的方法用于与用户会话交互的服务。不过,实际上,请考虑一个应用程序。

Try changing the service settings to enable "Allow service to interact with desktop", or equivalent for your OS version. Be aware, though, that this is bad practice; services should not generally interact with the GUI, and the ability to do so will (theoretically) be going away in a future version of Windows.

There are alternatives; this article describes a cleaner way for a service to interact with a user session. Really, though, consider an application instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文