Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

c# - Get Desktop Size from Windows Service?

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.

In WinForms -- the standard C# method of:

SystemInformation.VirtualScreen.Width   
SystemInformation.VirtualScreen.Height

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.

Any thoughts?

Edit:

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

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.)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...