ReactOS  0.4.15-dev-3441-g67ad4e7
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 
11 VOID
12 WINAPI
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 
23 VOID
24 WINAPI
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 - reseives actual size of descriptor
65  *
66  * Return Vaules:
67  * TRUE on success
68  * FALSE on failure, call GetLastError() for more information
69  */
70 /*
71  * @implemented
72  */
73 BOOL
74 WINAPI
76  IN HANDLE hObject,
77  IN PSECURITY_INFORMATION pSecurityInfo,
78  OUT PSECURITY_DESCRIPTOR pSecurityDescriptor,
80  OUT PDWORD pdwLengthNeeded
81 )
82 {
84 
85  Status = NtQuerySecurityObject(hObject,
86  *pSecurityInfo,
87  pSecurityDescriptor,
88  dwLength,
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  */
118 BOOL
119 WINAPI
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  */
143 BOOL
144 WINAPI
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 
164 BOOL
165 FASTCALL
167 {
168  if (Wnd->head.pti == (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo)
169  return TRUE;
170  else
173 }
174 
175 BOOL
176 FASTCALL
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 
194 FASTCALL
196 {
197  INT Index;
199 
200  if (!handle) return NULL;
201 
202  Index = (((UINT_PTR)handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
203 
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 //
250 PVOID
251 FASTCALL
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;
281  case TYPE_SETWINDOWPOS:
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
300  ret = DesktopPtrToUser(pEntry->ptr);
301 
302  return ret;
303 }
304 
305 //
306 // Validate Handle and return the pointer to the object.
307 //
308 PVOID
309 FASTCALL
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
329  ret = DesktopPtrToUser(pEntry->ptr);
330 
331  return ret;
332 }
333 
334 //
335 // Validate a callproc handle and return the pointer to the object.
336 //
338 FASTCALL
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 //
357 PWND
358 FASTCALL
360 {
362  ASSERT(ClientInfo != NULL);
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 //
374 PWND
375 FASTCALL
377 {
378  PWND Wnd;
380  ASSERT(ClientInfo != NULL);
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 
395 PWND
396 FASTCALL
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 //
408 PWND
409 FASTCALL
411 {
412  if (hwnd == HWND_DESKTOP)
413  return GetThreadDesktopWnd();
414 
415  return ValidateHwnd(hwnd);
416 }
417 
418 /*
419  * @implemented
420  */
421 DWORD 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  */
457 DWORD 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 }
unsigned __int3264 UINT_PTR
Definition: activex.cpp:275
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define IN
Definition: typedefs.h:39
#define ERROR_INVALID_CURSOR_HANDLE
Definition: winerror.h:883
#define ERROR_INVALID_DWP_HANDLE
Definition: winerror.h:886
#define ERROR_INVALID_ACCEL_HANDLE
Definition: winerror.h:884
#define ERROR_INVALID_HOOK_HANDLE
Definition: winerror.h:885
#define QUERY_WINDOW_UNIQUE_PROCESS_ID
Definition: ntuser.h:2900
BOOL WINAPI SetUserObjectSecurity(IN HANDLE hObject, IN PSECURITY_INFORMATION pSecurityInfo, IN PSECURITY_DESCRIPTOR pSecurityDescriptor)
Definition: misc.c:120
#define WideCharToMultiByte
Definition: compat.h:111
DWORD ExStyle2
Definition: ntuser.h:715
PTHREADINFO GetW32ThreadInfo(VOID)
Definition: misc.c:782
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define TRUE
Definition: types.h:120
PCALLPROCDATA FASTCALL ValidateCallProc(HANDLE hCallProc)
Definition: misc.c:339
VOID WINAPI UserSetLastError(IN DWORD dwErrCode)
Definition: misc.c:13
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
LONG NTSTATUS
Definition: precomp.h:26
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
#define GetWin32ClientInfo()
Definition: ntuser.h:326
#define FIRST_USER_HANDLE
Definition: ntuser.h:10
char * LPSTR
Definition: xmlstorage.h:182
HANDLE UniqueProcess
Definition: compat.h:684
#define FASTCALL
Definition: nt_native.h:50
int32_t INT
Definition: typedefs.h:58
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:814
CLIENT_DATA ClientInfo
BOOL WINAPI IsGUIThread(BOOL bConvert)
Definition: misc.c:145
DWORD_PTR NTAPI NtUserQueryWindow(HWND hWnd, DWORD Index)
Definition: window.c:3929
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
static __inline PVOID DesktopPtrToUser(PVOID Ptr)
Definition: user_x.h:12
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
#define ERROR_INVALID_MENU_HANDLE
Definition: winerror.h:882
static PVOID ptr
Definition: dispmode.c:27
VOID WINAPI UserSetLastNTError(IN NTSTATUS Status)
Definition: misc.c:25
THRDESKHEAD head
Definition: ntuser.h:665
PVOID FASTCALL ValidateHandleNoErr(HANDLE handle, UINT uType)
Definition: misc.c:310
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
PPROCESSINFO g_ppi
Definition: dllmain.c:29
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
Status
Definition: gdiplustypes.h:24
Definition: ntuser.h:16
static const BOOL g_ObjectHeapTypeShared[TYPE_CTYPES]
Definition: misc.c:221
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 ERROR_INVALID_WINDOW_HANDLE
Definition: winerror.h:881
struct _THREADINFO * PTHREADINFO
Definition: ntwin32.h:6
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1163
#define GetProcessHeap()
Definition: compat.h:595
NTSYSAPI NTSTATUS WINAPI RtlMultiByteToUnicodeN(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD)
#define ASSERT(a)
Definition: mode.c:44
PVOID FASTCALL ValidateHandle(HANDLE handle, UINT uType)
Definition: misc.c:252
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD DWORD * dwLength
Definition: fusion.c:85
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
DWORD * PSECURITY_INFORMATION
Definition: ms-dtyp.idl:311
_In_ WDFCOLLECTION _In_ ULONG Index
#define WINAPI
Definition: msvc.h:6
Definition: xml2sdb.h:79
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle, SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR SecurityDescriptor)
PWND FASTCALL ValidateHwndNoErr(HWND hwnd)
Definition: misc.c:376
#define SetLastError(x)
Definition: compat.h:611
BOOL FASTCALL TestState(PWND pWnd, UINT Flag)
Definition: misc.c:177
int ret
NTSYSAPI NTSTATUS NTAPI RtlUnicodeToMultiByteN(PCHAR MbString, ULONG MbSize, PULONG ResultSize, PCWCH UnicodeString, ULONG UnicodeSize)
struct _WND * spwnd
Definition: ntuser.h:133
uint32_t DWORD_PTR
Definition: typedefs.h:65
BOOL FASTCALL TestWindowProcess(PWND Wnd)
Definition: misc.c:166
Definition: ntuser.h:663
DWORD_PTR APIENTRY NtUserGetThreadState(DWORD Routine)
Definition: misc.c:231
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
#define HANDLEENTRY_DESTROY
Definition: ntuser.h:13
unsigned short USHORT
Definition: pedump.c:61
_In_ uint64_t _In_ uint64_t _In_ uint64_t generation
Definition: btrfs.c:2929
PWND FASTCALL ValidateHwnd(HWND hwnd)
Definition: misc.c:359
DWORD state
Definition: ntuser.h:671
DWORD WINAPI MBToWCSEx(WORD CodePage, LPSTR MBString, LONG MBSize, LPWSTR *UnicodeString, LONG UnicodeSize, BOOL Allocate)
Definition: misc.c:457
static __inline PVOID SharedPtrToUser(PVOID Ptr)
Definition: user_x.h:4
#define HWND_DESKTOP
Definition: winuser.h:1199
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
DWORD * PDWORD
Definition: pedump.c:68
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define MultiByteToWideChar
Definition: compat.h:110
PWND FASTCALL GetThreadDesktopWnd(VOID)
Definition: misc.c:397
#define OUT
Definition: typedefs.h:40
#define HIWORD(l)
Definition: typedefs.h:247
DWORD state2
Definition: ntuser.h:672
#define HANDLEENTRY_INDESTROY
Definition: ntuser.h:14
PWND FASTCALL ValidateHwndOrDesk(HWND hwnd)
Definition: misc.c:410
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
BOOL WINAPI GetUserObjectSecurity(IN HANDLE hObject, IN PSECURITY_INFORMATION pSecurityInfo, OUT PSECURITY_DESCRIPTOR pSecurityDescriptor, IN DWORD dwLength, OUT PDWORD pdwLengthNeeded)
Definition: misc.c:75
static __inline PDESKTOPINFO GetThreadDesktopInfo(VOID)
Definition: user_x.h:43
WCHAR * LPWSTR
Definition: xmlstorage.h:184
PLIST_ENTRY pEntry
Definition: fxioqueue.cpp:4484
PUSER_HANDLE_TABLE gHandleTable
Definition: object.c:13
#define LOWORD(l)
Definition: pedump.c:82
PUSER_HANDLE_ENTRY gHandleEntries
Definition: dllmain.c:33