ReactOS  0.4.14-dev-554-g2f8d847
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 {
83 DWORD dwWin32Error;
85 
86 
88  hObject, // Object Handle
89  *pSecurityInfo, // Security Information
90  pSecurityDescriptor,// Security Descriptor
91  dwLength, // Buffer Length
92  pdwLengthNeeded // Actual Length
93  );
94 
95  if ( ! NT_SUCCESS( Status ) ) {
96  dwWin32Error = RtlNtStatusToDosError( Status );
97  NtCurrentTeb()->LastErrorValue = dwWin32Error;
98  return FALSE;
99  }
100 
101  return TRUE;
102 }
103 
104 
105 /*
106  * SetUserObjectSecurity
107  *
108  * Sets new security descriptor to user object specified by
109  * handle 'hObject'. Descriptor must be in self-relative format.
110  *
111  * Arguments:
112  * 1) hObject - handle to an object to set information for
113  * 2) pSecurityInfo - type of information to apply
114  * 3) pSecurityDescriptor - buffer which descriptor to set
115  *
116  * Return Vaules:
117  * TRUE on success
118  * FALSE on failure, call GetLastError() for more information
119  */
120 /*
121  * @implemented
122  */
123 BOOL
124 WINAPI
126  IN HANDLE hObject,
127  IN PSECURITY_INFORMATION pSecurityInfo,
128  IN PSECURITY_DESCRIPTOR pSecurityDescriptor
129 )
130 {
131 DWORD dwWin32Error;
133 
134 
136  hObject, // Object Handle
137  *pSecurityInfo, // Security Information
138  pSecurityDescriptor // Security Descriptor
139  );
140 
141  if ( ! NT_SUCCESS( Status ) ) {
142  dwWin32Error = RtlNtStatusToDosError( Status );
143  NtCurrentTeb()->LastErrorValue = dwWin32Error;
144  return FALSE;
145  }
146 
147  return TRUE;
148 }
149 
150 /*
151  * @implemented
152  */
153 BOOL
154 WINAPI
156  BOOL bConvert)
157 {
158  PTHREADINFO ti = (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo;
159  if (ti == NULL)
160  {
161  if(bConvert)
162  {
164  if ((PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo) return TRUE;
165  else
167  }
168  return FALSE;
169  }
170  else
171  return TRUE;
172 }
173 
174 BOOL
175 FASTCALL
177 {
178  if (Wnd->head.pti == (PTHREADINFO)NtCurrentTeb()->Win32ThreadInfo)
179  return TRUE;
180  else
183 }
184 
185 BOOL
186 FASTCALL
188 {
189  UINT bit;
190  bit = 1 << LOWORD(Flag);
191  switch(HIWORD(Flag))
192  {
193  case 0:
194  return (pWnd->state & bit);
195  case 1:
196  return (pWnd->state2 & bit);
197  case 2:
198  return (pWnd->ExStyle2 & bit);
199  }
200  return FALSE;
201 }
202 
204 FASTCALL
206 {
207  INT Index;
209 
210  if (!handle) return NULL;
211 
212  Index = (((UINT_PTR)handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
213 
214  if (Index < 0 || Index >= gHandleTable->nb_handles)
215  return NULL;
216 
218  return NULL;
219 
220  generation = (UINT_PTR)handle >> 16;
221 
223  return &gHandleEntries[Index];
224 
225  return NULL;
226 }
227 
228 /*
229  * Decide whether an object is located on the desktop or shared heap
230  */
232 {
233  FALSE, /* TYPE_FREE (not used) */
234  FALSE, /* TYPE_WINDOW */
235  FALSE, /* TYPE_MENU */
236  TRUE, /* TYPE_CURSOR */
237  TRUE, /* TYPE_SETWINDOWPOS */
238  FALSE, /* TYPE_HOOK */
239  TRUE, /* TYPE_CLIPDATA */
240  FALSE, /* TYPE_CALLPROC */
241  TRUE, /* TYPE_ACCELTABLE */
242  FALSE, /* TYPE_DDEACCESS */
243  FALSE, /* TYPE_DDECONV */
244  FALSE, /* TYPE_DDEXACT */
245  TRUE, /* TYPE_MONITOR */
246  TRUE, /* TYPE_KBDLAYOUT */
247  TRUE, /* TYPE_KBDFILE */
248  TRUE, /* TYPE_WINEVENTHOOK */
249  TRUE, /* TYPE_TIMER */
250  FALSE, /* TYPE_INPUTCONTEXT */
251  FALSE, /* TYPE_HIDDATA */
252  FALSE, /* TYPE_DEVICEINFO */
253  FALSE, /* TYPE_TOUCHINPUTINFO */
254  FALSE, /* TYPE_GESTUREINFOOBJ */
255 };
256 
257 //
258 // Validate Handle and return the pointer to the object.
259 //
260 PVOID
261 FASTCALL
263 {
264  PVOID ret;
265  PUSER_HANDLE_ENTRY pEntry;
266 
267  ASSERT(uType < TYPE_CTYPES);
268 
269  pEntry = GetUser32Handle(handle);
270 
271  if (pEntry && uType == 0)
272  uType = pEntry->type;
273 
274 // Must have an entry and must be the same type!
275  if ( (!pEntry) ||
276  (pEntry->type != uType) ||
277  !pEntry->ptr ||
278  (pEntry->flags & HANDLEENTRY_DESTROY) || (pEntry->flags & HANDLEENTRY_INDESTROY) )
279  {
280  switch ( uType )
281  { // Test (with wine too) confirms these results!
282  case TYPE_WINDOW:
284  break;
285  case TYPE_MENU:
287  break;
288  case TYPE_CURSOR:
290  break;
291  case TYPE_SETWINDOWPOS:
293  break;
294  case TYPE_HOOK:
296  break;
297  case TYPE_ACCELTABLE:
299  break;
300  default:
302  break;
303  }
304  return NULL;
305  }
306 
307  if (g_ObjectHeapTypeShared[uType])
308  ret = SharedPtrToUser(pEntry->ptr);
309  else
310  ret = DesktopPtrToUser(pEntry->ptr);
311 
312  return ret;
313 }
314 
315 //
316 // Validate Handle and return the pointer to the object.
317 //
318 PVOID
319 FASTCALL
321 {
322  PVOID ret;
323  PUSER_HANDLE_ENTRY pEntry;
324 
325  ASSERT(uType < TYPE_CTYPES);
326 
327  pEntry = GetUser32Handle(handle);
328 
329  if (pEntry && uType == 0)
330  uType = pEntry->type;
331 
332 // Must have an entry and must be the same type!
333  if ( (!pEntry) || (pEntry->type != uType) || !pEntry->ptr )
334  return NULL;
335 
336  if (g_ObjectHeapTypeShared[uType])
337  ret = SharedPtrToUser(pEntry->ptr);
338  else
339  ret = DesktopPtrToUser(pEntry->ptr);
340 
341  return ret;
342 }
343 
344 //
345 // Validate a callproc handle and return the pointer to the object.
346 //
348 FASTCALL
350 {
351  PUSER_HANDLE_ENTRY pEntry;
352 
353  PCALLPROCDATA CallProc = ValidateHandle(hCallProc, TYPE_CALLPROC);
354 
355  pEntry = GetUser32Handle(hCallProc);
356 
357  if (CallProc != NULL && pEntry->ppi == g_ppi)
358  return CallProc;
359 
360  return NULL;
361 }
362 
363 
364 //
365 // Validate a window handle and return the pointer to the object.
366 //
367 PWND
368 FASTCALL
370 {
372  ASSERT(ClientInfo != NULL);
373 
374  /* See if the window is cached */
375  if (hwnd && hwnd == ClientInfo->CallbackWnd.hWnd)
376  return ClientInfo->CallbackWnd.pWnd;
377 
379 }
380 
381 //
382 // Validate a window handle and return the pointer to the object.
383 //
384 PWND
385 FASTCALL
387 {
388  PWND Wnd;
390  ASSERT(ClientInfo != NULL);
391 
392  /* See if the window is cached */
393  if (hwnd == ClientInfo->CallbackWnd.hWnd)
394  return ClientInfo->CallbackWnd.pWnd;
395 
397  if (Wnd != NULL)
398  {
399  return Wnd;
400  }
401 
402  return NULL;
403 }
404 
405 PWND
406 FASTCALL
408 {
410  if (Wnd != NULL)
411  Wnd = DesktopPtrToUser(Wnd);
412  return Wnd;
413 }
414 
415 //
416 // Validate a window handle and return the pointer to the object.
417 //
418 PWND
419 FASTCALL
421 {
422  if (hwnd == HWND_DESKTOP)
423  return GetThreadDesktopWnd();
424 
425  return ValidateHwnd(hwnd);
426 }
427 
428 /*
429  * @implemented
430  */
431 DWORD WINAPI WCSToMBEx(WORD CodePage,LPWSTR UnicodeString,LONG UnicodeSize,LPSTR *MBString,LONG MBSize,BOOL Allocate)
432 {
433  DWORD Size;
434  if (UnicodeSize == -1)
435  {
436  UnicodeSize = wcslen(UnicodeString)+1;
437  }
438  if (MBSize == -1)
439  {
440  if (!Allocate)
441  {
442  return 0;
443  }
444  MBSize = UnicodeSize * 2;
445  }
446  if (Allocate)
447  {
448  LPSTR SafeString = RtlAllocateHeap(GetProcessHeap(), 0, MBSize);
449  if (SafeString == NULL)
450  return 0;
451  *MBString = SafeString;
452  }
453  if (CodePage == 0)
454  {
455  RtlUnicodeToMultiByteN(*MBString,MBSize,&Size,UnicodeString,UnicodeSize);
456  }
457  else
458  {
459  WideCharToMultiByte(CodePage,0,UnicodeString,UnicodeSize,*MBString,MBSize,0,0);
460  }
461  return UnicodeSize;
462 }
463 
464 /*
465  * @implemented
466  */
467 DWORD WINAPI MBToWCSEx(WORD CodePage,LPSTR MBString,LONG MBSize,LPWSTR *UnicodeString,LONG UnicodeSize,BOOL Allocate)
468 {
469  DWORD Size;
470  if (MBSize == -1)
471  {
472  MBSize = strlen(MBString)+1;
473  }
474  if (UnicodeSize == -1)
475  {
476  if (!Allocate)
477  {
478  return 0;
479  }
480  UnicodeSize = MBSize;
481  }
482  if (Allocate)
483  {
484  LPWSTR SafeString = RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize);
485  if (SafeString == NULL)
486  return 0;
487  *UnicodeString = SafeString;
488  }
489  UnicodeSize *= sizeof(WCHAR);
490  if (CodePage == 0)
491  {
492  RtlMultiByteToUnicodeN(*UnicodeString,UnicodeSize,&Size,MBString,MBSize);
493  }
494  else
495  {
496  Size = MultiByteToWideChar(CodePage,0,MBString,MBSize,*UnicodeString,UnicodeSize);
497  }
498  return Size;
499 }
unsigned __int3264 UINT_PTR
Definition: activex.cpp:275
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define IN
Definition: typedefs.h:38
#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 TRUE
Definition: types.h:120
#define QUERY_WINDOW_UNIQUE_PROCESS_ID
Definition: ntuser.h:2796
BOOL WINAPI SetUserObjectSecurity(IN HANDLE hObject, IN PSECURITY_INFORMATION pSecurityInfo, IN PSECURITY_DESCRIPTOR pSecurityDescriptor)
Definition: misc.c:125
#define WideCharToMultiByte
Definition: compat.h:101
DWORD ExStyle2
Definition: ntuser.h:709
PTHREADINFO GetW32ThreadInfo(VOID)
Definition: misc.c:781
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
NTSYSAPI NTSTATUS WINAPI RtlMultiByteToUnicodeN(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD)
unsigned char type
Definition: ntuser.h:28
PCALLPROCDATA FASTCALL ValidateCallProc(HANDLE hCallProc)
Definition: misc.c:349
VOID WINAPI UserSetLastError(IN DWORD dwErrCode)
Definition: misc.c:13
#define ERROR_INVALID_HANDLE
Definition: compat.h:88
LONG NTSTATUS
Definition: precomp.h:26
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define GetWin32ClientInfo()
Definition: ntuser.h:320
#define FIRST_USER_HANDLE
Definition: ntuser.h:13
char * LPSTR
Definition: xmlstorage.h:182
HANDLE UniqueProcess
Definition: compat.h:482
#define FASTCALL
Definition: nt_native.h:50
struct _PROCESSINFO * ppi
Definition: ntuser.h:26
int32_t INT
Definition: typedefs.h:56
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:656
CLIENT_DATA ClientInfo
BOOL WINAPI IsGUIThread(BOOL bConvert)
Definition: misc.c:155
DWORD_PTR NTAPI NtUserQueryWindow(HWND hWnd, DWORD Index)
Definition: window.c:4001
static __inline PVOID DesktopPtrToUser(PVOID Ptr)
Definition: user_x.h:12
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:659
smooth NULL
Definition: ftsmooth.c:416
PVOID FASTCALL ValidateHandleNoErr(HANDLE handle, UINT uType)
Definition: misc.c:320
DWORD WINAPI WCSToMBEx(WORD CodePage, LPWSTR UnicodeString, LONG UnicodeSize, LPSTR *MBString, LONG MBSize, BOOL Allocate)
Definition: misc.c:431
PUSER_HANDLE_ENTRY FASTCALL GetUser32Handle(HANDLE handle)
Definition: misc.c:205
PPROCESSINFO g_ppi
Definition: dllmain.c:29
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
Definition: ntuser.h:19
static const BOOL g_ObjectHeapTypeShared[TYPE_CTYPES]
Definition: misc.c:231
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:1176
#define GetProcessHeap()
Definition: compat.h:403
PVOID FASTCALL ValidateHandle(HANDLE handle, UINT uType)
Definition: misc.c:262
__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
#define WINAPI
Definition: msvc.h:8
Definition: xml2sdb.h:79
static const UCHAR Index[8]
Definition: usbohci.c:18
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:386
#define SetLastError(x)
Definition: compat.h:417
BOOL FASTCALL TestState(PWND pWnd, UINT Flag)
Definition: misc.c:187
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
int ret
NTSYSAPI NTSTATUS NTAPI RtlUnicodeToMultiByteN(PCHAR MbString, ULONG MbSize, PULONG ResultSize, PCWCH UnicodeString, ULONG UnicodeSize)
struct _WND * spwnd
Definition: ntuser.h:136
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
uint32_t DWORD_PTR
Definition: typedefs.h:63
BOOL FASTCALL TestWindowProcess(PWND Wnd)
Definition: misc.c:176
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
Definition: ntuser.h:657
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:16
unsigned short USHORT
Definition: pedump.c:61
_In_ uint64_t _In_ uint64_t _In_ uint64_t generation
Definition: btrfs.c:2854
PWND FASTCALL ValidateHwnd(HWND hwnd)
Definition: misc.c:369
DWORD state
Definition: ntuser.h:665
DWORD WINAPI MBToWCSEx(WORD CodePage, LPSTR MBString, LONG MBSize, LPWSTR *UnicodeString, LONG UnicodeSize, BOOL Allocate)
Definition: misc.c:467
static __inline PVOID SharedPtrToUser(PVOID Ptr)
Definition: user_x.h:4
#define HWND_DESKTOP
Definition: winuser.h:1195
unsigned int UINT
Definition: ndis.h:50
DWORD * PDWORD
Definition: pedump.c:68
#define MultiByteToWideChar
Definition: compat.h:100
PWND FASTCALL GetThreadDesktopWnd(VOID)
Definition: misc.c:407
#define OUT
Definition: typedefs.h:39
#define HIWORD(l)
Definition: typedefs.h:246
DWORD state2
Definition: ntuser.h:666
void * ptr
Definition: ntuser.h:21
#define HANDLEENTRY_INDESTROY
Definition: ntuser.h:17
PWND FASTCALL ValidateHwndOrDesk(HWND hwnd)
Definition: misc.c:420
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
PUSER_HANDLE_TABLE gHandleTable
Definition: object.c:13
unsigned char flags
Definition: ntuser.h:29
#define LOWORD(l)
Definition: pedump.c:82
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
PUSER_HANDLE_ENTRY gHandleEntries
Definition: dllmain.c:31