def openWindow(): ''' Attempts to open a window on the Winlogon desktop. This can also be used to just test opening a window on the Application desktop. NOTE: The Winlogon desktop part is not currently working with this example installed as an interactive service. It may be necessary instead to set this up as something started by a Winlogon Notification Package. ''' logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=r'C:\temp\myapp.log', filemode='w') logging.debug("Starting") cur_winsta = win32service.GetProcessWindowStation() logging.debug("Got process window station") cur_desktop = win32service.GetThreadDesktop(win32api.GetCurrentThreadId()) logging.debug("Got current desktop") try: new_winsta = win32service.OpenWindowStation( "winsta0", False, win32con.WINSTA_ACCESSCLIPBOARD | win32con.WINSTA_ACCESSGLOBALATOMS | win32con.WINSTA_CREATEDESKTOP | win32con.WINSTA_ENUMDESKTOPS | win32con.WINSTA_ENUMERATE | win32con.WINSTA_EXITWINDOWS | win32con.WINSTA_READATTRIBUTES | win32con.WINSTA_READSCREEN | win32con.WINSTA_WRITEATTRIBUTES) new_winsta.SetProcessWindowStation() desktop = win32service.OpenDesktop( "Winlogon", 0, False, win32con.DESKTOP_CREATEMENU | win32con.DESKTOP_CREATEWINDOW | win32con.DESKTOP_ENUMERATE | win32con.DESKTOP_HOOKCONTROL | win32con.DESKTOP_JOURNALPLAYBACK | win32con.DESKTOP_JOURNALRECORD | win32con.DESKTOP_READOBJECTS | win32con.DESKTOP_SWITCHDESKTOP | win32con.DESKTOP_WRITEOBJECTS) desktop.SetThreadDesktop() logging.debug("Running calculator") os.system("calc") logging.debug("Done") except: logging.debug("Caught exception:") logging.debug(sys.exc_info()[0]) # Print the exception type logging.debug(sys.exc_info()[1]) # Print the exception info cur_winsta.SetProcessWindowStation() cur_desktop.SetThreadDesktop() cur_desktop.CloseDesktop() cur_winsta.CloseWindowStation() if new_winsta is not None: new_winsta.CloseWindowStation() if desktop is not None: desktop.CloseDesktop()
def grant_winsta_and_desktop(th): ''' Grant the token's user access to the current process's window station and desktop. ''' current_sid = win32security.GetTokenInformation(th, win32security.TokenUser)[0] # Add permissions for the sid to the current windows station and thread id. # This prevents windows error 0xC0000142. winsta = win32process.GetProcessWindowStation() set_user_perm(winsta, WINSTA_ALL, current_sid) desktop = win32service.GetThreadDesktop(win32api.GetCurrentThreadId()) set_user_perm(desktop, DESKTOP_ALL, current_sid)
def _CreateAltDesktop(window_station, name: str): """Creates an alternate desktop. Args: window_station: Handle to window station. name: Name of the desktop. Returns: A handle to the alternate desktop. """ current_desktop = win32service.GetThreadDesktop( win32api.GetCurrentThreadId()) security_attributes = _GetSecurityAttributes(current_desktop) current_station = win32service.GetProcessWindowStation() window_station.SetProcessWindowStation() desktop = win32service.CreateDesktop( name, 0, (DESKTOP_CREATEWINDOW | DESKTOP_READOBJECTS | ntsecuritycon.READ_CONTROL | ntsecuritycon.WRITE_DAC | ntsecuritycon.WRITE_OWNER), security_attributes) current_station.SetProcessWindowStation() return desktop