ReactOS 0.4.15-dev-8096-ga0eec98
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
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 */
38BOOL
41 OUT LPDWORD lpdwFlags)
42{
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,
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 */
76BOOL
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,
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
110 {
112 }
113
115 ObjectHandleFlagInformation,
116 &HandleInfo,
117 sizeof(HandleInfo));
118 if (NT_SUCCESS(Status)) return TRUE;
119
121 return FALSE;
122}
123
124/*
125 * @implemented
126 */
127BOOL
128WINAPI
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 */
147BOOL
148WINAPI
149DuplicateHandle(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 */
206UINT
207WINAPI
209{
210 return nCount;
211}
212
213/*
214 * @implemented
215 */
216DWORD
217WINAPI
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 */
229HANDLE
230WINAPI
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 */
242BOOL
243WINAPI
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 */
NTSTATUS NtQueryObject(IN HANDLE Handle, IN OBJECT_INFO_CLASS ObjectInformationClass, OUT PVOID ObjectInformation, IN ULONG ObjectInformationLength, OUT PULONG ReturnLength)
#define NtCurrentPeb()
Definition: FLS.c:22
LONG NTSTATUS
Definition: precomp.h:26
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define CloseHandle
Definition: compat.h:739
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define SetLastError(x)
Definition: compat.h:752
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
BOOL WINAPI DECLSPEC_HOTPATCH CloseConsoleHandle(HANDLE hHandle)
Definition: console.c:1142
HANDLE WINAPI DECLSPEC_HOTPATCH DuplicateConsoleHandle(HANDLE hConsole, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions)
Definition: console.c:424
BOOL WINAPI GetConsoleHandleInformation(IN HANDLE hHandle, OUT LPDWORD lpdwFlags)
Definition: console.c:465
BOOL WINAPI SetConsoleHandleInformation(IN HANDLE hHandle, IN DWORD dwMask, IN DWORD dwFlags)
Definition: console.c:495
BOOL WINAPI SetHandleInformation(IN HANDLE hObject, IN DWORD dwMask, IN DWORD dwFlags)
Definition: handle.c:78
BOOL WINAPI GetHandleInformation(IN HANDLE hObject, OUT LPDWORD lpdwFlags)
Definition: handle.c:40
BOOL WINAPI SetHandleContext(IN HANDLE Handle, IN DWORD Context)
Definition: handle.c:244
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
UINT WINAPI SetHandleCount(IN UINT nCount)
Definition: handle.c:208
HANDLE WINAPI CreateSocketHandle(VOID)
Definition: handle.c:231
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
DWORD WINAPI GetHandleContext(IN HANDLE Handle)
Definition: handle.c:218
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
ULONG Handle
Definition: gdb_input.c:15
Status
Definition: gdiplustypes.h:25
#define OBJ_INHERIT
Definition: winternl.h:225
static BOOL bInheritHandle
Definition: pipe.c:82
unsigned int UINT
Definition: ndis.h:50
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
#define NtCurrentProcess()
Definition: nt_native.h:1657
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
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:3410
NTSTATUS NTAPI NtSetInformationObject(IN HANDLE ObjectHandle, IN OBJECT_INFORMATION_CLASS ObjectInformationClass, IN PVOID ObjectInformation, IN ULONG Length)
Definition: oblife.c:1824
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define IsConsoleHandle(h)
Definition: console.h:14
DWORD dwOptions
Definition: solitaire.cpp:25
uint32_t * LPDWORD
Definition: typedefs.h:59
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesWritten
Definition: wdfiotarget.h:960
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
#define STD_INPUT_HANDLE
Definition: winbase.h:267
#define HANDLE_FLAG_PROTECT_FROM_CLOSE
Definition: winbase.h:265
#define HANDLE_FLAG_INHERIT
Definition: winbase.h:264
#define STD_ERROR_HANDLE
Definition: winbase.h:269
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1176
#define WINAPI
Definition: msvc.h:6
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define NtCurrentThread()