从Windows服务获取桌面大小? [英] Get Desktop Size from Windows Service?

查看:48
本文介绍了从Windows服务获取桌面大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我编写的服务内部获取Windows桌面的大小(整个东西,而不仅仅是一个屏幕).

I'm trying to get the size of the Windows' desktop (the whole thing, not just a single screen) from inside a service that I've written.

在WinForms中-的标准C#方法:

In WinForms -- the standard C# method of:

SystemInformation.VirtualScreen.Width   
SystemInformation.VirtualScreen.Height

似乎可以正常工作(如果您导入Winforms DLL,我想避免)-但是它返回了错误的值.桌面大小为 2048x768 (2个屏幕),但是该服务报告 1024x768 (大概只在其中一个屏幕上显示).

seems to work (if you import the Winforms DLL, which I want to avoid) -- but it returns the wrong value. The desktop size is 2048x768 (2 screens), but the service reports 1024x768 (presumably it only picks up on one of the screens.)

检查服务与桌面交互的选项无效.

Checking the option for the service to interact with the desktop has no effect.

有什么想法吗?

发布在 C#上的解决方案:无法获得完整的桌面尺寸?不起作用服务内部.他们都报告了错误的值.

The solutions posted at C#: Get complete desktop size? don't work inside of a service. They all report the wrong value.

有趣的是,报告的值似乎有所不同,并且与实际的桌面大小无关(某些计算机报告 800x600 ,即使该计算机上的单个显示器分辨率更高).)

Interestingly, it seems like the value that is reported varies and is of no relation to the actual desktop size (some machines report 800x600 even though a single display on that machine is a much higher resolution.)

那么-还有更多想法吗?可以进入注册表和/或命令行.唯一的限制是我无法启动winforms应用来解决该问题.

So -- any more ideas? Dropping into the registry and/or to the command line is OK. The only restriction is that I can't launch a winforms app to figure it out.

推荐答案

您不能在服务中执行此操作.服务在会话0中运行,该会话中没有GDI功能.创建过程后,您将无法更改会话,也不能在其他会话中使用UI.

You cannot do this inside the service. Services run in session 0, where no GDI functions work. Once the process is created, you cannot change sessions, and cannot use UI in different session.

为您提供的一种可能的解决方案是在用户会话中启动一个新进程.您可以开始查看此 SO问题.此方法的其他先决条件是您的服务必须作为本地系统运行,以便可以启用SE_TCB_NAME特权(通过调用AdjustTokenPrivilegies).因为您说的是您已经挂接到用户登录通知,所以您应该能够提取您感兴趣的会话的会话ID.

One of the possible solutions for you is to launch a new process in user session. You can start looking at this SO question. The other pre-requisites for this method is that your service has to be running as Local System, so that you can enable SE_TCB_NAME privilege (by calling AdjustTokenPrivilegies). Since you are saying you already hooked up to the user logon notification, you should be able to extract session ID of the session that you are interested in.

一旦在用户会话中启动了某个流程,就必须将结果从新流程传递回服务流程.为此,可以使用任何一种IPC机制.

Once you have a process lauched in user session, you have to pass the result from the new process back to your service process. For that any kind of IPC mechanism could be used.

这篇关于从Windows服务获取桌面大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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