1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 """
29 Debugging API wrappers in ctypes.
30
31 @see: U{http://apps.sourceforge.net/trac/winappdbg/wiki/Win32APIWrappers}
32 """
33
34 __revision__ = "$Id: user32.py 437 2009-11-08 10:10:51Z qvasimodo $"
35
36 from defines import *
37
38
39
40
41 GWL_WNDPROC = -4
42 GWL_HINSTANCE = -6
43 GWL_HWNDPARENT = -8
44 GWL_STYLE = -16
45 GWL_EXSTYLE = -20
46 GWL_USERDATA = -21
47 GWL_ID = -12
48
49
50 SW_HIDE = 0
51 SW_SHOWNORMAL = 1
52 SW_NORMAL = 1
53 SW_SHOWMINIMIZED = 2
54 SW_SHOWMAXIMIZED = 3
55 SW_MAXIMIZE = 3
56 SW_SHOWNOACTIVATE = 4
57 SW_SHOW = 5
58 SW_MINIMIZE = 6
59 SW_SHOWMINNOACTIVE = 7
60 SW_SHOWNA = 8
61 SW_RESTORE = 9
62 SW_SHOWDEFAULT = 10
63 SW_FORCEMINIMIZE = 11
64
65
66 SMTO_NORMAL = 0
67 SMTO_BLOCK = 1
68 SMTO_ABORTIFHUNG = 2
69 SMTO_NOTIMEOUTIFNOTHUNG = 8
70 SMTO_ERRORONEXIT = 0x20
71
72
73
74 WM_USER = 0x400
75 WM_NULL = 0
76 WM_CREATE = 1
77 WM_DESTROY = 2
78 WM_MOVE = 3
79 WM_SIZE = 5
80 WM_ACTIVATE = 6
81 WA_INACTIVE = 0
82 WA_ACTIVE = 1
83 WA_CLICKACTIVE = 2
84 WM_SETFOCUS = 7
85 WM_KILLFOCUS = 8
86 WM_ENABLE = 0x0A
87 WM_SETREDRAW = 0x0B
88 WM_SETTEXT = 0x0C
89 WM_GETTEXT = 0x0D
90 WM_GETTEXTLENGTH = 0x0E
91 WM_PAINT = 0x0F
92 WM_CLOSE = 0x10
93 WM_QUERYENDSESSION = 0x11
94 WM_QUIT = 0x12
95 WM_QUERYOPEN = 0x13
96 WM_ERASEBKGND = 0x14
97 WM_SYSCOLORCHANGE = 0x15
98 WM_ENDSESSION = 0x16
99 WM_SHOWWINDOW = 0x18
100 WM_WININICHANGE = 0x1A
101 WM_SETTINGCHANGE = WM_WININICHANGE
102 WM_DEVMODECHANGE = 0x1B
103 WM_ACTIVATEAPP = 0x1C
104 WM_FONTCHANGE = 0x1D
105 WM_TIMECHANGE = 0x1E
106 WM_CANCELMODE = 0x1F
107 WM_SETCURSOR = 0x20
108 WM_MOUSEACTIVATE = 0x21
109 WM_CHILDACTIVATE = 0x22
110 WM_QUEUESYNC = 0x23
111 WM_GETMINMAXINFO = 0x24
112 WM_PAINTICON = 0x26
113 WM_ICONERASEBKGND = 0x27
114 WM_NEXTDLGCTL = 0x28
115 WM_SPOOLERSTATUS = 0x2A
116 WM_DRAWITEM = 0x2B
117 WM_MEASUREITEM = 0x2C
118 WM_DELETEITEM = 0x2D
119 WM_VKEYTOITEM = 0x2E
120 WM_CHARTOITEM = 0x2F
121 WM_SETFONT = 0x30
122 WM_GETFONT = 0x31
123 WM_SETHOTKEY = 0x32
124 WM_GETHOTKEY = 0x33
125 WM_QUERYDRAGICON = 0x37
126 WM_COMPAREITEM = 0x39
127 WM_GETOBJECT = 0x3D
128 WM_COMPACTING = 0x41
129 WM_OTHERWINDOWCREATED = 0x42
130 WM_OTHERWINDOWDESTROYED = 0x43
131 WM_COMMNOTIFY = 0x44
132 CN_RECEIVE = 0x1
133 CN_TRANSMIT = 0x2
134 CN_EVENT = 0x4
135 WM_WINDOWPOSCHANGING = 0x46
136 WM_WINDOWPOSCHANGED = 0x47
137 WM_POWER = 0x48
138 PWR_OK = 1
139 PWR_FAIL = -1
140 PWR_SUSPENDREQUEST = 1
141 PWR_SUSPENDRESUME = 2
142 PWR_CRITICALRESUME = 3
143 WM_COPYDATA = 0x4A
144 WM_CANCELJOURNAL = 0x4B
145 WM_NOTIFY = 0x4E
146 WM_INPUTLANGCHANGEREQUEST = 0x50
147 WM_INPUTLANGCHANGE = 0x51
148 WM_TCARD = 0x52
149 WM_HELP = 0x53
150 WM_USERCHANGED = 0x54
151 WM_NOTIFYFORMAT = 0x55
152 WM_CONTEXTMENU = 0x7B
153 WM_STYLECHANGING = 0x7C
154 WM_STYLECHANGED = 0x7D
155 WM_DISPLAYCHANGE = 0x7E
156 WM_GETICON = 0x7F
157 WM_SETICON = 0x80
158 WM_NCCREATE = 0x81
159 WM_NCDESTROY = 0x82
160 WM_NCCALCSIZE = 0x83
161 WM_NCHITTEST = 0x84
162 WM_NCPAINT = 0x85
163 WM_NCACTIVATE = 0x86
164 WM_GETDLGCODE = 0x87
165 WM_SYNCPAINT = 0x88
166 WM_NCMOUSEMOVE = 0x0A0
167 WM_NCLBUTTONDOWN = 0x0A1
168 WM_NCLBUTTONUP = 0x0A2
169 WM_NCLBUTTONDBLCLK = 0x0A3
170 WM_NCRBUTTONDOWN = 0x0A4
171 WM_NCRBUTTONUP = 0x0A5
172 WM_NCRBUTTONDBLCLK = 0x0A6
173 WM_NCMBUTTONDOWN = 0x0A7
174 WM_NCMBUTTONUP = 0x0A8
175 WM_NCMBUTTONDBLCLK = 0x0A9
176 WM_KEYFIRST = 0x100
177 WM_KEYDOWN = 0x100
178 WM_KEYUP = 0x101
179 WM_CHAR = 0x102
180 WM_DEADCHAR = 0x103
181 WM_SYSKEYDOWN = 0x104
182 WM_SYSKEYUP = 0x105
183 WM_SYSCHAR = 0x106
184 WM_SYSDEADCHAR = 0x107
185 WM_KEYLAST = 0x108
186 WM_INITDIALOG = 0x110
187 WM_COMMAND = 0x111
188 WM_SYSCOMMAND = 0x112
189 WM_TIMER = 0x113
190 WM_HSCROLL = 0x114
191 WM_VSCROLL = 0x115
192 WM_INITMENU = 0x116
193 WM_INITMENUPOPUP = 0x117
194 WM_MENUSELECT = 0x11F
195 WM_MENUCHAR = 0x120
196 WM_ENTERIDLE = 0x121
197 WM_CTLCOLORMSGBOX = 0x132
198 WM_CTLCOLOREDIT = 0x133
199 WM_CTLCOLORLISTBOX = 0x134
200 WM_CTLCOLORBTN = 0x135
201 WM_CTLCOLORDLG = 0x136
202 WM_CTLCOLORSCROLLBAR = 0x137
203 WM_CTLCOLORSTATIC = 0x138
204 WM_MOUSEFIRST = 0x200
205 WM_MOUSEMOVE = 0x200
206 WM_LBUTTONDOWN = 0x201
207 WM_LBUTTONUP = 0x202
208 WM_LBUTTONDBLCLK = 0x203
209 WM_RBUTTONDOWN = 0x204
210 WM_RBUTTONUP = 0x205
211 WM_RBUTTONDBLCLK = 0x206
212 WM_MBUTTONDOWN = 0x207
213 WM_MBUTTONUP = 0x208
214 WM_MBUTTONDBLCLK = 0x209
215 WM_MOUSELAST = 0x209
216 WM_PARENTNOTIFY = 0x210
217 WM_ENTERMENULOOP = 0x211
218 WM_EXITMENULOOP = 0x212
219 WM_MDICREATE = 0x220
220 WM_MDIDESTROY = 0x221
221 WM_MDIACTIVATE = 0x222
222 WM_MDIRESTORE = 0x223
223 WM_MDINEXT = 0x224
224 WM_MDIMAXIMIZE = 0x225
225 WM_MDITILE = 0x226
226 WM_MDICASCADE = 0x227
227 WM_MDIICONARRANGE = 0x228
228 WM_MDIGETACTIVE = 0x229
229 WM_MDISETMENU = 0x230
230 WM_DROPFILES = 0x233
231 WM_MDIREFRESHMENU = 0x234
232 WM_CUT = 0x300
233 WM_COPY = 0x301
234 WM_PASTE = 0x302
235 WM_CLEAR = 0x303
236 WM_UNDO = 0x304
237 WM_RENDERFORMAT = 0x305
238 WM_RENDERALLFORMATS = 0x306
239 WM_DESTROYCLIPBOARD = 0x307
240 WM_DRAWCLIPBOARD = 0x308
241 WM_PAINTCLIPBOARD = 0x309
242 WM_VSCROLLCLIPBOARD = 0x30A
243 WM_SIZECLIPBOARD = 0x30B
244 WM_ASKCBFORMATNAME = 0x30C
245 WM_CHANGECBCHAIN = 0x30D
246 WM_HSCROLLCLIPBOARD = 0x30E
247 WM_QUERYNEWPALETTE = 0x30F
248 WM_PALETTEISCHANGING = 0x310
249 WM_PALETTECHANGED = 0x311
250 WM_HOTKEY = 0x312
251 WM_PRINT = 0x317
252 WM_PRINTCLIENT = 0x318
253 WM_PENWINFIRST = 0x380
254 WM_PENWINLAST = 0x38F
255
256
257
258
263 self.hwnd.append(hwnd.value)
264 return TRUE
265
266 WNDENUMPROC = WINFUNCTYPE(BOOL, HWND, PVOID)
267
268
269
270
271
272 -def FindWindowA(lpClassName = None, lpWindowName = None):
273 _FindWindowA = windll.user32.FindWindowA
274 _FindWindowA.argtypes = [LPSTR, LPSTR]
275 _FindWindowA.restype = HWND
276
277 if not lpClassName:
278 lpClassName = None
279 if not lpWindowName:
280 lpWindowName = None
281 hWnd = _FindWindowA(lpClassName, lpWindowName)
282 hWnd = hWnd.value
283 if hWnd == NULL:
284 raise ctypes.WinError()
285 return hWnd
286
287 -def FindWindowW(lpClassName = None, lpWindowName = None):
288 _FindWindowW = windll.user32.FindWindowW
289 _FindWindowW.argtypes = [LPSTR, LPSTR]
290 _FindWindowW.restype = HWND
291
292 if not lpClassName:
293 lpClassName = None
294 if not lpWindowName:
295 lpWindowName = None
296 hWnd = _FindWindowW(lpClassName, lpWindowName)
297 hWnd = hWnd.value
298 if hWnd == NULL:
299 raise ctypes.WinError()
300 return hWnd
301
302 FindWindow = GuessStringType(FindWindowA, FindWindowW)
303
304
305
306
307
308
310 _GetClassNameA = windll.user32.GetClassNameA
311 _GetClassNameA.argtypes = [HWND, LPSTR, ctypes.c_int]
312 _GetClassNameA.restype = ctypes.c_int
313
314 nMaxCount = 0x1000
315 dwCharSize = sizeof(CHAR)
316 while 1:
317 lpClassName = ctypes.create_string_buffer("", nMaxCount)
318 nCount = _GetClassNameA(hWnd, lpClassName, nMaxCount)
319 if nCount == 0:
320 raise ctypes.WinError()
321 if nCount < nMaxCount - dwCharSize:
322 break
323 nMaxCount += 0x1000
324 return lpClassName.value
325
327 _GetClassNameW = windll.user32.GetClassNameW
328 _GetClassNameW.argtypes = [HWND, LPWSTR, ctypes.c_int]
329 _GetClassNameW.restype = ctypes.c_int
330
331 nMaxCount = 0x1000
332 dwCharSize = sizeof(WCHAR)
333 while 1:
334 lpClassName = ctypes.create_unicode_buffer(u"", nMaxCount)
335 nCount = _GetClassNameW(hWnd, lpClassName, nMaxCount)
336 if nCount == 0:
337 raise ctypes.WinError()
338 if nCount < nMaxCount - dwCharSize:
339 break
340 nMaxCount += 0x1000
341 return lpClassName.value
342
343 GetClassName = GuessStringType(GetClassNameA, GetClassNameW)
344
345
346
347
348
350 _GetWindowLongA = windll.user32.GetWindowLongA
351 _GetWindowLongA.argtypes = [HWND, ctypes.c_int]
352 _GetWindowLongA.restype = LONG
353 return _GetWindowLongA(hWnd, nIndex)
354
356 _GetWindowLongW = windll.user32.GetWindowLongW
357 _GetWindowLongW.argtypes = [HWND, ctypes.c_int]
358 _GetWindowLongW.restype = LONG
359 return _GetWindowLongW(hWnd, nIndex)
360
361
362 GetWindowLong = GetWindowLongA
363
364
366 _GetShellWindow = windll.user32.GetShellWindow
367 _GetShellWindow.argtypes = []
368 _GetShellWindow.restype = HWND
369 _GetShellWindow.errcheck = RaiseIfZero
370 return _GetShellWindow()
371
372
373
374
375
386
387
388
389
391 _GetParent = windll.user32.GetParent
392 _GetParent.argtypes = [HWND]
393 _GetParent.restype = HWND
394
395 hWndParent = _GetParent(hWnd)
396 hWndParent = hWndParent.value
397 if hWndParent == NULL:
398 winerr = GetLastError()
399 if winerr != ERROR_SUCCESS:
400 raise ctypes.WinError(winerr)
401 return hWndParent
402
403
404
405
406
408 _EnableWindow = windll.user32.EnableWindow
409 _EnableWindow.argtypes = [HWND, BOOL]
410 _EnableWindow.restype = bool
411 return _EnableWindow(hWnd, bool(bEnable))
412
413
414
415
416
418 _ShowWindow = windll.user32.ShowWindow
419 _ShowWindow.argtypes = [HWND, ctypes.c_int]
420 _ShowWindow.restype = bool
421 return _ShowWindow(hWnd, nCmdShow)
422
423
424
425
426
428 _ShowWindowAsync = windll.user32.ShowWindowAsync
429 _ShowWindowAsync.argtypes = [HWND, ctypes.c_int]
430 _ShowWindowAsync.restype = bool
431 return _ShowWindowAsync(hWnd, nCmdShow)
432
433
434
435
436
439
440
441
442
443
445 _EnumWindows = windll.user32.EnumWindows
446 _EnumWindows.argtypes = [WNDENUMPROC, LPARAM]
447 _EnumWindows.restype = bool
448
449 EnumFunc = __EnumWndProc()
450 lpEnumFunc = WNDENUMPROC(EnumFunc)
451 if not _EnumWindows(lpEnumFunc, NULL):
452 errcode = GetLastError()
453 if errcode != ERROR_NO_MORE_FILES:
454 raise ctypes.WinError(errcode)
455 return EnumFunc.hwnd
456
457
458
459
460
463
464
465
466
467
468
481
482
483
484
485
488
489
490
491
492
493
495 _EnumChildWindows = windll.user32.EnumChildWindows
496 _EnumChildWindows.argtypes = [HWND, WNDENUMPROC, LPARAM]
497 _EnumChildWindows.restype = bool
498
499 EnumFunc = __EnumChildProc()
500 lpEnumFunc = WNDENUMPROC(EnumFunc)
501 if not _EnumChildWindows(hWndParent, lpEnumFunc, NULL):
502 errcode = GetLastError()
503 if errcode != ERROR_NO_MORE_FILES:
504 raise ctypes.WinError(errcode)
505 return EnumFunc.hwnd
506
507
508
509
510
511
512
514 _SendMessageA = windll.user32.SendMessageA
515 _SendMessageA.argtypes = [HWND, UINT, WPARAM, LPARAM]
516 _SendMessageA.restype = LRESULT
517 return _SendMessageA(hWnd, Msg, wParam, lParam)
518
520 _SendMessageW = windll.user32.SendMessageW
521 _SendMessageW.argtypes = [HWND, UINT, WPARAM, LPARAM]
522 _SendMessageW.restype = LRESULT
523 return _SendMessageW(hWnd, Msg, wParam, lParam)
524
525 SendMessage = GuessStringType(SendMessageA, SendMessageW)
526
527
528
529
530
531
532
533 -def PostMessageA(hWnd, Msg, wParam = 0, lParam = 0):
534 _PostMessageA = windll.user32.PostMessageA
535 _PostMessageA.argtypes = [HWND, UINT, WPARAM, LPARAM]
536 _PostMessageA.restype = bool
537 _PostMessageA.errcheck = RaiseIfZero
538 _PostMessageA(hWnd, Msg, wParam, lParam)
539
540 -def PostMessageW(hWnd, Msg, wParam = 0, lParam = 0):
541 _PostMessageW = windll.user32.PostMessageW
542 _PostMessageW.argtypes = [HWND, UINT, WPARAM, LPARAM]
543 _PostMessageW.restype = bool
544 _PostMessageW.errcheck = RaiseIfZero
545 _PostMessageW(hWnd, Msg, wParam, lParam)
546
547 PostMessage = GuessStringType(PostMessageA, PostMessageW)
548
549
550
551
552
553
554
555 -def PostThreadMessageA(idThread, Msg, wParam = 0, lParam = 0):
556 _PostThreadMessageA = windll.user32.PostThreadMessageA
557 _PostThreadMessageA.argtypes = [DWORD, UINT, WPARAM, LPARAM]
558 _PostThreadMessageA.restype = bool
559 _PostThreadMessageA.errcheck = RaiseIfZero
560 _PostThreadMessageA(idThread, Msg, wParam, lParam)
561
562 -def PostThreadMessageW(idThread, Msg, wParam = 0, lParam = 0):
563 _PostThreadMessageW = windll.user32.PostThreadMessageW
564 _PostThreadMessageW.argtypes = [DWORD, UINT, WPARAM, LPARAM]
565 _PostThreadMessageW.restype = bool
566 _PostThreadMessageW.errcheck = RaiseIfZero
567 _PostThreadMessageW(idThread, Msg, wParam, lParam)
568
569 PostThreadMessage = GuessStringType(PostThreadMessageA, PostThreadMessageW)
570
571
572
573
574
575
576
577
578
579
581 _SendMessageTimeoutA = windll.user32.SendMessageTimeoutA
582 _SendMessageTimeoutA.argtypes = [HWND, UINT, WPARAM, LPARAM, UINT, UINT, PDWORD_PTR]
583 _SendMessageTimeoutA.restype = LRESULT
584 dwResult = DWORD(0)
585 success = _SendMessageTimeoutA(hWnd, Msg, wParam, lParam, fuFlags, uTimeout, ctypes.byref(dwResult))
586 if success == 0:
587 raise ctypes.WinError()
588 return dwResult.value
589
591 _SendMessageTimeoutW = windll.user32.SendMessageTimeoutW
592 _SendMessageTimeoutW.argtypes = [HWND, UINT, WPARAM, LPARAM, UINT, UINT, PDWORD_PTR]
593 _SendMessageTimeoutW.restype = LRESULT
594 dwResult = DWORD(0)
595 success = _SendMessageTimeoutW(hWnd, Msg, wParam, lParam, fuFlags, uTimeout, ctypes.byref(dwResult))
596 if success == 0:
597 raise ctypes.WinError()
598 return dwResult.value
599
600 SendMessageTimeout = GuessStringType(SendMessageTimeoutA, SendMessageTimeoutW)
601
602
603
604
605
606
607
609 _SendNotifyMessageA = windll.user32.SendNotifyMessageA
610 _SendNotifyMessageA.argtypes = [HWND, UINT, WPARAM, LPARAM]
611 _SendNotifyMessageA.restype = bool
612 _SendNotifyMessageA.errcheck = RaiseIfZero
613 _SendNotifyMessageA(hWnd, Msg, wParam, lParam)
614
616 _SendNotifyMessageW = windll.user32.SendNotifyMessageW
617 _SendNotifyMessageW.argtypes = [HWND, UINT, WPARAM, LPARAM]
618 _SendNotifyMessageW.restype = bool
619 _SendNotifyMessageW.errcheck = RaiseIfZero
620 _SendNotifyMessageW(hWnd, Msg, wParam, lParam)
621
622 SendNotifyMessage = GuessStringType(SendNotifyMessageA, SendNotifyMessageW)
623
624
625
626
627
628
629
630
632 _SendDlgItemMessageA = windll.user32.SendDlgItemMessageA
633 _SendDlgItemMessageA.argtypes = [HWND, ctypes.c_int, UINT, WPARAM, LPARAM]
634 _SendDlgItemMessageA.restype = LRESULT
635 return _SendDlgItemMessageA(hDlg, nIDDlgItem, Msg, wParam, lParam)
636
638 _SendDlgItemMessageW = windll.user32.SendDlgItemMessageW
639 _SendDlgItemMessageW.argtypes = [HWND, ctypes.c_int, UINT, WPARAM, LPARAM]
640 _SendDlgItemMessageW.restype = LRESULT
641 return _SendDlgItemMessageW(hDlg, nIDDlgItem, Msg, wParam, lParam)
642
643 SendDlgItemMessage = GuessStringType(SendDlgItemMessageA, SendDlgItemMessageW)
644
645
646
647
649 _RegisterWindowMessageA = windll.user32.RegisterWindowMessageA
650 _RegisterWindowMessageA.argtypes = [LPSTR]
651 _RegisterWindowMessageA.restype = UINT
652
653 lpString = ctypes.create_string_buffer(lpString)
654 uMsg = _RegisterWindowMessageA(lpString)
655 if uMsg == 0:
656 raise ctypes.WinError()
657 return uMsg
658
660 _RegisterWindowMessageW = windll.user32.RegisterWindowMessageW
661 _RegisterWindowMessageW.argtypes = [LPWSTR]
662 _RegisterWindowMessageW.restype = UINT
663
664 lpString = ctypes.create_unicode_buffer(lpString)
665 uMsg = _RegisterWindowMessageW(lpString)
666 if uMsg == 0:
667 raise ctypes.WinError()
668 return uMsg
669
670 RegisterWindowMessage = GuessStringType(RegisterWindowMessageA, RegisterWindowMessageW)
671