{"id":642,"date":"2014-07-20T04:34:03","date_gmt":"2014-07-20T11:34:03","guid":{"rendered":"http:\/\/www.coretechnologies.com\/blog\/?p=642"},"modified":"2014-07-20T04:34:03","modified_gmt":"2014-07-20T11:34:03","slug":"running-autoit-session-0","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/miscellaneous\/running-autoit-session-0\/","title":{"rendered":"Tips for Running AutoIt in the Isolated Session 0"},"content":{"rendered":"<p><a href=\"\/blog\/images\/autoit-screenshot.png\" class=\"zoomPopup\" rel=\"zoomgroup noopener noreferrer\" title=\"AutoIt\" target=\"_blank\"><br \/>\n<img decoding=\"async\" src=\"\/blog\/images\/autoit-screenshot.png\" border=\"0\" title=\"AutoIt (click to enlarge)\" alt=\"The isolated Session 0 desktop\" width=\"520\" \/><br \/>\n<\/a><\/p>\n<p>\nStarting with Windows Vista in 2007 and continuing with every subsequent release, Microsoft has banned users from logging in to the first session created as Windows boots. This <a href=\"\/WindowsServices\/FAQ.html#WhatIsSession0Isolation\" target=\"_blank\" rel=\"noopener noreferrer\">&#8220;Session 0 Isolation&#8221;<\/a> has been accompanied by a steady erosion of the interactive capabilities of Session 0, rendering the Session 0 desktop virtually unrecognizable in the post-XP world. The prevailing wisdom in Redmond: Why support a proper working desktop in Session 0 when no one in their right mind is expected to use it?\n<\/p>\n<p><a href=\"\/blog\/images\/session-0-desktop.png\" class=\"zoomPopup\" rel=\"zoomgroup noopener noreferrer\" title=\"The isolated Session 0 desktop\" target=\"_blank\"><br \/>\n<img decoding=\"async\" src=\"\/blog\/images\/session-0-desktop.png\" border=\"0\" title=\"The isolated Session 0 desktop (click to enlarge)\" alt=\"The isolated Session 0 desktop\" width=\"520\" \/><br \/>\n<\/a><\/p>\n<p>\nUnfortunately those of us working with interactive applications and Windows Services in Session 0 don&#8217;t have the luxury of ignoring GUI-related issues. And users of the <a href=\"http:\/\/www.autoitscript.com\/site\/autoit\/\" target=\"_blank\" rel=\"noopener noreferrer\">excellent AutoIt automation toolkit<\/a> have to make special accommodations when creating scripts to click buttons and fill in forms in Session 0. Here is what we have learned while troubleshooting AutoIt with our customers:\n<\/p>\n<ol>\n<li>\n<h2><b>WinActivate, WinWaitActive and WinActive Often Fail<\/b><\/h2>\n<p>\nEssential AutoIt functions like <a href=\"https:\/\/www.autoitscript.com\/autoit3\/docs\/functions\/WinActivate.htm\" target=\"_blank\" rel=\"noopener noreferrer\">WinActivate<\/a>, <a href=\"https:\/\/www.autoitscript.com\/autoit3\/docs\/functions\/WinWaitActive.htm\" target=\"_blank\" rel=\"noopener noreferrer\">WinWaitActive<\/a> and <a href=\"https:\/\/www.autoitscript.com\/autoit3\/docs\/functions\/WinActive.htm\" target=\"_blank\" rel=\"noopener noreferrer\">WinActive<\/a> rely on the operating system to track the active window. Unfortunately it seems that the active window is not tracked in Session 0 unless a user is actively viewing the desktop! Therefore a script that takes some action only if a window has been activated may never do its work. In this sample code, <a href=\"http:\/\/www.autoitscript.com\/forum\/topic\/45982-winactive-always-returns-0-when-running-script-under-windows-service-manager\/\" target=\"_blank\" rel=\"noopener noreferrer\">WinActivate will always return 0<\/a> when run in an unattended Session 0 and the button will never be clicked:\n<\/p>\n<pre style=\"padding:8px; background-color:#F4F4F4\">\n; Activate  the window and click the button.\nif (WinActivate($windowName)) Then\n\t; Click the button on the window.\n\tControlClick($windowName, $buttonName, \"\")\nEndIf\n<\/pre>\n<p>\n<b>Our advice:<\/b> Don&#8217;t depend on the Win* functions to find and activate a window. Simply operate on your window regardless of its active state. For example, restructure the code above to look like this:\n<\/p>\n<pre style=\"padding:8px; background-color:#F4F4F4\">\n; Try to activate  the window if possible. May fail in Session 0.\nWinActivate($windowName)\n; Click the button on the window.\nControlClick($windowName, $buttonName, \"\")\n<\/pre>\n<\/li>\n<li>\n<h2><b>The Mouse Functions (MouseMove, MouseClick, etc.) Don&#8217;t Work<\/b><\/h2>\n<p>\nAs with window activation, it seems that the mouse is not tracked when session 0 is unattended. Functions that move and manipulate the mouse can fail unexpectedly.\n<\/p>\n<p>\n<b>Our advice:<\/b> Don&#8217;t use the mouse functions in your scripts.<\/b>\n<\/p>\n<\/li>\n<li>\n<h2><b>AutoIt Scripts created with Au3Record Are Unreliable<\/b><\/h2>\n<p>\nThe <a href=\"\">Au3Record utility<\/a> bundled with AutoIt will easily record a set of actions for later replay. It captures mouse movements, mouse clicks, key presses and more. However the script created makes heavy use of the problematic activation and mouse related functions cited above, thus rendering Au3Record scripts virtually useless in Session 0.<\/p>\n<p>\n<b>Our advice:<\/b> Don&#8217;t use scripts created by Au3Record in Session 0.<\/b>\n<\/p>\n<\/li>\n<li>\n<h2><b>You can Rely on ControlSend and ControlClick in Session 0<\/b><\/h2>\n<p>\nFortunately functions like <a href=\"https:\/\/www.autoitscript.com\/autoit3\/docs\/functions\/ControlSend.htm\" target=\"_blank\" rel=\"noopener noreferrer\">ControlSend<\/a> and <a href=\"https:\/\/www.autoitscript.com\/autoit3\/docs\/functions\/ControlClick.htm\" target=\"_blank\" rel=\"noopener noreferrer\">ControlClick<\/a> continue to work as expected in Session 0. Neither of them relies on the target window being active nor the mouse being in a specific position, so they sidestep trouble related to those areas on the isolated desktop.\n<\/p>\n<\/li>\n<\/ol>\n<p><\/p>\n<p>\nAre you using AutoIt in the isolated Session 0? Have you run into other functionality that does not work as expected? Please let us know what you have found! Your feedback will be much appreciated.<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/interact-with-desktop\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: Why doesn&#039;t Allow service to interact with desktop work?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/qa-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: Why doesn&#039;t &quot;Allow service to interact with desktop&quot; work?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows\/is-auto-logon-safe\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Is it Safe to Automatically Log In to my Computer?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/login-password-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Is it Safe to Automatically Log In to my Computer?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/vs-custom-windows-service\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Why Should I Buy AlwaysUp Instead of Writing My Own Windows Service?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/build-vs-buy-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Why Should I Buy AlwaysUp Instead of Writing My Own Windows Service?<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>Starting with Windows Vista in 2007 and continuing with every subsequent release, Microsoft has banned users from logging in to the first session created as Windows boots. This &#8220;Session 0 Isolation&#8221; has been accompanied by a steady erosion of the &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/miscellaneous\/running-autoit-session-0\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[35,40,153,196],"class_list":["post-642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-miscellaneous","tag-autoit","tag-automation","tag-session-0-isolation","tag-windows-services-2"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":0,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/2253"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}