title: How to detect if logon screen or UAC is active
body: <p>I want to find a way to detect if logon screen or UAC is active within my service process. For example:</p>
  
  <ol>
  <li>the screen on which a user needs to choose account and type in password to log on.</li>
  <li>the screen on which a user needs to type in password after screensaver (locked screen?).</li>
  <li>when a UAC pops up.</li>
  </ol>
  
  <p>Right now I look through the session 1 and examine all the processes, if I could find logonui.exe, I thought either of the first two scenarios should happen. But in fact, this method can only detect 
  scenario 1. </p>
  
  <p><a href="http://en.wikipedia.org/wiki/Winlogon" rel="nofollow noreferrer">winlogon.exe</a> still exists after logon screen. So I can't use winlogon.exe as a search target.</p>
  
  <p>According to <a href="http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682573(v=vs.85).aspx" rel="nofollow noreferrer">this</a> link, I think if I can find the current active (input) desktop name, default, screensaver or winlogon, I would be able to solve my question. So I followed <a href="https://stackoverflow.com/questions/4260878/openinputdesktop-to-determine-secure-login-desktop">swatkat's</a> answer, and my code is like this </p>
  
  <pre><code>HWINSTA hs = GetProcessWindowStation();
  HWINSTA hwinsta = OpenWindowStation("winsta0", FALSE, READ_CONTROL | WRITE_DAC);
  SetProcessWindowStation(hwinsta);
  
  HDESK hd = OpenInputDesktop(0, TRUE, GENERIC_READ | DESKTOP_READOBJECTS);
  if (hd != NULL) {
      DWORD size;
      GetUserObjectInformation(hd, UOI_NAME, NULL, 0, &amp;size);
      TCHAR* name = (TCHAR*)alloca(size + sizeof(TCHAR));
      GetUserObjectInformation(hd, UOI_NAME, name, size, &amp;size);
      CString result(name);
      LOG((CLOG_DEBUG "input desktop name: %s %d", result.c_str(), size));
      CloseDesktop(hd);
  }
  else {
      LOG((CLOG_DEBUG "failed to call OpenInputDesktop"));
  }
  
  CloseWindowStation(hwinsta);
  SetProcessWindowStation(hs);
  </code></pre>
  
  <p>This seems to always return "default" no matter which screen it's on.</p>
  
  <p>I'm not really familiar with windows desktop and station, so if I did something wrong please let me know.</p>
  
  <p>Thanks in advance.
  Jerry </p>