ReactOS 0.4.15-dev-7788-g1ad9096
misc.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS user32.dll
4 * FILE: win32ss/user/user32/misc/misc.c
5 * PURPOSE: Misc
6 * PROGRAMMER: Thomas Weidenmueller (w3seek@users.sourceforge.net)
7 */
8
9#include <user32.h>
10
11VOID
14{
15 /*
16 * Equivalent of SetLastError in kernel32, but without breaking
17 * into the debugger nor checking whether the last old error is
18 * the same as the one we are going to set.
19 */
20 NtCurrentTeb()->LastErrorValue = dwErrCode;
21}
22
23VOID
26{
27 /*
28 * Equivalent of BaseSetLastNTError in kernel32, but using
29 * UserSetLastError: convert from NT to Win32, then set.
30 */
32}
33
34
37{
38 PTHREADINFO ti;
39
40 ti = (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo;
41 if (ti == NULL)
42 {
43 /* create the THREADINFO structure */
45 ti = (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo;
46 }
47
48 return ti;
49}
50
51
52/*
53 * GetUserObjectSecurity
54 *
55 * Retrieves security information for user object specified
56 * with handle 'hObject'. Descriptor returned in self-relative
57 * format.
58 *
59 * Arguments:
60 * 1) hObject - handle to an object to retrieve information for
61 * 2) pSecurityInfo - type of information to retrieve
62 * 3) pSecurityDescriptor - buffer which receives descriptor
63 * 4) dwLength - size, in bytes, of buffer 'pSecurityDescriptor'
64 * 5) pdwLengthNeeded - receives actual size of the descriptor
65 *
66 * Return Vaules:
67 * TRUE on success
68 * FALSE on failure, call GetLastError() for more information
69 */
70/*
71 * @implemented
72 */
73BOOL
76 IN HANDLE hObject,
77 IN PSECURITY_INFORMATION pSecurityInfo,
78 OUT PSECURITY_DESCRIPTOR pSecurityDescriptor,
80 OUT PDWORD pdwLengthNeeded
81)
82{
84
86 *pSecurityInfo,
87 pSecurityDescriptor,
89 pdwLengthNeeded);
90 if (!NT_SUCCESS(Status))
91 {
93 return FALSE;
94 }
95
96 return TRUE;
97}
98
99
100/*
101 * SetUserObjectSecurity
102 *
103 * Sets new security descriptor to user object specified by
104 * handle 'hObject'. Descriptor must be in self-relative format.
105 *
106 * Arguments:
107 * 1) hObject - handle to an object to set information for
108 * 2) pSecurityInfo - type of information to apply
109 * 3) pSecurityDescriptor - buffer which descriptor to set
110 *
111 * Return Vaules:
112 * TRUE on success
113 * FALSE on failure, call GetLastError() for more information
114 */
115/*
116 * @implemented
117 */
118BOOL
119WINAPI
121 IN HANDLE hObject,
122 IN PSECURITY_INFORMATION pSecurityInfo,
123 IN PSECURITY_DESCRIPTOR pSecurityDescriptor
124)
125{
127
128 Status = NtSetSecurityObject(hObject,
129 *pSecurityInfo,
130 pSecurityDescriptor);
131 if (!NT_SUCCESS(Status))
132 {
134 return FALSE;
135 }
136
137 return TRUE;
138}
139
140/*
141 * @implemented
142 */
143BOOL
144WINAPI
146 BOOL bConvert)
147{
148 PTHREADINFO ti = (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo;
149 if (ti == NULL)
150 {
151 if(bConvert)
152 {
154 if ((PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo) return TRUE;
155 else
157 }
158 return FALSE;
159 }
160 else
161 return TRUE;
162}
163
164BOOL
167{
168 if (Wnd->head.pti == (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo)
169 return TRUE;
170 else
173}
174
175BOOL
178{
179 UINT bit;
180 bit = 1 << LOWORD(Flag);
181 switch(HIWORD(Flag))
182 {
183 case 0:
184 return (pWnd->state & bit);
185 case 1:
186 return (pWnd->state2 & bit);
187 case 2:
188 return (pWnd->ExStyle2 & bit);
189 }
190 return FALSE;
191}
192
196{
197 INT Index;
199
200 if (!handle) return NULL;
201
202 Index = (((UINT_PTR)handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
203
204 if (Index < 0 || Index >= gHandleTable->nb_handles)
205 return NULL;
206
208 return NULL;
209
210 generation = (UINT_PTR)handle >> 16;
211
213 return &gHandleEntries[Index];
214
215 return NULL;
216}
217
218/*
219 * Decide whether an object is located on the desktop or shared heap
220 */
222{
223 FALSE, /* TYPE_FREE (not used) */
224 FALSE, /* TYPE_WINDOW */
225 FALSE, /* TYPE_MENU */
226 TRUE, /* TYPE_CURSOR */
227 TRUE, /* TYPE_SETWINDOWPOS */
228 FALSE, /* TYPE_HOOK */
229 TRUE, /* TYPE_CLIPDATA */
230 FALSE, /* TYPE_CALLPROC */
231 TRUE, /* TYPE_ACCELTABLE */
232 FALSE, /* TYPE_DDEACCESS */
233 FALSE, /* TYPE_DDECONV */
234 FALSE, /* TYPE_DDEXACT */
235 TRUE, /* TYPE_MONITOR */
236 TRUE, /* TYPE_KBDLAYOUT */
237 TRUE, /* TYPE_KBDFILE */
238 TRUE, /* TYPE_WINEVENTHOOK */
239 TRUE, /* TYPE_TIMER */
240 FALSE, /* TYPE_INPUTCONTEXT */
241 FALSE, /* TYPE_HIDDATA */
242 FALSE, /* TYPE_DEVICEINFO */
243 FALSE, /* TYPE_TOUCHINPUTINFO */
244 FALSE, /* TYPE_GESTUREINFOOBJ */
245};
246
247//
248// Validate Handle and return the pointer to the object.
249//
250PVOID
253{
254 PVOID ret;
256
257 ASSERT(uType < TYPE_CTYPES);
258
260
261 if (pEntry && uType == 0)
262 uType = pEntry->type;
263
264// Must have an entry and must be the same type!
265 if ( (!pEntry) ||
266 (pEntry->type != uType) ||
267 !pEntry->ptr ||
268 (pEntry->flags & HANDLEENTRY_DESTROY) || (pEntry->flags & HANDLEENTRY_INDESTROY) )
269 {
270 switch ( uType )
271 { // Test (with wine too) confirms these results!
272 case TYPE_WINDOW:
274 break;
275 case TYPE_MENU:
277 break;
278 case TYPE_CURSOR:
280 break;
283 break;
284 case TYPE_HOOK:
286 break;
287 case TYPE_ACCELTABLE:
289 break;
290 default:
292 break;
293 }
294 return NULL;
295 }
296
297 if (g_ObjectHeapTypeShared[uType])
298 ret = SharedPtrToUser(pEntry->ptr);
299 else
301
302 return ret;
303}
304
305//
306// Validate Handle and return the pointer to the object.
307//
308PVOID
311{
312 PVOID ret;
314
315 ASSERT(uType < TYPE_CTYPES);
316
318
319 if (pEntry && uType == 0)
320 uType = pEntry->type;
321
322// Must have an entry and must be the same type!
323 if ( (!pEntry) || (pEntry->type != uType) || !pEntry->ptr )
324 return NULL;
325
326 if (g_ObjectHeapTypeShared[uType])
327 ret = SharedPtrToUser(pEntry->ptr);
328 else
330
331 return ret;
332}
333
334//
335// Validate a callproc handle and return the pointer to the object.
336//
340{
342
343 PCALLPROCDATA CallProc = ValidateHandle(hCallProc, TYPE_CALLPROC);
344
345 pEntry = GetUser32Handle(hCallProc);
346
347 if (CallProc != NULL && pEntry->ppi == g_ppi)
348 return CallProc;
349
350 return NULL;
351}
352
353
354//
355// Validate a window handle and return the pointer to the object.
356//
357PWND
360{
363
364 /* See if the window is cached */
365 if (hwnd && hwnd == ClientInfo->CallbackWnd.hWnd)
366 return ClientInfo->CallbackWnd.pWnd;
367
369}
370
371//
372// Validate a window handle and return the pointer to the object.
373//
374PWND
377{
378 PWND Wnd;
381
382 /* See if the window is cached */
383 if (hwnd == ClientInfo->CallbackWnd.hWnd)
384 return ClientInfo->CallbackWnd.pWnd;
385
387 if (Wnd != NULL)
388 {
389 return Wnd;
390 }
391
392 return NULL;
393}
394
395PWND
398{
400 if (Wnd != NULL)
401 Wnd = DesktopPtrToUser(Wnd);
402 return Wnd;
403}
404
405//
406// Validate a window handle and return the pointer to the object.
407//
408PWND
411{
412 if (hwnd == HWND_DESKTOP)
413 return GetThreadDesktopWnd();
414
415 return ValidateHwnd(hwnd);
416}
417
418/*
419 * @implemented
420 */
421DWORD WINAPI WCSToMBEx(WORD CodePage,LPWSTR UnicodeString,LONG UnicodeSize,LPSTR *MBString,LONG MBSize,BOOL Allocate)
422{
423 DWORD Size;
424 if (UnicodeSize == -1)
425 {
426 UnicodeSize = wcslen(UnicodeString)+1;
427 }
428 if (MBSize == -1)
429 {
430 if (!Allocate)
431 {
432 return 0;
433 }
434 MBSize = UnicodeSize * 2;
435 }
436 if (Allocate)
437 {
438 LPSTR SafeString = RtlAllocateHeap(GetProcessHeap(), 0, MBSize);
439 if (SafeString == NULL)
440 return 0;
441 *MBString = SafeString;
442 }
443 if (CodePage == 0)
444 {
445 RtlUnicodeToMultiByteN(*MBString,MBSize,&Size,UnicodeString,UnicodeSize);
446 }
447 else
448 {
449 WideCharToMultiByte(CodePage,0,UnicodeString,UnicodeSize,*MBString,MBSize,0,0);
450 }
451 return UnicodeSize;
452}
453
454/*
455 * @implemented
456 */
457DWORD WINAPI MBToWCSEx(WORD CodePage,LPSTR MBString,LONG MBSize,LPWSTR *UnicodeString,LONG UnicodeSize,BOOL Allocate)
458{
459 DWORD Size;
460 if (MBSize == -1)
461 {
462 MBSize = strlen(MBString)+1;
463 }
464 if (UnicodeSize == -1)
465 {
466 if (!Allocate)
467 {
468 return 0;
469 }
470 UnicodeSize = MBSize;
471 }
472 if (Allocate)
473 {
474 LPWSTR SafeString = RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize);
475 if (SafeString == NULL)
476 return 0;
477 *UnicodeString = SafeString;
478 }
479 UnicodeSize *= sizeof(WCHAR);
480 if (CodePage == 0)
481 {
482 RtlMultiByteToUnicodeN(*UnicodeString,UnicodeSize,&Size,MBString,MBSize);
483 }
484 else
485 {
486 Size = MultiByteToWideChar(CodePage,0,MBString,MBSize,*UnicodeString,UnicodeSize);
487 }
488 return Size;
489}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
LONG NTSTATUS
Definition: precomp.h:26
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define GetProcessHeap()
Definition: compat.h:736
#define SetLastError(x)
Definition: compat.h:752
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
static DWORD DWORD * dwLength
Definition: fusion.c:86
#define ValidateHwnd(hwnd)
Definition: precomp.h:85
#define ValidateHwndNoErr(hwnd)
Definition: precomp.h:84
static PVOID FASTCALL DesktopPtrToUser(PVOID ptr)
Definition: utils.c:324
_In_ uint64_t _In_ uint64_t _In_ uint64_t generation
Definition: btrfs.c:2996
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
PLIST_ENTRY pEntry
Definition: fxioqueue.cpp:4484
Status
Definition: gdiplustypes.h:25
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HANDLEENTRY_INDESTROY
Definition: ntuser.h:14
@ THREADSTATE_GETTHREADINFO
Definition: ntuser.h:2490
@ TYPE_HOOK
Definition: ntuser.h:45
@ TYPE_CTYPES
Definition: ntuser.h:62
@ TYPE_CALLPROC
Definition: ntuser.h:47
@ TYPE_CURSOR
Definition: ntuser.h:43
@ TYPE_WINDOW
Definition: ntuser.h:41
@ TYPE_ACCELTABLE
Definition: ntuser.h:48
@ TYPE_MENU
Definition: ntuser.h:42
@ TYPE_SETWINDOWPOS
Definition: ntuser.h:44
#define HANDLEENTRY_DESTROY
Definition: ntuser.h:13
DWORD_PTR NTAPI NtUserQueryWindow(HWND hWnd, DWORD Index)
Definition: window.c:4189
#define GetWin32ClientInfo()
Definition: ntuser.h:352
#define QUERY_WINDOW_UNIQUE_PROCESS_ID
Definition: ntuser.h:2827
#define FIRST_USER_HANDLE
Definition: ntuser.h:10
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
CLIENT_DATA ClientInfo
#define NtCurrentTeb
#define ASSERT(a)
Definition: mode.c:44
static PVOID ptr
Definition: dispmode.c:27
DWORD * PSECURITY_INFORMATION
Definition: ms-dtyp.idl:311
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
unsigned int UINT
Definition: ndis.h:50
_Use_decl_annotations_ NTSTATUS NTAPI RtlUnicodeToMultiByteN(_Out_ PCHAR MbString, _In_ ULONG MbSize, _Out_opt_ PULONG ResultSize, _In_ PCWCH UnicodeString, _In_ ULONG UnicodeSize)
Definition: nlsboot.c:107
_Use_decl_annotations_ NTSTATUS NTAPI RtlMultiByteToUnicodeN(_Out_ PWCH UnicodeString, _In_ ULONG UnicodeSize, _Out_opt_ PULONG ResultSize, _In_ PCCH MbString, _In_ ULONG MbSize)
Definition: nlsboot.c:62
#define FASTCALL
Definition: nt_native.h:50
struct _THREADINFO * PTHREADINFO
Definition: ntwin32.h:6
NTSTATUS NTAPI NtQuerySecurityObject(IN HANDLE Handle, IN SECURITY_INFORMATION SecurityInformation, OUT PSECURITY_DESCRIPTOR SecurityDescriptor, IN ULONG Length, OUT PULONG ResultLength)
Definition: obsecure.c:803
#define LOWORD(l)
Definition: pedump.c:82
DWORD * PDWORD
Definition: pedump.c:68
long LONG
Definition: pedump.c:60
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle, SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR SecurityDescriptor)
Definition: xml2sdb.h:80
HANDLE UniqueProcess
Definition: compat.h:825
struct _WND * spwnd
Definition: ntuser.h:137
Definition: ntuser.h:17
Definition: ntuser.h:694
DWORD ExStyle2
Definition: ntuser.h:745
THRDESKHEAD head
Definition: ntuser.h:695
DWORD state2
Definition: ntuser.h:702
DWORD state
Definition: ntuser.h:701
uint32_t DWORD_PTR
Definition: typedefs.h:65
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
#define HIWORD(l)
Definition: typedefs.h:247
#define OUT
Definition: typedefs.h:40
PPROCESSINFO g_ppi
Definition: dllmain.c:29
PUSER_HANDLE_ENTRY gHandleEntries
Definition: dllmain.c:33
static __inline PVOID SharedPtrToUser(PVOID Ptr)
Definition: user_x.h:4
static __inline PDESKTOPINFO GetThreadDesktopInfo(VOID)
Definition: user_x.h:43
int ret
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
PTHREADINFO GetW32ThreadInfo(VOID)
Definition: misc.c:805
DWORD_PTR APIENTRY NtUserGetThreadState(DWORD Routine)
Definition: misc.c:240
PUSER_HANDLE_TABLE gHandleTable
Definition: object.c:13
PWND FASTCALL GetThreadDesktopWnd(VOID)
Definition: misc.c:397
BOOL WINAPI SetUserObjectSecurity(IN HANDLE hObject, IN PSECURITY_INFORMATION pSecurityInfo, IN PSECURITY_DESCRIPTOR pSecurityDescriptor)
Definition: misc.c:120
BOOL WINAPI IsGUIThread(BOOL bConvert)
Definition: misc.c:145
static const BOOL g_ObjectHeapTypeShared[TYPE_CTYPES]
Definition: misc.c:221
DWORD WINAPI MBToWCSEx(WORD CodePage, LPSTR MBString, LONG MBSize, LPWSTR *UnicodeString, LONG UnicodeSize, BOOL Allocate)
Definition: misc.c:457
VOID WINAPI UserSetLastError(IN DWORD dwErrCode)
Definition: misc.c:13
PVOID FASTCALL ValidateHandle(HANDLE handle, UINT uType)
Definition: misc.c:252
VOID WINAPI UserSetLastNTError(IN NTSTATUS Status)
Definition: misc.c:25
PVOID FASTCALL ValidateHandleNoErr(HANDLE handle, UINT uType)
Definition: misc.c:310
BOOL WINAPI GetUserObjectSecurity(IN HANDLE hObject, IN PSECURITY_INFORMATION pSecurityInfo, OUT PSECURITY_DESCRIPTOR pSecurityDescriptor, IN DWORD dwLength, OUT PDWORD pdwLengthNeeded)
Definition: misc.c:75
PWND FASTCALL ValidateHwndOrDesk(HWND hwnd)
Definition: misc.c:410
BOOL FASTCALL TestWindowProcess(PWND Wnd)
Definition: misc.c:166
DWORD WINAPI WCSToMBEx(WORD CodePage, LPWSTR UnicodeString, LONG UnicodeSize, LPSTR *MBString, LONG MBSize, BOOL Allocate)
Definition: misc.c:421
PUSER_HANDLE_ENTRY FASTCALL GetUser32Handle(HANDLE handle)
Definition: misc.c:195
BOOL FASTCALL TestState(PWND pWnd, UINT Flag)
Definition: misc.c:177
PCALLPROCDATA FASTCALL ValidateCallProc(HANDLE hCallProc)
Definition: misc.c:339
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
#define WINAPI
Definition: msvc.h:6
#define ERROR_INVALID_ACCEL_HANDLE
Definition: winerror.h:884
#define ERROR_INVALID_DWP_HANDLE
Definition: winerror.h:886
#define ERROR_INVALID_MENU_HANDLE
Definition: winerror.h:882
#define ERROR_INVALID_WINDOW_HANDLE
Definition: winerror.h:881
#define ERROR_INVALID_CURSOR_HANDLE
Definition: winerror.h:883
#define ERROR_INVALID_HOOK_HANDLE
Definition: winerror.h:885
#define HWND_DESKTOP
Definition: winuser.h:1209
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:814
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1151
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184