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
, PVOID
* pWnd
)
99 PWINDOW_OBJECT Window
= UserGetWindowObject(*hWnd
);
100 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
102 *hWnd
= ClientInfo
->CallbackWnd
.hWnd
;
103 *pWnd
= ClientInfo
->CallbackWnd
.pvWnd
;
105 ClientInfo
->CallbackWnd
.hWnd
= hWndS
;
106 ClientInfo
->CallbackWnd
.pvWnd
= DesktopHeapAddressToUser(Window
->Wnd
);
110 IntRestoreTebWndCallback (HWND hWnd
, PVOID pWnd
)
112 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
114 ClientInfo
->CallbackWnd
.hWnd
= hWnd
;
115 ClientInfo
->CallbackWnd
.pvWnd
= pWnd
;
118 /* FUNCTIONS *****************************************************************/
121 co_IntCallSentMessageCallback(SENDASYNCPROC CompletionCallback
,
124 ULONG_PTR CompletionCallbackContext
,
127 SENDASYNCPROC_CALLBACK_ARGUMENTS Arguments
;
128 PVOID ResultPointer
, pWnd
;
132 Arguments
.Callback
= CompletionCallback
;
133 Arguments
.Wnd
= hWnd
;
135 Arguments
.Context
= CompletionCallbackContext
;
136 Arguments
.Result
= Result
;
138 IntSetTebWndCallback (&hWnd
, &pWnd
);
142 Status
= KeUserModeCallback(USER32_CALLBACK_SENDASYNCPROC
,
144 sizeof(SENDASYNCPROC_CALLBACK_ARGUMENTS
),
150 IntRestoreTebWndCallback (hWnd
, pWnd
);
152 if (!NT_SUCCESS(Status
))
160 co_IntCallWindowProc(WNDPROC Proc
,
166 INT lParamBufferSize
)
168 WINDOWPROC_CALLBACK_ARGUMENTS StackArguments
;
169 PWINDOWPROC_CALLBACK_ARGUMENTS Arguments
;
171 PVOID ResultPointer
, pWnd
;
173 ULONG ArgumentLength
;
176 if (0 < lParamBufferSize
)
178 ArgumentLength
= sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
) + lParamBufferSize
;
179 Arguments
= IntCbAllocateMemory(ArgumentLength
);
180 if (NULL
== Arguments
)
182 DPRINT1("Unable to allocate buffer for window proc callback\n");
185 RtlMoveMemory((PVOID
) ((char *) Arguments
+ sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
)),
186 (PVOID
) lParam
, lParamBufferSize
);
190 Arguments
= &StackArguments
;
191 ArgumentLength
= sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
);
193 Arguments
->Proc
= Proc
;
194 Arguments
->IsAnsiProc
= IsAnsiProc
;
195 Arguments
->Wnd
= Wnd
;
196 Arguments
->Msg
= Message
;
197 Arguments
->wParam
= wParam
;
198 Arguments
->lParam
= lParam
;
199 Arguments
->lParamBufferSize
= lParamBufferSize
;
200 ResultPointer
= NULL
;
201 ResultLength
= ArgumentLength
;
203 IntSetTebWndCallback (&Wnd
, &pWnd
);
207 Status
= KeUserModeCallback(USER32_CALLBACK_WINDOWPROC
,
215 /* Simulate old behaviour: copy into our local buffer */
216 RtlMoveMemory(Arguments
, ResultPointer
, ArgumentLength
);
218 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
220 Status
= _SEH2_GetExceptionCode();
226 IntRestoreTebWndCallback (Wnd
, pWnd
);
228 if (!NT_SUCCESS(Status
))
230 if (0 < lParamBufferSize
)
232 IntCbFreeMemory(Arguments
);
236 Result
= Arguments
->Result
;
238 if (0 < lParamBufferSize
)
240 RtlMoveMemory((PVOID
) lParam
,
241 (PVOID
) ((char *) Arguments
+ sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
)),
243 IntCbFreeMemory(Arguments
);
250 co_IntLoadSysMenuTemplate()
257 ResultPointer
= NULL
;
258 ResultLength
= sizeof(LRESULT
);
262 Status
= KeUserModeCallback(USER32_CALLBACK_LOADSYSMENUTEMPLATE
,
267 if (NT_SUCCESS(Status
))
269 /* Simulate old behaviour: copy into our local buffer */
270 Result
= *(LRESULT
*)ResultPointer
;
275 return (HMENU
)Result
;
279 co_IntLoadDefaultCursors(VOID
)
284 BOOL DefaultCursor
= TRUE
;
286 ResultPointer
= NULL
;
287 ResultLength
= sizeof(LRESULT
);
291 Status
= KeUserModeCallback(USER32_CALLBACK_LOADDEFAULTCURSORS
,
299 if (!NT_SUCCESS(Status
))
307 co_IntCallHookProc(INT HookId
,
313 PUNICODE_STRING ModuleName
)
315 ULONG ArgumentLength
;
321 PHOOKPROC_CALLBACK_ARGUMENTS Common
;
322 CBT_CREATEWNDW
*CbtCreateWnd
=NULL
;
324 PHOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS CbtCreatewndExtra
= NULL
;
325 UNICODE_STRING WindowName
;
326 UNICODE_STRING ClassName
;
327 PANSI_STRING asWindowName
;
328 PANSI_STRING asClassName
;
330 ArgumentLength
= sizeof(HOOKPROC_CALLBACK_ARGUMENTS
) - sizeof(WCHAR
)
331 + ModuleName
->Length
;
338 CbtCreateWnd
= (CBT_CREATEWNDW
*) lParam
;
339 ArgumentLength
+= sizeof(HOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS
);
341 asWindowName
= (PANSI_STRING
)&WindowName
;
342 asClassName
= (PANSI_STRING
)&ClassName
;
346 RtlInitAnsiString(asWindowName
, (PCSZ
)CbtCreateWnd
->lpcs
->lpszName
);
347 ArgumentLength
+= WindowName
.Length
+ sizeof(CHAR
);
351 RtlInitUnicodeString(&WindowName
, CbtCreateWnd
->lpcs
->lpszName
);
352 ArgumentLength
+= WindowName
.Length
+ sizeof(WCHAR
);
355 if (! IS_ATOM(CbtCreateWnd
->lpcs
->lpszClass
))
359 RtlInitAnsiString(asClassName
, (PCSZ
)CbtCreateWnd
->lpcs
->lpszClass
);
360 ArgumentLength
+= ClassName
.Length
+ sizeof(CHAR
);
364 RtlInitUnicodeString(&ClassName
, CbtCreateWnd
->lpcs
->lpszClass
);
365 ArgumentLength
+= ClassName
.Length
+ sizeof(WCHAR
);
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 memcpy(Common
->ModuleName
, ModuleName
->Buffer
, ModuleName
->Length
);
437 Extra
= (PCHAR
) Common
->ModuleName
+ Common
->ModuleNameLength
;
445 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
446 CbtCreatewndExtra
= (PHOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS
) Extra
;
447 RtlCopyMemory( &CbtCreatewndExtra
->Cs
, CbtCreateWnd
->lpcs
, sizeof(CREATESTRUCTW
) );
448 CbtCreatewndExtra
->WndInsertAfter
= CbtCreateWnd
->hwndInsertAfter
;
449 Extra
= (PCHAR
) (CbtCreatewndExtra
+ 1);
450 RtlCopyMemory(Extra
, WindowName
.Buffer
, WindowName
.Length
);
451 CbtCreatewndExtra
->Cs
.lpszName
= (LPCWSTR
) (Extra
- (PCHAR
) CbtCreatewndExtra
);
452 CbtCreatewndExtra
->Cs
.lpszClass
= ClassName
.Buffer
;
453 Extra
+= WindowName
.Length
;
456 *((CHAR
*) Extra
) = '\0';
457 Extra
+= sizeof(CHAR
);
461 *((WCHAR
*) Extra
) = L
'\0';
462 Extra
+= sizeof(WCHAR
);
465 if (! IS_ATOM(ClassName
.Buffer
))
467 RtlCopyMemory(Extra
, ClassName
.Buffer
, ClassName
.Length
);
468 CbtCreatewndExtra
->Cs
.lpszClass
=
469 (LPCWSTR
) MAKELONG(Extra
- (PCHAR
) CbtCreatewndExtra
, 1);
470 Extra
+= ClassName
.Length
;
473 *((CHAR
*) Extra
) = '\0';
475 *((WCHAR
*) Extra
) = L
'\0';
478 case HCBT_CLICKSKIPPED
:
479 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MOUSEHOOKSTRUCT
));
480 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
483 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(RECTL
));
484 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
487 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CBTACTIVATESTRUCT
));
488 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
493 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(KBDLLHOOKSTRUCT
));
494 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
497 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MSLLHOOKSTRUCT
));
498 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
501 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MOUSEHOOKSTRUCT
));
502 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
505 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CWPSTRUCT
));
506 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
508 case WH_CALLWNDPROCRET
:
509 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CWPRETSTRUCT
));
510 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
513 case WH_SYSMSGFILTER
:
515 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MSG
));
516 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
517 // DPRINT1("KHOOK Memory: %x\n",Common);
519 case WH_FOREGROUNDIDLE
:
525 ResultPointer
= NULL
;
526 ResultLength
= sizeof(LRESULT
);
530 Status
= KeUserModeCallback(USER32_CALLBACK_HOOKPROC
,
540 ProbeForRead(ResultPointer
, sizeof(LRESULT
), 1);
541 /* Simulate old behaviour: copy into our local buffer */
542 Result
= *(LRESULT
*)ResultPointer
;
544 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
550 if (!NT_SUCCESS(Status
))
555 if (HookId
== WH_CBT
&& Code
== HCBT_CREATEWND
)
557 if (CbtCreatewndExtra
)
561 The parameters could have been changed, include the coordinates
562 and dimensions of the window. We copy it back.
564 CbtCreateWnd
->hwndInsertAfter
= CbtCreatewndExtra
->WndInsertAfter
;
565 CbtCreateWnd
->lpcs
->x
= CbtCreatewndExtra
->Cs
.x
;
566 CbtCreateWnd
->lpcs
->y
= CbtCreatewndExtra
->Cs
.y
;
567 CbtCreateWnd
->lpcs
->cx
= CbtCreatewndExtra
->Cs
.cx
;
568 CbtCreateWnd
->lpcs
->cy
= CbtCreatewndExtra
->Cs
.cy
;
570 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
578 if (Argument
) IntCbFreeMemory(Argument
);
584 // Events are notifications w/o results.
588 co_IntCallEventProc(HWINEVENTHOOK hook
,
599 PEVENTPROC_CALLBACK_ARGUMENTS Common
;
600 ULONG ArgumentLength
, ResultLength
;
601 PVOID Argument
, ResultPointer
;
603 ArgumentLength
= sizeof(EVENTPROC_CALLBACK_ARGUMENTS
);
605 Argument
= IntCbAllocateMemory(ArgumentLength
);
606 if (NULL
== Argument
)
608 DPRINT1("EventProc callback failed: out of memory\n");
611 Common
= (PEVENTPROC_CALLBACK_ARGUMENTS
) Argument
;
613 Common
->event
= event
;
615 Common
->idObject
= idObject
;
616 Common
->idChild
= idChild
;
617 Common
->dwEventThread
= dwEventThread
;
618 Common
->dwmsEventTime
= dwmsEventTime
;
621 ResultPointer
= NULL
;
622 ResultLength
= sizeof(LRESULT
);
626 Status
= KeUserModeCallback(USER32_CALLBACK_EVENTPROC
,
634 IntCbFreeMemory(Argument
);
636 if (!NT_SUCCESS(Status
))
645 // Callback Load Menu and results.
649 co_IntCallLoadMenu( HINSTANCE hModule
,
650 PUNICODE_STRING pMenuName
)
654 PLOADMENU_CALLBACK_ARGUMENTS Common
;
655 ULONG ArgumentLength
, ResultLength
;
656 PVOID Argument
, ResultPointer
;
658 ArgumentLength
= sizeof(LOADMENU_CALLBACK_ARGUMENTS
);
660 ArgumentLength
+= pMenuName
->Length
+ sizeof(WCHAR
);
662 Argument
= IntCbAllocateMemory(ArgumentLength
);
663 if (NULL
== Argument
)
665 DPRINT1("LoadMenu callback failed: out of memory\n");
668 Common
= (PLOADMENU_CALLBACK_ARGUMENTS
) Argument
;
670 // Help Intersource check and MenuName is now 4 bytes + so zero it.
671 RtlZeroMemory(Common
, ArgumentLength
);
673 Common
->hModule
= hModule
;
674 if (pMenuName
->Length
)
675 RtlCopyMemory(&Common
->MenuName
, pMenuName
->Buffer
, pMenuName
->Length
);
677 RtlCopyMemory(&Common
->MenuName
, &pMenuName
->Buffer
, sizeof(WCHAR
));
679 ResultPointer
= NULL
;
680 ResultLength
= sizeof(LRESULT
);
684 Status
= KeUserModeCallback(USER32_CALLBACK_LOADMENU
,
692 Result
= *(LRESULT
*)ResultPointer
;
694 IntCbFreeMemory(Argument
);
696 if (!NT_SUCCESS(Status
))
701 return (HMENU
)Result
;
706 co_IntClientThreadSetup(VOID
)
709 ULONG ArgumentLength
, ResultLength
;
710 PVOID Argument
, ResultPointer
;
712 ArgumentLength
= ResultLength
= 0;
713 Argument
= ResultPointer
= NULL
;
717 Status
= KeUserModeCallback(USER32_CALLBACK_CLIENTTHREADSTARTUP
,