ReactOS  0.4.14-dev-50-g13bb5e2
handle.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Win32 Base API
3  * LICENSE: See COPYING in the top level directory
4  * FILE: dll/win32/kernel32/client/handle.c
5  * PURPOSE: Object Handle Functions
6  * PROGRAMMERS: Ariadne ( ariadne@xs4all.nl)
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include <k32.h>
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* PRIVATE FUNCTIONS **********************************************************/
17 
18 HANDLE
20 {
21  PRTL_USER_PROCESS_PARAMETERS Ppb = NtCurrentPeb()->ProcessParameters;
22 
23  switch ((ULONG_PTR)hHandle)
24  {
25  case STD_INPUT_HANDLE: return Ppb->StandardInput;
26  case STD_OUTPUT_HANDLE: return Ppb->StandardOutput;
27  case STD_ERROR_HANDLE: return Ppb->StandardError;
28  }
29 
30  return hHandle;
31 }
32 
33 /* PUBLIC FUNCTIONS ***********************************************************/
34 
35 /*
36  * @implemented
37  */
38 BOOL
39 WINAPI
41  OUT LPDWORD lpdwFlags)
42 {
46  DWORD Flags;
47 
48  hObject = TranslateStdHandle(hObject);
49 
50  if (IsConsoleHandle(hObject))
51  {
52  return GetConsoleHandleInformation(hObject, lpdwFlags);
53  }
54 
55  Status = NtQueryObject(hObject,
56  ObjectHandleFlagInformation,
57  &HandleInfo,
59  &BytesWritten);
60  if (!NT_SUCCESS(Status))
61  {
63  return FALSE;
64  }
65 
66  Flags = 0;
67  if (HandleInfo.Inherit) Flags |= HANDLE_FLAG_INHERIT;
69  *lpdwFlags = Flags;
70  return TRUE;
71 }
72 
73 /*
74  * @implemented
75  */
76 BOOL
77 WINAPI
79  IN DWORD dwMask,
81 {
85 
86  hObject = TranslateStdHandle(hObject);
87 
88  if (IsConsoleHandle(hObject))
89  {
90  return SetConsoleHandleInformation(hObject, dwMask, dwFlags);
91  }
92 
93  Status = NtQueryObject(hObject,
94  ObjectHandleFlagInformation,
95  &HandleInfo,
97  &BytesWritten);
98  if (!NT_SUCCESS(Status))
99  {
101  return FALSE;
102  }
103 
104  if (dwMask & HANDLE_FLAG_INHERIT)
105  {
106  HandleInfo.Inherit = (dwFlags & HANDLE_FLAG_INHERIT) != 0;
107  }
108 
109  if (dwMask & HANDLE_FLAG_PROTECT_FROM_CLOSE)
110  {
112  }
113 
114  Status = NtSetInformationObject(hObject,
115  ObjectHandleFlagInformation,
116  &HandleInfo,
117  sizeof(HandleInfo));
118  if (NT_SUCCESS(Status)) return TRUE;
119 
121  return FALSE;
122 }
123 
124 /*
125  * @implemented
126  */
127 BOOL
128 WINAPI
130 {
132 
133  hObject = TranslateStdHandle(hObject);
134 
135  if (IsConsoleHandle(hObject)) return CloseConsoleHandle(hObject);
136 
137  Status = NtClose(hObject);
138  if (NT_SUCCESS(Status)) return TRUE;
139 
141  return FALSE;
142 }
143 
144 /*
145  * @implemented
146  */
147 BOOL
148 WINAPI
149 DuplicateHandle(IN HANDLE hSourceProcessHandle,
150  IN HANDLE hSourceHandle,
151  IN HANDLE hTargetProcessHandle,
152  OUT LPHANDLE lpTargetHandle,
153  IN DWORD dwDesiredAccess,
156 {
158  HANDLE hTargetHandle;
159 
160  hSourceHandle = TranslateStdHandle(hSourceHandle);
161 
162  if ((IsConsoleHandle(hSourceHandle)) &&
163  ((hSourceHandle != NtCurrentProcess()) &&
164  (hSourceHandle != NtCurrentThread())))
165  {
166  /*
167  * We can duplicate console handles only if both the source
168  * and the target processes are in fact the current process.
169  */
170  if ((hSourceProcessHandle != NtCurrentProcess()) ||
171  (hTargetProcessHandle != NtCurrentProcess()))
172  {
174  return FALSE;
175  }
176 
177  hTargetHandle = DuplicateConsoleHandle(hSourceHandle,
178  dwDesiredAccess,
180  dwOptions);
181  if (hTargetHandle != INVALID_HANDLE_VALUE)
182  {
183  if (lpTargetHandle) *lpTargetHandle = hTargetHandle;
184  return TRUE;
185  }
186 
187  return FALSE;
188  }
189 
190  Status = NtDuplicateObject(hSourceProcessHandle,
191  hSourceHandle,
192  hTargetProcessHandle,
193  lpTargetHandle,
194  dwDesiredAccess,
196  dwOptions);
197  if (NT_SUCCESS(Status)) return TRUE;
198 
200  return FALSE;
201 }
202 
203 /*
204  * @implemented
205  */
206 UINT
207 WINAPI
209 {
210  return nCount;
211 }
212 
213 /*
214  * @implemented
215  */
216 DWORD
217 WINAPI
219 {
220  /* This is Windows behavior, not a ReactOS Stub */
221  DbgPrintEx(0, 0, "Unsupported API - kernel32!GetHandleContext() called\n");
223  return FALSE;
224 }
225 
226 /*
227  * @implemented
228  */
229 HANDLE
230 WINAPI
232 {
233  /* This is Windows behavior, not a ReactOS Stub */
234  DbgPrintEx(0, 0, "Unsupported API - kernel32!CreateSocketHandle() called\n");
236  return FALSE;
237 }
238 
239 /*
240  * @implemented
241  */
242 BOOL
243 WINAPI
245  IN DWORD Context)
246 {
247  /* This is Windows behavior, not a ReactOS Stub */
248  DbgPrintEx(0, 0, "Unsupported API - kernel32!SetHandleContext() called\n");
250  return FALSE;
251 }
252 
253 /* EOF */
DWORD dwOptions
Definition: solitaire.cpp:23
HANDLE WINAPI CreateSocketHandle(VOID)
Definition: handle.c:231
#define IN
Definition: typedefs.h:38
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesWritten
Definition: fltkernel.h:1293
#define TRUE
Definition: types.h:120
DWORD WINAPI GetHandleContext(IN HANDLE Handle)
Definition: handle.c:218
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
BOOL WINAPI SetConsoleHandleInformation(IN HANDLE hHandle, IN DWORD dwMask, IN DWORD dwFlags)
Definition: console.c:495
HANDLE WINAPI DECLSPEC_HOTPATCH DuplicateConsoleHandle(HANDLE hConsole, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions)
Definition: console.c:424
#define NtCurrentThread()
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
uint32_t ULONG_PTR
Definition: typedefs.h:63
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
static BOOL bInheritHandle
Definition: pipe.c:82
unsigned int BOOL
Definition: ntddk_ex.h:94
NTSTATUS NTAPI NtSetInformationObject(IN HANDLE ObjectHandle, IN OBJECT_INFORMATION_CLASS ObjectInformationClass, IN PVOID ObjectInformation, IN ULONG Length)
Definition: oblife.c:1735
#define HANDLE_FLAG_INHERIT
Definition: winbase.h:261
UINT WINAPI SetHandleCount(IN UINT nCount)
Definition: handle.c:208
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define STD_INPUT_HANDLE
Definition: winbase.h:264
#define NtCurrentProcess()
Definition: nt_native.h:1657
_In_ HANDLE Handle
Definition: extypes.h:390
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NtQueryObject(IN HANDLE Handle, IN OBJECT_INFO_CLASS ObjectInformationClass, OUT PVOID ObjectInformation, IN ULONG ObjectInformationLength, OUT PULONG ReturnLength)
#define STD_ERROR_HANDLE
Definition: winbase.h:266
#define OBJ_INHERIT
Definition: winternl.h:225
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
#define IsConsoleHandle(h)
Definition: console.h:14
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
Status
Definition: gdiplustypes.h:24
#define NtCurrentPeb()
Definition: FLS.c:20
unsigned int UINT
Definition: ndis.h:50
#define HANDLE_FLAG_PROTECT_FROM_CLOSE
Definition: winbase.h:262
BOOL WINAPI DECLSPEC_HOTPATCH CloseConsoleHandle(HANDLE hHandle)
Definition: console.c:1142
NTSTATUS NTAPI NtDuplicateObject(IN HANDLE SourceProcessHandle, IN HANDLE SourceHandle, IN HANDLE TargetProcessHandle OPTIONAL, OUT PHANDLE TargetHandle OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG HandleAttributes, IN ULONG Options)
Definition: obhandle.c:3407
#define OUT
Definition: typedefs.h:39
uint32_t * LPDWORD
Definition: typedefs.h:57
unsigned int ULONG
Definition: retypes.h:1
BOOL WINAPI GetHandleInformation(IN HANDLE hObject, OUT LPDWORD lpdwFlags)
Definition: handle.c:40
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
BOOL WINAPI CloseHandle(IN HANDLE hObject)
Definition: handle.c:129
BOOL WINAPI SetHandleInformation(IN HANDLE hObject, IN DWORD dwMask, IN DWORD dwFlags)
Definition: handle.c:78
BOOL WINAPI SetHandleContext(IN HANDLE Handle, IN DWORD Context)
Definition: handle.c:244
BOOL WINAPI GetConsoleHandleInformation(IN HANDLE hHandle, OUT LPDWORD lpdwFlags)
Definition: console.c:465
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149