{"id":103416,"date":"2020-02-07T07:00:00","date_gmt":"2020-02-07T15:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103416"},"modified":"2020-02-07T06:42:02","modified_gmt":"2020-02-07T14:42:02","slug":"20200207-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200207-00\/?p=103416","title":{"rendered":"Psychic debugging: Why does my app sometimes fail to change the display settings?"},"content":{"rendered":"<p>A customer was having trouble with their Windows 7 Embedded system. (You can tell how old this story is because Windows 7 Embedded was still a thing.)<\/p>\n<p>Their app runs as part of the Startup group, and they found that if a field technician replaced the system&#8217;s screen, the resolution would sometimes reset to the default, because y&#8217;know, Plug and Play is like that sometimes. To fix this, they had their app call <code>Change\u00adDisplay\u00adSettings<\/code> when it starts up, to ensure that the screen resolution was what they wanted. This works great\u2026 most of the time. Sometimes, the call to <code>Change\u00adDisplay\u00adSettings<\/code> fails. The problem was intermittent, so debugging it was difficult.<\/p>\n<p>While searching for possible reasons why they couldn&#8217;t change the screen resolution, they discovered the <code>JOB\u00adOBJECT_<\/code><code>BASIC_<\/code><code>UI_<\/code><code>RESTRICTIONS<\/code> structure, and in particular the <code>JOB_<\/code><code>OBJECT_<\/code><code>UI\u00adLIMIT_<\/code><code>DISPLAYSETTINGS<\/code> flag which prevents processes in the job from calling <code>Change\u00adDisplay\u00adSettings<\/code>. They also remembered that <a href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20110817-00\/?p=9883\"> programs in the Startup group ran inside a job object<\/a>, and they put two and two together and concluded that Explorer was running their app in a job object that had blocked screen resolution changes.<\/p>\n<p>The customer had a few questions: Was their theory correct? If so, is there a way to escape the job object, or is there a policy or setting that can disable the job object outright?<\/p>\n<p>I&#8217;m going to answer the questions in the wrong order.<\/p>\n<p>Yes, there is a way to escape the job object, but it&#8217;s not obvious. Explorer does not enable <code>JOB_<\/code><code>OBJECT_<\/code><code>LIMIT_<\/code><code>BREAK\u00adAWAY_<\/code><code>OKAY<\/code> on the job. Was this due to oversight, or was it an intentional limit, but it really doesn&#8217;t matter because either way, <a href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20160329-00\/?p=93214\"> you have to deal with it<\/a>.<\/p>\n<p>Instead, you&#8217;ll have to use sneaky tricks to break away. I called out one of them in the original article: <a href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20110817-00\/?p=9883\"> Use a logon-triggered scheduled task<\/a>. Another one is mentioned in the documentation for job objects: <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/ProcThread\/job-objects\"> Child processes created with the WMI method <code>Win32.<\/code><code>Process.<\/code><code>Create<\/code> are not associated with the job<\/a>.<\/p>\n<p>Okay, next question: Is there a setting to disable the job object? Yes there is. As I noted in the original article, you can set the <code>Delay_<\/code><code>Sec<\/code> to zero to disable running Startup apps in a box.<\/p>\n<p>But the customer said that they&#8217;re running Windows 7, and the default value for <code>Delay_<\/code><code>Sec<\/code> is already zero on Windows 7, so the only way they could end up in a job object is if somebody changed the default setting. (Given that this is an embedded system, that&#8217;s possible, though I don&#8217;t see why anybody would want to enable it. Usually, embedded systems <i>disable<\/i> stuff.)<\/p>\n<p>Is their theory correct? Not really. While it&#8217;s true that Explorer can be asked to run Startup apps in a job object, it does not apply any UI restrictions on those apps. Apps in the Startup group are not blocked by the job object from changing the display settings.<\/p>\n<p>I have a different theory about why their app cannot change the display settings: In order to change the display settings, the app must be running on the current input desktop. My theory is that under sporadic conditions, the app either starts up too soon (before the Welcome screen has switched to the application desktop), or starts up too late (at which point the screen saver may have already started).<\/p>\n<p>Unfortunately, we never heard back from the customer, so I will never know whether my psychic powers were effective.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Maybe you&#8217;re in the box.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Maybe you&#8217;re in the box.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103416"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103416\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}