2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Window classes
5 * FILE: subsys/win32k/ntuser/wndproc.c
6 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Thomas Weidenmueller (w3seek@users.sourceforge.net)
9 * 06-06-2001 CSH Created
10 * NOTES: Please use the Callback Memory Management functions for
11 * callbacks to make sure, the memory is freed on thread
15 /* INCLUDES ******************************************************************/
22 /* CALLBACK MEMORY MANAGEMENT ************************************************/
24 typedef struct _INT_CALLBACK_HEADER
26 /* list entry in the THREADINFO structure */
29 INT_CALLBACK_HEADER
, *PINT_CALLBACK_HEADER
;
32 IntCbAllocateMemory(ULONG Size
)
34 PINT_CALLBACK_HEADER Mem
;
35 PTHREADINFO W32Thread
;
37 if(!(Mem
= ExAllocatePoolWithTag(PagedPool
, Size
+ sizeof(INT_CALLBACK_HEADER
),
43 W32Thread
= PsGetCurrentThreadWin32Thread();
46 /* insert the callback memory into the thread's callback list */
48 InsertTailList(&W32Thread
->W32CallbackListHead
, &Mem
->ListEntry
);
54 IntCbFreeMemory(PVOID Data
)
56 PINT_CALLBACK_HEADER Mem
;
57 PTHREADINFO W32Thread
;
61 Mem
= ((PINT_CALLBACK_HEADER
)Data
- 1);
63 W32Thread
= PsGetCurrentThreadWin32Thread();
66 /* remove the memory block from the thread's callback list */
67 RemoveEntryList(&Mem
->ListEntry
);
70 ExFreePoolWithTag(Mem
, TAG_CALLBACK
);
74 IntCleanupThreadCallbacks(PTHREADINFO W32Thread
)
76 PLIST_ENTRY CurrentEntry
;
77 PINT_CALLBACK_HEADER Mem
;
79 while (!IsListEmpty(&W32Thread
->W32CallbackListHead
))
81 CurrentEntry
= RemoveHeadList(&W32Thread
->W32CallbackListHead
);
82 Mem
= CONTAINING_RECORD(CurrentEntry
, INT_CALLBACK_HEADER
,
92 // Pass the Current Window handle and pointer to the Client Callback.
93 // This will help user space programs speed up read access with the window object.
96 IntSetTebWndCallback (HWND
* hWnd
, PWND
* pWnd
)
99 PWND Window
= UserGetWindowObject(*hWnd
);
100 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
102 *hWnd
= ClientInfo
->CallbackWnd
.hWnd
;
103 *pWnd
= ClientInfo
->CallbackWnd
.pWnd
;
105 ClientInfo
->CallbackWnd
.hWnd
= hWndS
;
106 ClientInfo
->CallbackWnd
.pWnd
= DesktopHeapAddressToUser(Window
);
110 IntRestoreTebWndCallback (HWND hWnd
, PWND pWnd
)
112 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
114 ClientInfo
->CallbackWnd
.hWnd
= hWnd
;
115 ClientInfo
->CallbackWnd
.pWnd
= pWnd
;
118 /* FUNCTIONS *****************************************************************/
121 co_IntCallSentMessageCallback(SENDASYNCPROC CompletionCallback
,
124 ULONG_PTR CompletionCallbackContext
,
127 SENDASYNCPROC_CALLBACK_ARGUMENTS Arguments
;
133 Arguments
.Callback
= CompletionCallback
;
134 Arguments
.Wnd
= hWnd
;
136 Arguments
.Context
= CompletionCallbackContext
;
137 Arguments
.Result
= Result
;
139 IntSetTebWndCallback (&hWnd
, &pWnd
);
143 Status
= KeUserModeCallback(USER32_CALLBACK_SENDASYNCPROC
,
145 sizeof(SENDASYNCPROC_CALLBACK_ARGUMENTS
),
151 IntRestoreTebWndCallback (hWnd
, pWnd
);
153 if (!NT_SUCCESS(Status
))
161 co_IntCallWindowProc(WNDPROC Proc
,
167 INT lParamBufferSize
)
169 WINDOWPROC_CALLBACK_ARGUMENTS StackArguments
;
170 PWINDOWPROC_CALLBACK_ARGUMENTS Arguments
;
175 ULONG ArgumentLength
;
178 if (0 < lParamBufferSize
)
180 ArgumentLength
= sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
) + lParamBufferSize
;
181 Arguments
= IntCbAllocateMemory(ArgumentLength
);
182 if (NULL
== Arguments
)
184 DPRINT1("Unable to allocate buffer for window proc callback\n");
187 RtlMoveMemory((PVOID
) ((char *) Arguments
+ sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
)),
188 (PVOID
) lParam
, lParamBufferSize
);
192 Arguments
= &StackArguments
;
193 ArgumentLength
= sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
);
195 Arguments
->Proc
= Proc
;
196 Arguments
->IsAnsiProc
= IsAnsiProc
;
197 Arguments
->Wnd
= Wnd
;
198 Arguments
->Msg
= Message
;
199 Arguments
->wParam
= wParam
;
200 Arguments
->lParam
= lParam
;
201 Arguments
->lParamBufferSize
= lParamBufferSize
;
202 ResultPointer
= NULL
;
203 ResultLength
= ArgumentLength
;
205 IntSetTebWndCallback (&Wnd
, &pWnd
);
209 Status
= KeUserModeCallback(USER32_CALLBACK_WINDOWPROC
,
217 /* Simulate old behaviour: copy into our local buffer */
218 RtlMoveMemory(Arguments
, ResultPointer
, ArgumentLength
);
220 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
222 Status
= _SEH2_GetExceptionCode();
228 IntRestoreTebWndCallback (Wnd
, pWnd
);
230 if (!NT_SUCCESS(Status
))
232 if (0 < lParamBufferSize
)
234 IntCbFreeMemory(Arguments
);
238 Result
= Arguments
->Result
;
240 if (0 < lParamBufferSize
)
242 RtlMoveMemory((PVOID
) lParam
,
243 (PVOID
) ((char *) Arguments
+ sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
)),
245 IntCbFreeMemory(Arguments
);
252 co_IntLoadSysMenuTemplate()
259 ResultPointer
= NULL
;
260 ResultLength
= sizeof(LRESULT
);
264 Status
= KeUserModeCallback(USER32_CALLBACK_LOADSYSMENUTEMPLATE
,
269 if (NT_SUCCESS(Status
))
271 /* Simulate old behaviour: copy into our local buffer */
274 ProbeForRead(ResultPointer
, sizeof(LRESULT
), 1);
275 Result
= *(LRESULT
*)ResultPointer
;
277 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
286 return (HMENU
)Result
;
290 co_IntLoadDefaultCursors(VOID
)
295 BOOL DefaultCursor
= TRUE
;
297 ResultPointer
= NULL
;
298 ResultLength
= sizeof(LRESULT
);
302 Status
= KeUserModeCallback(USER32_CALLBACK_LOADDEFAULTCURSORS
,
310 if (!NT_SUCCESS(Status
))
318 co_IntCallHookProc(INT HookId
,
324 PUNICODE_STRING ModuleName
)
326 ULONG ArgumentLength
;
327 PVOID Argument
= NULL
;
332 PHOOKPROC_CALLBACK_ARGUMENTS Common
;
333 CBT_CREATEWNDW
*CbtCreateWnd
= NULL
;
335 PHOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS CbtCreatewndExtra
= NULL
;
342 pti
= PsGetCurrentThreadWin32Thread();
343 if (pti
->TIF_flags
& TIF_INCLEANUP
)
345 DPRINT1("Thread is in cleanup and trying to call hook %d\n", Code
);
349 ArgumentLength
= sizeof(HOOKPROC_CALLBACK_ARGUMENTS
) - sizeof(WCHAR
)
350 + ModuleName
->Length
;
357 pWnd
= UserGetWindowObject((HWND
) wParam
);
360 DPRINT1("WH_CBT HCBT_CREATEWND wParam bad hWnd!\n");
363 // Due to KsStudio.exe, just pass the callers original pointers
364 // except class which point to kernel space if not an atom.
365 // Found by, Olaf Siejka
366 CbtCreateWnd
= (CBT_CREATEWNDW
*) lParam
;
367 ArgumentLength
+= sizeof(HOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS
);
371 ArgumentLength
+= sizeof(RECTL
);
374 ArgumentLength
+= sizeof(CBTACTIVATESTRUCT
);
376 case HCBT_CLICKSKIPPED
:
377 ArgumentLength
+= sizeof(MOUSEHOOKSTRUCT
);
380 case HCBT_KEYSKIPPED
:
383 case HCBT_SYSCOMMAND
:
384 /* These types pass through. */
385 case HCBT_DESTROYWND
:
389 DPRINT1("Trying to call unsupported CBT hook %d\n", Code
);
394 ArgumentLength
+= sizeof(KBDLLHOOKSTRUCT
);
397 ArgumentLength
+= sizeof(MSLLHOOKSTRUCT
);
400 ArgumentLength
+= sizeof(MOUSEHOOKSTRUCT
);
403 ArgumentLength
+= sizeof(CWPSTRUCT
);
405 case WH_CALLWNDPROCRET
:
406 ArgumentLength
+= sizeof(CWPRETSTRUCT
);
409 case WH_SYSMSGFILTER
:
411 ArgumentLength
+= sizeof(MSG
);
413 case WH_FOREGROUNDIDLE
:
418 DPRINT1("Trying to call unsupported window hook %d\n", HookId
);
422 Argument
= IntCbAllocateMemory(ArgumentLength
);
423 if (NULL
== Argument
)
425 DPRINT1("HookProc callback failed: out of memory\n");
428 Common
= (PHOOKPROC_CALLBACK_ARGUMENTS
) Argument
;
429 Common
->HookId
= HookId
;
431 Common
->wParam
= wParam
;
432 Common
->lParam
= lParam
;
435 Common
->ModuleNameLength
= ModuleName
->Length
;
436 if (ModuleName
->Buffer
)
437 RtlCopyMemory(Common
->ModuleName
, ModuleName
->Buffer
, ModuleName
->Length
);
438 Extra
= (PCHAR
) Common
->ModuleName
+ Common
->ModuleNameLength
;
446 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
447 CbtCreatewndExtra
= (PHOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS
) Extra
;
448 RtlCopyMemory( &CbtCreatewndExtra
->Cs
, CbtCreateWnd
->lpcs
, sizeof(CREATESTRUCTW
) );
449 CbtCreatewndExtra
->WndInsertAfter
= CbtCreateWnd
->hwndInsertAfter
;
450 CbtCreatewndExtra
->Cs
.lpszClass
= CbtCreateWnd
->lpcs
->lpszClass
; // if Atom
451 CbtCreatewndExtra
->Cs
.lpszName
= CbtCreateWnd
->lpcs
->lpszName
;
452 Extra
= (PCHAR
) (CbtCreatewndExtra
+ 1);
454 case HCBT_CLICKSKIPPED
:
455 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MOUSEHOOKSTRUCT
));
456 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
459 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(RECTL
));
460 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
463 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CBTACTIVATESTRUCT
));
464 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
469 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(KBDLLHOOKSTRUCT
));
470 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
473 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MSLLHOOKSTRUCT
));
474 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
477 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MOUSEHOOKSTRUCT
));
478 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
481 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CWPSTRUCT
));
482 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
484 case WH_CALLWNDPROCRET
:
485 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CWPRETSTRUCT
));
486 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
489 case WH_SYSMSGFILTER
:
491 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MSG
));
492 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
494 case WH_FOREGROUNDIDLE
:
500 ResultPointer
= NULL
;
501 ResultLength
= sizeof(LRESULT
);
505 Status
= KeUserModeCallback(USER32_CALLBACK_HOOKPROC
,
515 ProbeForRead(ResultPointer
, sizeof(LRESULT
), 1);
516 /* Simulate old behaviour: copy into our local buffer */
517 Result
= *(LRESULT
*)ResultPointer
;
519 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
526 if (!NT_SUCCESS(Status
))
530 /* Support write backs... SEH is in UserCallNextHookEx. */
534 if (Code
== HCBT_CREATEWND
)
536 if (CbtCreatewndExtra
)
538 The parameters could have been changed, include the coordinates
539 and dimensions of the window. We copy it back.
541 CbtCreateWnd
->hwndInsertAfter
= CbtCreatewndExtra
->WndInsertAfter
;
542 CbtCreateWnd
->lpcs
->x
= CbtCreatewndExtra
->Cs
.x
;
543 CbtCreateWnd
->lpcs
->y
= CbtCreatewndExtra
->Cs
.y
;
544 CbtCreateWnd
->lpcs
->cx
= CbtCreatewndExtra
->Cs
.cx
;
545 CbtCreateWnd
->lpcs
->cy
= CbtCreatewndExtra
->Cs
.cy
;
549 // "The GetMsgProc hook procedure can examine or modify the message."
553 RtlCopyMemory((PVOID
) lParam
, Extra
, sizeof(MSG
));
561 DPRINT1("Exception CallHookProc HookId %d Code %d\n",HookId
,Code
);
563 if (Argument
) IntCbFreeMemory(Argument
);
569 // Events are notifications w/o results.
573 co_IntCallEventProc(HWINEVENTHOOK hook
,
584 PEVENTPROC_CALLBACK_ARGUMENTS Common
;
585 ULONG ArgumentLength
, ResultLength
;
586 PVOID Argument
, ResultPointer
;
588 ArgumentLength
= sizeof(EVENTPROC_CALLBACK_ARGUMENTS
);
590 Argument
= IntCbAllocateMemory(ArgumentLength
);
591 if (NULL
== Argument
)
593 DPRINT1("EventProc callback failed: out of memory\n");
596 Common
= (PEVENTPROC_CALLBACK_ARGUMENTS
) Argument
;
598 Common
->event
= event
;
600 Common
->idObject
= idObject
;
601 Common
->idChild
= idChild
;
602 Common
->dwEventThread
= dwEventThread
;
603 Common
->dwmsEventTime
= dwmsEventTime
;
606 ResultPointer
= NULL
;
607 ResultLength
= sizeof(LRESULT
);
611 Status
= KeUserModeCallback(USER32_CALLBACK_EVENTPROC
,
619 IntCbFreeMemory(Argument
);
621 if (!NT_SUCCESS(Status
))
630 // Callback Load Menu and results.
634 co_IntCallLoadMenu( HINSTANCE hModule
,
635 PUNICODE_STRING pMenuName
)
639 PLOADMENU_CALLBACK_ARGUMENTS Common
;
640 ULONG ArgumentLength
, ResultLength
;
641 PVOID Argument
, ResultPointer
;
643 ArgumentLength
= sizeof(LOADMENU_CALLBACK_ARGUMENTS
);
645 ArgumentLength
+= pMenuName
->Length
+ sizeof(WCHAR
);
647 Argument
= IntCbAllocateMemory(ArgumentLength
);
648 if (NULL
== Argument
)
650 DPRINT1("LoadMenu callback failed: out of memory\n");
653 Common
= (PLOADMENU_CALLBACK_ARGUMENTS
) Argument
;
655 // Help Intersource check and MenuName is now 4 bytes + so zero it.
656 RtlZeroMemory(Common
, ArgumentLength
);
658 Common
->hModule
= hModule
;
659 if (pMenuName
->Length
)
660 RtlCopyMemory(&Common
->MenuName
, pMenuName
->Buffer
, pMenuName
->Length
);
662 RtlCopyMemory(&Common
->MenuName
, &pMenuName
->Buffer
, sizeof(WCHAR
));
664 ResultPointer
= NULL
;
665 ResultLength
= sizeof(LRESULT
);
669 Status
= KeUserModeCallback(USER32_CALLBACK_LOADMENU
,
677 Result
= *(LRESULT
*)ResultPointer
;
679 IntCbFreeMemory(Argument
);
681 if (!NT_SUCCESS(Status
))
686 return (HMENU
)Result
;
691 co_IntClientThreadSetup(VOID
)
694 ULONG ArgumentLength
, ResultLength
;
695 PVOID Argument
, ResultPointer
;
697 ArgumentLength
= ResultLength
= 0;
698 Argument
= ResultPointer
= NULL
;
702 Status
= KeUserModeCallback(USER32_CALLBACK_CLIENTTHREADSTARTUP
,