Skip to content

Instantly share code, notes, and snippets.

@akx
Created July 21, 2013 12:58
Show Gist options
  • Save akx/6048485 to your computer and use it in GitHub Desktop.
Save akx/6048485 to your computer and use it in GitHub Desktop.
import ctypes, win32con, functools
U = ctypes.windll.user32
EnumWindows = U.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = U.GetWindowTextW
RealGetWindowClass = U.RealGetWindowClass
IsWindowVisible = U.IsWindowVisible
PostMessage = U.PostMessageW
WM_CLOSE = 16
ubuf = ctypes.create_unicode_buffer(510)
sbuf = ctypes.create_string_buffer(510)
def foreach_window(callback, hwnd, lParam):
if IsWindowVisible(hwnd):
GetWindowText(hwnd, ubuf, 512)
title = ubuf.value
RealGetWindowClass(hwnd, sbuf, 512)
klass = sbuf.value
callback((hwnd, title, klass))
return True
def close_explorer((hwnd, title, klass)):
if klass == "CabinetWClass":
print "Closing", title
PostMessage(hwnd, WM_CLOSE, 0, 0)
def main():
EnumWindows(EnumWindowsProc(functools.partial(foreach_window, close_explorer)), 0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment