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 gfLogonProcess
= NtUserxRegisterLogonProcess(dwProcessId
, bRegister
);
50 USER_API_MESSAGE ApiMessage
;
52 ApiMessage
.Data
.RegisterLogonProcessRequest
.ProcessId
= dwProcessId
;
53 ApiMessage
.Data
.RegisterLogonProcessRequest
.Register
= bRegister
;
55 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
57 CSR_CREATE_API_NUMBER(USERSRV_SERVERDLL_INDEX
, UserpRegisterLogonProcess
),
58 sizeof(CSRSS_REGISTER_LOGON_PROCESS
));
59 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= ApiMessage
.Status
))
61 SetLastError(RtlNtStatusToDosError(Status
));
62 ERR("Failed to register logon process with CSRSS\n");
67 return gfLogonProcess
;
75 SetLogonNotifyWindow(HWND Wnd
, HWINSTA WinSta
)
77 /// HACK: Windows does not do this !! ReactOS-specific
78 /* Maybe we should call NtUserSetLogonNotifyWindow and let that one inform CSRSS??? */
79 USER_API_MESSAGE Request
;
82 Request
.Data
.SetLogonNotifyWindowRequest
.LogonNotifyWindow
= Wnd
;
84 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&Request
,
86 CSR_CREATE_API_NUMBER(USERSRV_SERVERDLL_INDEX
, UserpRosSetLogonNotifyWindow
),
87 sizeof(CSRSS_SET_LOGON_NOTIFY_WINDOW
));
88 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
90 SetLastError(RtlNtStatusToDosError(Status
));
95 return NtUserSetLogonNotifyWindow(Wnd
);
102 UpdatePerUserSystemParameters(
106 return NtUserUpdatePerUserSystemParameters(dwReserved
, bEnable
);
110 GetW32ThreadInfo(VOID
)
114 ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
117 /* create the THREADINFO structure */
118 NtUserGetThreadState(THREADSTATE_GETTHREADINFO
);
119 ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
127 * GetUserObjectSecurity
129 * Retrieves security information for user object specified
130 * with handle 'hObject'. Descriptor returned in self-relative
134 * 1) hObject - handle to an object to retrieve information for
135 * 2) pSecurityInfo - type of information to retrieve
136 * 3) pSecurityDescriptor - buffer which receives descriptor
137 * 4) dwLength - size, in bytes, of buffer 'pSecurityDescriptor'
138 * 5) pdwLengthNeeded - reseives actual size of descriptor
142 * FALSE on failure, call GetLastError() for more information
149 GetUserObjectSecurity(
151 IN PSECURITY_INFORMATION pSecurityInfo
,
152 OUT PSECURITY_DESCRIPTOR pSecurityDescriptor
,
154 OUT PDWORD pdwLengthNeeded
161 Status
= NtQuerySecurityObject(
162 hObject
, // Object Handle
163 *pSecurityInfo
, // Security Information
164 pSecurityDescriptor
,// Security Descriptor
165 dwLength
, // Buffer Length
166 pdwLengthNeeded
// Actual Length
169 if ( ! NT_SUCCESS( Status
) ) {
170 dwWin32Error
= RtlNtStatusToDosError( Status
);
171 NtCurrentTeb()->LastErrorValue
= dwWin32Error
;
180 * SetUserObjectSecurity
182 * Sets new security descriptor to user object specified by
183 * handle 'hObject'. Descriptor must be in self-relative format.
186 * 1) hObject - handle to an object to set information for
187 * 2) pSecurityInfo - type of information to apply
188 * 3) pSecurityDescriptor - buffer which descriptor to set
192 * FALSE on failure, call GetLastError() for more information
199 SetUserObjectSecurity(
201 IN PSECURITY_INFORMATION pSecurityInfo
,
202 IN PSECURITY_DESCRIPTOR pSecurityDescriptor
209 Status
= NtSetSecurityObject(
210 hObject
, // Object Handle
211 *pSecurityInfo
, // Security Information
212 pSecurityDescriptor
// Security Descriptor
215 if ( ! NT_SUCCESS( Status
) ) {
216 dwWin32Error
= RtlNtStatusToDosError( Status
);
217 NtCurrentTeb()->LastErrorValue
= dwWin32Error
;
234 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);
239 return DestroyWindow(hWnd
);
255 PTHREADINFO ti
= (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
;
260 NtUserGetThreadState(THREADSTATE_GETTHREADINFO
);
261 if ((PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
) return TRUE
;
263 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
273 TestWindowProcess(PWND Wnd
)
275 if (Wnd
->head
.pti
== (PTHREADINFO
)NtCurrentTeb()->Win32ThreadInfo
)
278 return (NtUserQueryWindow(Wnd
->head
.h
, QUERY_WINDOW_UNIQUE_PROCESS_ID
) ==
279 (DWORD_PTR
)NtCurrentTeb()->ClientId
.UniqueProcess
);
284 TestState(PWND pWnd
, UINT Flag
)
287 bit
= 1 << LOWORD(Flag
);
291 return (pWnd
->state
& bit
);
293 return (pWnd
->state2
& bit
);
295 return (pWnd
->ExStyle2
& bit
);
302 GetUser32Handle(HANDLE handle
)
307 Index
= (((UINT_PTR
)handle
& 0xffff) - FIRST_USER_HANDLE
) >> 1;
309 if (Index
< 0 || Index
>= gHandleTable
->nb_handles
)
312 if (!gHandleEntries
[Index
].type
|| !gHandleEntries
[Index
].ptr
)
315 generation
= (UINT_PTR
)handle
>> 16;
317 if (generation
== gHandleEntries
[Index
].generation
|| !generation
|| generation
== 0xffff)
318 return &gHandleEntries
[Index
];
324 * Decide whether an object is located on the desktop or shared heap
326 static const BOOL g_ObjectHeapTypeShared
[otEvent
+ 1] =
328 FALSE
, /* otFree (not used) */
329 FALSE
, /* otWindow */
330 TRUE
, /* otMenu FALSE */
331 TRUE
, /* otCursorIcon */
334 FALSE
, /* (not used) */
335 FALSE
, /* otCallProc */
337 FALSE
, /* (not used) */
338 FALSE
, /* (not used) */
339 FALSE
, /* (not used) */
340 TRUE
, /* otMonitor */
341 FALSE
, /* (not used) */
342 FALSE
, /* (not used) */
347 // Validate Handle and return the pointer to the object.
351 ValidateHandle(HANDLE handle
, UINT uType
)
354 PUSER_HANDLE_ENTRY pEntry
;
356 ASSERT(uType
<= otEvent
);
358 pEntry
= GetUser32Handle(handle
);
360 if (pEntry
&& uType
== 0)
361 uType
= pEntry
->type
;
363 // Must have an entry and must be the same type!
365 (pEntry
->type
!= uType
) ||
367 (pEntry
->flags
& HANDLEENTRY_INDESTROY
) )
370 { // Test (with wine too) confirms these results!
372 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
375 SetLastError(ERROR_INVALID_MENU_HANDLE
);
378 SetLastError(ERROR_INVALID_CURSOR_HANDLE
);
381 SetLastError(ERROR_INVALID_DWP_HANDLE
);
384 SetLastError(ERROR_INVALID_HOOK_HANDLE
);
387 SetLastError(ERROR_INVALID_ACCEL_HANDLE
);
390 SetLastError(ERROR_INVALID_HANDLE
);
396 if (g_ObjectHeapTypeShared
[uType
])
397 ret
= SharedPtrToUser(pEntry
->ptr
);
399 ret
= DesktopPtrToUser(pEntry
->ptr
);
405 // Validate Handle and return the pointer to the object.
409 ValidateHandleNoErr(HANDLE handle
, UINT uType
)
412 PUSER_HANDLE_ENTRY pEntry
;
414 ASSERT(uType
<= otEvent
);
416 pEntry
= GetUser32Handle(handle
);
418 if (pEntry
&& uType
== 0)
419 uType
= pEntry
->type
;
421 // Must have an entry and must be the same type!
422 if ( (!pEntry
) || (pEntry
->type
!= uType
) || !pEntry
->ptr
)
425 if (g_ObjectHeapTypeShared
[uType
])
426 ret
= SharedPtrToUser(pEntry
->ptr
);
428 ret
= DesktopPtrToUser(pEntry
->ptr
);
434 // Validate a callproc handle and return the pointer to the object.
438 ValidateCallProc(HANDLE hCallProc
)
440 PUSER_HANDLE_ENTRY pEntry
;
442 PCALLPROCDATA CallProc
= ValidateHandle(hCallProc
, otCallProc
);
444 pEntry
= GetUser32Handle(hCallProc
);
446 if (CallProc
!= NULL
&& pEntry
->ppi
== g_ppi
)
454 // Validate a window handle and return the pointer to the object.
458 ValidateHwnd(HWND hwnd
)
461 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
462 ASSERT(ClientInfo
!= NULL
);
464 /* See if the window is cached */
465 if (hwnd
== ClientInfo
->CallbackWnd
.hWnd
)
466 return ClientInfo
->CallbackWnd
.pWnd
;
468 Wnd
= ValidateHandle((HANDLE
)hwnd
, otWindow
);
478 // Validate a window handle and return the pointer to the object.
482 ValidateHwndNoErr(HWND hwnd
)
485 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
486 ASSERT(ClientInfo
!= NULL
);
488 /* See if the window is cached */
489 if (hwnd
== ClientInfo
->CallbackWnd
.hWnd
)
490 return ClientInfo
->CallbackWnd
.pWnd
;
492 Wnd
= ValidateHandleNoErr((HANDLE
)hwnd
, otWindow
);
503 GetThreadDesktopWnd(VOID
)
505 PWND Wnd
= GetThreadDesktopInfo()->spwnd
;
507 Wnd
= DesktopPtrToUser(Wnd
);
512 // Validate a window handle and return the pointer to the object.
516 ValidateHwndOrDesk(HWND hwnd
)
518 if (hwnd
== HWND_DESKTOP
)
519 return GetThreadDesktopWnd();
521 return ValidateHwnd(hwnd
);
527 DWORD WINAPI
WCSToMBEx(WORD CodePage
,LPWSTR UnicodeString
,LONG UnicodeSize
,LPSTR
*MBString
,LONG MBSize
,BOOL Allocate
)
530 if (UnicodeSize
== -1)
532 UnicodeSize
= wcslen(UnicodeString
)+1;
540 MBSize
= UnicodeSize
* 2;
544 LPSTR SafeString
= RtlAllocateHeap(GetProcessHeap(), 0, MBSize
);
545 if (SafeString
== NULL
)
547 *MBString
= SafeString
;
551 RtlUnicodeToMultiByteN(*MBString
,MBSize
,&Size
,UnicodeString
,UnicodeSize
);
555 WideCharToMultiByte(CodePage
,0,UnicodeString
,UnicodeSize
,*MBString
,MBSize
,0,0);
563 DWORD WINAPI
MBToWCSEx(WORD CodePage
,LPSTR MBString
,LONG MBSize
,LPWSTR
*UnicodeString
,LONG UnicodeSize
,BOOL Allocate
)
568 MBSize
= strlen(MBString
)+1;
570 if (UnicodeSize
== -1)
576 UnicodeSize
= MBSize
;
580 LPWSTR SafeString
= RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize
);
581 if (SafeString
== NULL
)
583 *UnicodeString
= SafeString
;
585 UnicodeSize
*= sizeof(WCHAR
);
588 RtlMultiByteToUnicodeN(*UnicodeString
,UnicodeSize
,&Size
,MBString
,MBSize
);
592 Size
= MultiByteToWideChar(CodePage
,0,MBString
,MBSize
,*UnicodeString
,UnicodeSize
);