peterp Posted October 19, 2011 Posted October 19, 2011 Hello, I have run into a problem with a vb.net program I am developing, and was wondering if anyone could help. Background: The program runs as a service, which may be the root cause of my issue. I have found a couple of ways of getting the screen resolution, which all report the same thing. WMI version Private Sub getRes() Dim objWMIService, objComputer As Object objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}") For Each objComputer In objWMIService.InstancesOf("Win32_DisplayConfiguration") Utils.WriteToEventLog("WMIRes: " & objComputer.PelsWidth & "x" & objComputer.PelsHeight,EventLogEntryType.Information) Next End Sub Easy vb.net version private sub getRes() dim Height as Integer = my.computer.screen.bounds.height dim Width as Integer = my.computer.screen.bounds.width Utils.WriteToEventLog("Res: " & Width & "x" & Height, EventLogEntryType.Information) end Sub More comlplex vb.net version private sub getRes() for each device in my.computer.screen.allscreens height = device.bounds.height width = device.bounds.width ..... All very straight-forward. Except that it reports back 1024x768 when the real resolution is 1280x800 If I run the code in a new project with a windows form, it reports correctly. So, how can my service get the resolution of the screen of the device that it is running on?
PiqueABoo Posted October 21, 2011 Posted October 21, 2011 The program runs as a service, which may be the root cause of my issue Google "session 0 isolation" to find out why. There might be some trick or arcane dotNet call to get you around this, but I suspect you have to p/invoke the WTS API (for an example of the territory we're in run QWINSTA) to get the physical console user's session ID, and via that info about their session e.g. desktop dimensions. And if you're doing this because you want your service to do something on the user's desktop then the google I suggested should turn up typical methods. 1
peterp Posted October 24, 2011 Author Posted October 24, 2011 Yep, that looks like the reason. Cheers. I dont need to display anything on the screen, but just get the physical resolution in order to correctly select a file to copy. This must be done before user login is displayed :-( (Basically the desktop background is chosen from a set of backgrounds based on screen resolution. Windows 7 only supports certain resolutions and some of our machines dont fit those limited choices and so use the default image. This I wanted to change based on the actual res.) ie. defaultBackground.jpg background1024x1280.jpg background1280x800.jpg Windows 7 will detect and display 1024x768 no trouble, and other common ones, (1024x768 etc) However anything not on the list will get defaultbackground. 1280x800 isnt on the list, so I need to copy this file to defaultBackground as a service before it is used. However, becuase of this session 0 thing, it always thinks the screen is 1024x768 Annoying.
SYNACK Posted October 24, 2011 Posted October 24, 2011 You could dig through the system registry to look for the current default screen. how to read current screen resolution from registry? [Archive] - CodeGuru Forums could be a bit of work to figure out the active one though.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now