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 *****************************************************************/
43 RegisterLogonProcess(DWORD dwProcessId
, BOOL bRegister
)
45 return NtUserxRegisterLogonProcess(dwProcessId
,bRegister
);
53 SetLogonNotifyWindow (HWND Wnd
, HWINSTA WinSta
)
55 /* Maybe we should call NtUserSetLogonNotifyWindow and let that one inform CSRSS??? */
56 CSR_API_MESSAGE Request
;
60 CsrRequest
= MAKE_CSR_API(SET_LOGON_NOTIFY_WINDOW
, CSR_GUI
);
61 Request
.Data
.SetLogonNotifyWindowRequest
.LogonNotifyWindow
= Wnd
;
63 Status
= CsrClientCallServer(&Request
,
66 sizeof(CSR_API_MESSAGE
));
67 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
69 SetLastError(RtlNtStatusToDosError(Status
));
73 return NtUserSetLogonNotifyWindow(Wnd
);
80 UpdatePerUserSystemParameters(
84 return NtUserUpdatePerUserSystemParameters(dwReserved
, bEnable
);
88 GetW32ThreadInfo(VOID
)
92 ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
95 /* create the THREADINFO structure */
96 NtUserGetThreadState(THREADSTATE_GETTHREADINFO
);
97 ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
105 * GetUserObjectSecurity
107 * Retrieves security information for user object specified
108 * with handle 'hObject'. Descriptor returned in self-relative
112 * 1) hObject - handle to an object to retrieve information for
113 * 2) pSecurityInfo - type of information to retrieve
114 * 3) pSecurityDescriptor - buffer which receives descriptor
115 * 4) dwLength - size, in bytes, of buffer 'pSecurityDescriptor'
116 * 5) pdwLengthNeeded - reseives actual size of descriptor
120 * FALSE on failure, call GetLastError() for more information
127 GetUserObjectSecurity(
129 IN PSECURITY_INFORMATION pSecurityInfo
,
130 OUT PSECURITY_DESCRIPTOR pSecurityDescriptor
,
132 OUT PDWORD pdwLengthNeeded
139 Status
= NtQuerySecurityObject(
140 hObject
, // Object Handle
141 *pSecurityInfo
, // Security Information
142 pSecurityDescriptor
,// Security Descriptor
143 dwLength
, // Buffer Length
144 pdwLengthNeeded
// Actual Length
147 if ( ! NT_SUCCESS( Status
) ) {
148 dwWin32Error
= RtlNtStatusToDosError( Status
);
149 NtCurrentTeb()->LastErrorValue
= dwWin32Error
;
158 * SetUserObjectSecurity
160 * Sets new security descriptor to user object specified by
161 * handle 'hObject'. Descriptor must be in self-relative format.
164 * 1) hObject - handle to an object to set information for
165 * 2) pSecurityInfo - type of information to apply
166 * 3) pSecurityDescriptor - buffer which descriptor to set
170 * FALSE on failure, call GetLastError() for more information
177 SetUserObjectSecurity(
179 IN PSECURITY_INFORMATION pSecurityInfo
,
180 IN PSECURITY_DESCRIPTOR pSecurityDescriptor
187 Status
= NtSetSecurityObject(
188 hObject
, // Object Handle
189 *pSecurityInfo
, // Security Information
190 pSecurityDescriptor
// Security Descriptor
193 if ( ! NT_SUCCESS( Status
) ) {
194 dwWin32Error
= RtlNtStatusToDosError( Status
);
195 NtCurrentTeb()->LastErrorValue
= dwWin32Error
;
212 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);
217 return DestroyWindow(hWnd
);
233 PTHREADINFO ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
238 NtUserGetThreadState(THREADSTATE_GETTHREADINFO
);
239 if ((PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
) return TRUE
;
241 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
251 TestWindowProcess(PWND Wnd
)
253 if (Wnd
->head
.pti
== (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
)
256 return (NtUserQueryWindow(Wnd
->head
.h
, QUERY_WINDOW_UNIQUE_PROCESS_ID
) ==
257 (DWORD_PTR
)NtCurrentTeb()->ClientId
.UniqueProcess
);
262 TestState(PWND pWnd
, UINT Flag
)
265 bit
= 1 << LOWORD(Flag
);
269 return (pWnd
->state
& bit
);
271 return (pWnd
->state2
& bit
);
273 return (pWnd
->ExStyle2
& bit
);
280 GetUser32Handle(HANDLE handle
)
285 if (!handle
) return NULL
;
287 Index
= (((UINT_PTR
)handle
& 0xffff) - FIRST_USER_HANDLE
) >> 1;
289 if (Index
< 0 || Index
>= gHandleTable
->nb_handles
)
292 if (!gHandleEntries
[Index
].type
|| !gHandleEntries
[Index
].ptr
)
295 generation
= (UINT_PTR
)handle
>> 16;
297 if (generation
== gHandleEntries
[Index
].generation
|| !generation
|| generation
== 0xffff)
298 return &gHandleEntries
[Index
];
304 * Decide whether an object is located on the desktop or shared heap
306 static const BOOL g_ObjectHeapTypeShared
[otEvent
+ 1] =
308 FALSE
, /* otFree (not used) */
309 FALSE
, /* otWindow */
310 TRUE
, /* otMenu FALSE */
311 TRUE
, /* otCursorIcon */
314 FALSE
, /* (not used) */
315 FALSE
, /* otCallProc */
317 FALSE
, /* (not used) */
318 FALSE
, /* (not used) */
319 FALSE
, /* (not used) */
320 TRUE
, /* otMonitor */
321 FALSE
, /* (not used) */
322 FALSE
, /* (not used) */
327 // Validate Handle and return the pointer to the object.
331 ValidateHandle(HANDLE handle
, UINT uType
)
334 PUSER_HANDLE_ENTRY pEntry
;
336 ASSERT(uType
<= otEvent
);
338 pEntry
= GetUser32Handle(handle
);
340 if (pEntry
&& uType
== 0)
341 uType
= pEntry
->type
;
343 // Must have an entry and must be the same type!
345 (pEntry
->type
!= uType
) ||
347 (pEntry
->flags
& HANDLEENTRY_INDESTROY
) )
350 { // Test (with wine too) confirms these results!
352 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
355 SetLastError(ERROR_INVALID_MENU_HANDLE
);
358 SetLastError(ERROR_INVALID_CURSOR_HANDLE
);
361 SetLastError(ERROR_INVALID_DWP_HANDLE
);
364 SetLastError(ERROR_INVALID_HOOK_HANDLE
);
367 SetLastError(ERROR_INVALID_ACCEL_HANDLE
);
370 SetLastError(ERROR_INVALID_HANDLE
);
376 if (g_ObjectHeapTypeShared
[uType
])
377 ret
= SharedPtrToUser(pEntry
->ptr
);
379 ret
= DesktopPtrToUser(pEntry
->ptr
);
385 // Validate Handle and return the pointer to the object.
389 ValidateHandleNoErr(HANDLE handle
, UINT uType
)
392 PUSER_HANDLE_ENTRY pEntry
;
394 ASSERT(uType
<= otEvent
);
396 pEntry
= GetUser32Handle(handle
);
398 if (pEntry
&& uType
== 0)
399 uType
= pEntry
->type
;
401 // Must have an entry and must be the same type!
402 if ( (!pEntry
) || (pEntry
->type
!= uType
) || !pEntry
->ptr
)
405 if (g_ObjectHeapTypeShared
[uType
])
406 ret
= SharedPtrToUser(pEntry
->ptr
);
408 ret
= DesktopPtrToUser(pEntry
->ptr
);
414 // Validate a callproc handle and return the pointer to the object.
418 ValidateCallProc(HANDLE hCallProc
)
420 PUSER_HANDLE_ENTRY pEntry
;
422 PCALLPROCDATA CallProc
= ValidateHandle(hCallProc
, otCallProc
);
424 pEntry
= GetUser32Handle(hCallProc
);
426 if (CallProc
!= NULL
&& pEntry
->ppi
== g_ppi
)
434 // Validate a window handle and return the pointer to the object.
438 ValidateHwnd(HWND hwnd
)
440 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
441 ASSERT(ClientInfo
!= NULL
);
443 /* See if the window is cached */
444 if (hwnd
&& hwnd
== ClientInfo
->CallbackWnd
.hWnd
)
445 return ClientInfo
->CallbackWnd
.pWnd
;
447 return ValidateHandle((HANDLE
)hwnd
, otWindow
);
451 // Validate a window handle and return the pointer to the object.
455 ValidateHwndNoErr(HWND hwnd
)
458 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
459 ASSERT(ClientInfo
!= NULL
);
461 /* See if the window is cached */
462 if (hwnd
== ClientInfo
->CallbackWnd
.hWnd
)
463 return ClientInfo
->CallbackWnd
.pWnd
;
465 Wnd
= ValidateHandleNoErr((HANDLE
)hwnd
, otWindow
);
476 GetThreadDesktopWnd(VOID
)
478 PWND Wnd
= GetThreadDesktopInfo()->spwnd
;
480 Wnd
= DesktopPtrToUser(Wnd
);
485 // Validate a window handle and return the pointer to the object.
489 ValidateHwndOrDesk(HWND hwnd
)
491 if (hwnd
== HWND_DESKTOP
)
492 return GetThreadDesktopWnd();
494 return ValidateHwnd(hwnd
);
500 DWORD WINAPI
WCSToMBEx(WORD CodePage
,LPWSTR UnicodeString
,LONG UnicodeSize
,LPSTR
*MBString
,LONG MBSize
,BOOL Allocate
)
503 if (UnicodeSize
== -1)
505 UnicodeSize
= wcslen(UnicodeString
)+1;
513 MBSize
= UnicodeSize
* 2;
517 LPSTR SafeString
= RtlAllocateHeap(GetProcessHeap(), 0, MBSize
);
518 if (SafeString
== NULL
)
520 *MBString
= SafeString
;
524 RtlUnicodeToMultiByteN(*MBString
,MBSize
,&Size
,UnicodeString
,UnicodeSize
);
528 WideCharToMultiByte(CodePage
,0,UnicodeString
,UnicodeSize
,*MBString
,MBSize
,0,0);
536 DWORD WINAPI
MBToWCSEx(WORD CodePage
,LPSTR MBString
,LONG MBSize
,LPWSTR
*UnicodeString
,LONG UnicodeSize
,BOOL Allocate
)
541 MBSize
= strlen(MBString
)+1;
543 if (UnicodeSize
== -1)
549 UnicodeSize
= MBSize
;
553 LPWSTR SafeString
= RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize
);
554 if (SafeString
== NULL
)
556 *UnicodeString
= SafeString
;
558 UnicodeSize
*= sizeof(WCHAR
);
561 RtlMultiByteToUnicodeN(*UnicodeString
,UnicodeSize
,&Size
,MBString
,MBSize
);
565 Size
= MultiByteToWideChar(CodePage
,0,MBString
,MBSize
,*UnicodeString
,UnicodeSize
);