3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * PROJECT: ReactOS user32.dll
21 * FILE: lib/user32/misc/misc.c
23 * PROGRAMMER: Thomas Weidenmueller (w3seek@users.sourceforge.net)
28 /* INCLUDES ******************************************************************/
32 #include <wine/debug.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
36 /* FUNCTIONS *****************************************************************/
39 * Private calls for CSRSS
43 PrivateCsrssManualGuiCheck(LONG Check
)
45 NtUserCallOneParam(Check
, ONEPARAM_ROUTINE_CSRSS_GUICHECK
);
54 RegisterLogonProcess(DWORD dwProcessId
, BOOL bRegister
)
56 return NtUserCallTwoParam(dwProcessId
,
58 TWOPARAM_ROUTINE_REGISTERLOGONPROC
);
66 SetLogonNotifyWindow (HWND Wnd
, HWINSTA WinSta
)
68 /* Maybe we should call NtUserSetLogonNotifyWindow and let that one inform CSRSS??? */
69 CSR_API_MESSAGE Request
;
73 CsrRequest
= MAKE_CSR_API(SET_LOGON_NOTIFY_WINDOW
, CSR_GUI
);
74 Request
.Data
.SetLogonNotifyWindowRequest
.LogonNotifyWindow
= Wnd
;
76 Status
= CsrClientCallServer(&Request
,
79 sizeof(CSR_API_MESSAGE
));
80 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
82 SetLastError(RtlNtStatusToDosError(Status
));
93 UpdatePerUserSystemParameters(
97 return NtUserUpdatePerUserSystemParameters(dwReserved
, bEnable
);
101 GetW32ThreadInfo(VOID
)
105 ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
108 /* create the THREADINFO structure */
109 NtUserGetThreadState(THREADSTATE_GETTHREADINFO
);
110 ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
118 * GetUserObjectSecurity
120 * Retrieves security information for user object specified
121 * with handle 'hObject'. Descriptor returned in self-relative
125 * 1) hObject - handle to an object to retrieve information for
126 * 2) pSecurityInfo - type of information to retrieve
127 * 3) pSecurityDescriptor - buffer which receives descriptor
128 * 4) dwLength - size, in bytes, of buffer 'pSecurityDescriptor'
129 * 5) pdwLengthNeeded - reseives actual size of descriptor
133 * FALSE on failure, call GetLastError() for more information
140 GetUserObjectSecurity(
142 IN PSECURITY_INFORMATION pSecurityInfo
,
143 OUT PSECURITY_DESCRIPTOR pSecurityDescriptor
,
145 OUT PDWORD pdwLengthNeeded
152 Status
= NtQuerySecurityObject(
153 hObject
, // Object Handle
154 *pSecurityInfo
, // Security Information
155 pSecurityDescriptor
,// Security Descriptor
156 dwLength
, // Buffer Length
157 pdwLengthNeeded
// Actual Length
160 if ( ! NT_SUCCESS( Status
) ) {
161 dwWin32Error
= RtlNtStatusToDosError( Status
);
162 NtCurrentTeb()->LastErrorValue
= dwWin32Error
;
171 * SetUserObjectSecurity
173 * Sets new security descriptor to user object specified by
174 * handle 'hObject'. Descriptor must be in self-relative format.
177 * 1) hObject - handle to an object to set information for
178 * 2) pSecurityInfo - type of information to apply
179 * 3) pSecurityDescriptor - buffer which descriptor to set
183 * FALSE on failure, call GetLastError() for more information
190 SetUserObjectSecurity(
192 IN PSECURITY_INFORMATION pSecurityInfo
,
193 IN PSECURITY_DESCRIPTOR pSecurityDescriptor
200 Status
= NtSetSecurityObject(
201 hObject
, // Object Handle
202 *pSecurityInfo
, // Security Information
203 pSecurityDescriptor
// Security Descriptor
206 if ( ! NT_SUCCESS( Status
) ) {
207 dwWin32Error
= RtlNtStatusToDosError( Status
);
208 NtCurrentTeb()->LastErrorValue
= dwWin32Error
;
225 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);
230 return DestroyWindow(hWnd
);
246 PTHREADINFO ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
251 NtUserGetThreadState(THREADSTATE_GETTHREADINFO
);
252 if ((PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
) return TRUE
;
254 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
264 TestWindowProcess(PWND Wnd
)
266 if (Wnd
->head
.pti
== (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
)
269 return (NtUserQueryWindow(Wnd
->head
.h
, QUERY_WINDOW_UNIQUE_PROCESS_ID
) ==
270 (DWORD
)NtCurrentTeb()->ClientId
.UniqueProcess
);
277 DWORD Type
= GetObjectType((HGDIOBJ
) hDc
);
283 case OBJ_ENHMETAFILE
:
291 GetUser32Handle(HANDLE handle
)
296 Index
= (((UINT
)handle
& 0xffff) - FIRST_USER_HANDLE
) >> 1;
298 if (Index
< 0 || Index
>= gHandleTable
->nb_handles
)
301 if (!gHandleEntries
[Index
].type
|| !gHandleEntries
[Index
].ptr
)
304 generation
= (UINT
)handle
>> 16;
306 if (generation
== gHandleEntries
[Index
].generation
|| !generation
|| generation
== 0xffff)
307 return &gHandleEntries
[Index
];
313 * Decide whether an object is located on the desktop or shared heap
315 static const BOOL g_ObjectHeapTypeShared
[VALIDATE_TYPE_EVENT
+ 1] =
317 FALSE
, /* VALIDATE_TYPE_FREE (not used) */
318 TRUE
, /* VALIDATE_TYPE_WIN FALSE */
319 TRUE
, /* VALIDATE_TYPE_MENU FALSE */
320 TRUE
, /* VALIDATE_TYPE_CURSOR */
321 TRUE
, /* VALIDATE_TYPE_MWPOS */
322 TRUE
, /* VALIDATE_TYPE_HOOK FALSE */
323 FALSE
, /* (not used) */
324 TRUE
, /* VALIDATE_TYPE_CALLPROC FALSE */
325 TRUE
, /* VALIDATE_TYPE_ACCEL */
326 FALSE
, /* (not used) */
327 FALSE
, /* (not used) */
328 FALSE
, /* (not used) */
329 TRUE
, /* VALIDATE_TYPE_MONITOR */
330 FALSE
, /* (not used) */
331 FALSE
, /* (not used) */
332 TRUE
/* VALIDATE_TYPE_EVENT */
336 // Validate Handle and return the pointer to the object.
340 ValidateHandle(HANDLE handle
, UINT uType
)
343 PUSER_HANDLE_ENTRY pEntry
;
345 ASSERT(uType
<= VALIDATE_TYPE_EVENT
);
347 pEntry
= GetUser32Handle(handle
);
349 if (pEntry
&& uType
== 0)
350 uType
= pEntry
->type
;
352 // Must have an entry and must be the same type!
354 (pEntry
->type
!= uType
) ||
356 (pEntry
->flags
& HANDLEENTRY_INDESTROY
) )
359 { // Test (with wine too) confirms these results!
360 case VALIDATE_TYPE_WIN
:
361 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
363 case VALIDATE_TYPE_MENU
:
364 SetLastError(ERROR_INVALID_MENU_HANDLE
);
366 case VALIDATE_TYPE_CURSOR
:
367 SetLastError(ERROR_INVALID_CURSOR_HANDLE
);
369 case VALIDATE_TYPE_MWPOS
:
370 SetLastError(ERROR_INVALID_DWP_HANDLE
);
372 case VALIDATE_TYPE_HOOK
:
373 SetLastError(ERROR_INVALID_HOOK_HANDLE
);
375 case VALIDATE_TYPE_ACCEL
:
376 SetLastError(ERROR_INVALID_ACCEL_HANDLE
);
379 SetLastError(ERROR_INVALID_HANDLE
);
385 if (g_ObjectHeapTypeShared
[uType
])
386 ret
= SharedPtrToUser(pEntry
->ptr
);
388 ret
= DesktopPtrToUser(pEntry
->ptr
);
394 // Validate Handle and return the pointer to the object.
398 ValidateHandleNoErr(HANDLE handle
, UINT uType
)
401 PUSER_HANDLE_ENTRY pEntry
;
403 ASSERT(uType
<= VALIDATE_TYPE_EVENT
);
405 pEntry
= GetUser32Handle(handle
);
407 if (pEntry
&& uType
== 0)
408 uType
= pEntry
->type
;
410 // Must have an entry and must be the same type!
411 if ( (!pEntry
) || (pEntry
->type
!= uType
) || !pEntry
->ptr
)
414 if (g_ObjectHeapTypeShared
[uType
])
415 ret
= SharedPtrToUser(pEntry
->ptr
);
417 ret
= DesktopPtrToUser(pEntry
->ptr
);
423 // Validate a callproc handle and return the pointer to the object.
427 ValidateCallProc(HANDLE hCallProc
)
429 PUSER_HANDLE_ENTRY pEntry
;
431 PCALLPROCDATA CallProc
= ValidateHandle(hCallProc
, VALIDATE_TYPE_CALLPROC
);
433 pEntry
= GetUser32Handle(hCallProc
);
435 if (CallProc
!= NULL
&& pEntry
->ppi
== g_ppi
)
449 // Validate a window handle and return the pointer to the object.
453 ValidateHwnd(HWND hwnd
)
456 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
457 ASSERT(ClientInfo
!= NULL
);
459 /* See if the window is cached */
460 if (hwnd
== ClientInfo
->CallbackWnd
.hWnd
)
461 return ClientInfo
->CallbackWnd
.pvWnd
;
463 Wnd
= ValidateHandle((HANDLE
)hwnd
, VALIDATE_TYPE_WIN
);
469 /* HACK HACK HACK! This needs to be done until WINDOW_OBJECT is completely
470 superseded by the WINDOW structure. We *ASSUME* a pointer to the WINDOW
471 structure to be at the beginning of the WINDOW_OBJECT structure!!!
473 !!! REMOVE AS SOON AS WINDOW_OBJECT NO LONGER EXISTS !!!
476 if ( ((PWNDX
)Wnd
)->pWnd
!= NULL
)
477 return DesktopPtrToUser( ((PWNDX
)Wnd
)->pWnd
);
485 // Validate a window handle and return the pointer to the object.
489 ValidateHwndNoErr(HWND hwnd
)
492 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
493 ASSERT(ClientInfo
!= NULL
);
495 /* See if the window is cached */
496 if (hwnd
== ClientInfo
->CallbackWnd
.hWnd
)
497 return ClientInfo
->CallbackWnd
.pvWnd
;
499 Wnd
= ValidateHandleNoErr((HANDLE
)hwnd
, VALIDATE_TYPE_WIN
);
505 /* HACK HACK HACK! This needs to be done until WINDOW_OBJECT is completely
506 superseded by the WINDOW structure. We *ASSUME* a pointer to the WINDOW
507 structure to be at the beginning of the WINDOW_OBJECT structure!!!
509 !!! REMOVE AS SOON AS WINDOW_OBJECT NO LONGER EXISTS !!!
512 if ( ((PWNDX
)Wnd
)->pWnd
!= NULL
)
513 return DesktopPtrToUser( ((PWNDX
)Wnd
)->pWnd
);
522 GetThreadDesktopWnd(VOID
)
524 PWND Wnd
= GetThreadDesktopInfo()->spwnd
;
526 Wnd
= DesktopPtrToUser(Wnd
);
531 // Validate a window handle and return the pointer to the object.
535 ValidateHwndOrDesk(HWND hwnd
)
537 if (hwnd
== HWND_DESKTOP
)
538 return GetThreadDesktopWnd();
540 return ValidateHwnd(hwnd
);
546 DWORD WINAPI
WCSToMBEx(WORD CodePage
,LPWSTR UnicodeString
,LONG UnicodeSize
,LPSTR
*MBString
,LONG MBSize
,BOOL Allocate
)
549 if (UnicodeSize
== -1)
551 UnicodeSize
= wcslen(UnicodeString
)+1;
559 MBSize
= UnicodeSize
* 2;
563 LPSTR SafeString
= RtlAllocateHeap(GetProcessHeap(), 0, MBSize
);
564 if (SafeString
== NULL
)
566 *MBString
= SafeString
;
570 RtlUnicodeToMultiByteN(*MBString
,MBSize
,&Size
,UnicodeString
,UnicodeSize
);
574 WideCharToMultiByte(CodePage
,0,UnicodeString
,UnicodeSize
,*MBString
,MBSize
,0,0);
582 DWORD WINAPI
MBToWCSEx(WORD CodePage
,LPSTR MBString
,LONG MBSize
,LPWSTR
*UnicodeString
,LONG UnicodeSize
,BOOL Allocate
)
587 MBSize
= strlen(MBString
)+1;
589 if (UnicodeSize
== -1)
595 UnicodeSize
= MBSize
;
599 LPWSTR SafeString
= RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize
);
600 if (SafeString
== NULL
)
602 *UnicodeString
= SafeString
;
604 UnicodeSize
*= sizeof(WCHAR
);
607 RtlMultiByteToUnicodeN(*UnicodeString
,UnicodeSize
,&Size
,MBString
,MBSize
);
611 Size
= MultiByteToWideChar(CodePage
,0,MBString
,MBSize
,*UnicodeString
,UnicodeSize
);