fixed calls to NtDuplicateObject
[reactos.git] / reactos / lib / kernel32 / misc / handle.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/handle.c
6 * PURPOSE: Object functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 /* INCLUDES ******************************************************************/
13
14 #include <k32.h>
15
16 #define NDEBUG
17 #include "../include/debug.h"
18
19 /* GLOBALS *******************************************************************/
20
21 HANDLE STDCALL
22 DuplicateConsoleHandle (HANDLE hConsole,
23 DWORD dwDesiredAccess,
24 BOOL bInheritHandle,
25 DWORD dwOptions);
26
27 /* FUNCTIONS *****************************************************************/
28
29 /*
30 * @implemented
31 */
32 BOOL WINAPI
33 GetHandleInformation (HANDLE hObject,
34 LPDWORD lpdwFlags)
35 {
36 PRTL_USER_PROCESS_PARAMETERS Ppb;
37 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
38 ULONG BytesWritten;
39 NTSTATUS Status;
40 DWORD Flags;
41
42 Ppb = NtCurrentPeb()->ProcessParameters;
43 switch ((ULONG)hObject)
44 {
45 case STD_INPUT_HANDLE:
46 hObject = Ppb->StandardInput;
47 break;
48 case STD_OUTPUT_HANDLE:
49 hObject = Ppb->StandardOutput;
50 break;
51 case STD_ERROR_HANDLE:
52 hObject = Ppb->StandardError;
53 break;
54 }
55
56 Status = NtQueryObject (hObject,
57 ObjectHandleInformation,
58 &HandleInfo,
59 sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
60 &BytesWritten);
61 if (NT_SUCCESS(Status))
62 {
63 Flags = 0;
64 if (HandleInfo.Inherit)
65 Flags |= HANDLE_FLAG_INHERIT;
66 if (HandleInfo.ProtectFromClose)
67 Flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE;
68
69 *lpdwFlags = Flags;
70
71 return TRUE;
72 }
73 else
74 {
75 SetLastErrorByStatus (Status);
76 return FALSE;
77 }
78 }
79
80
81 /*
82 * @implemented
83 */
84 BOOL STDCALL
85 SetHandleInformation (HANDLE hObject,
86 DWORD dwMask,
87 DWORD dwFlags)
88 {
89 PRTL_USER_PROCESS_PARAMETERS Ppb;
90 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
91 ULONG BytesWritten;
92 NTSTATUS Status;
93
94 Ppb = NtCurrentPeb()->ProcessParameters;
95 switch ((ULONG)hObject)
96 {
97 case STD_INPUT_HANDLE:
98 hObject = Ppb->StandardInput;
99 break;
100 case STD_OUTPUT_HANDLE:
101 hObject = Ppb->StandardOutput;
102 break;
103 case STD_ERROR_HANDLE:
104 hObject = Ppb->StandardError;
105 break;
106 }
107
108 Status = NtQueryObject (hObject,
109 ObjectHandleInformation,
110 &HandleInfo,
111 sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
112 &BytesWritten);
113 if (NT_SUCCESS(Status))
114 {
115 if (dwMask & HANDLE_FLAG_INHERIT)
116 HandleInfo.Inherit = (dwFlags & HANDLE_FLAG_INHERIT) != 0;
117 if (dwMask & HANDLE_FLAG_PROTECT_FROM_CLOSE)
118 HandleInfo.ProtectFromClose = (dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE) != 0;
119
120 Status = NtSetInformationObject (hObject,
121 ObjectHandleInformation,
122 &HandleInfo,
123 sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION));
124 if(!NT_SUCCESS(Status))
125 {
126 SetLastErrorByStatus (Status);
127 return FALSE;
128 }
129
130 return TRUE;
131 }
132 else
133 {
134 SetLastErrorByStatus (Status);
135 return FALSE;
136 }
137 }
138
139
140 /*
141 * @implemented
142 */
143 BOOL STDCALL CloseHandle(HANDLE hObject)
144 /*
145 * FUNCTION: Closes an open object handle
146 * PARAMETERS:
147 * hObject = Identifies an open object handle
148 * RETURNS: If the function succeeds, the return value is nonzero
149 * If the function fails, the return value is zero
150 */
151 {
152 PRTL_USER_PROCESS_PARAMETERS Ppb;
153 NTSTATUS Status;
154
155 Ppb = NtCurrentPeb()->ProcessParameters;
156 switch ((ULONG)hObject)
157 {
158 case STD_INPUT_HANDLE:
159 hObject = Ppb->StandardInput;
160 break;
161 case STD_OUTPUT_HANDLE:
162 hObject = Ppb->StandardOutput;
163 break;
164 case STD_ERROR_HANDLE:
165 hObject = Ppb->StandardError;
166 break;
167 }
168
169 if (IsConsoleHandle(hObject))
170 {
171 return(CloseConsoleHandle(hObject));
172 }
173
174 Status = NtClose(hObject);
175 if (!NT_SUCCESS(Status))
176 {
177 SetLastErrorByStatus (Status);
178 return FALSE;
179 }
180
181 return TRUE;
182 }
183
184
185 /*
186 * @implemented
187 */
188 BOOL STDCALL DuplicateHandle(HANDLE hSourceProcessHandle,
189 HANDLE hSourceHandle,
190 HANDLE hTargetProcessHandle,
191 LPHANDLE lpTargetHandle,
192 DWORD dwDesiredAccess,
193 BOOL bInheritHandle,
194 DWORD dwOptions)
195 {
196 PRTL_USER_PROCESS_PARAMETERS Ppb;
197 DWORD SourceProcessId, TargetProcessId;
198 NTSTATUS Status;
199
200 Ppb = NtCurrentPeb()->ProcessParameters;
201 switch ((ULONG)hSourceHandle)
202 {
203 case STD_INPUT_HANDLE:
204 hSourceHandle = Ppb->StandardInput;
205 break;
206 case STD_OUTPUT_HANDLE:
207 hSourceHandle = Ppb->StandardOutput;
208 break;
209 case STD_ERROR_HANDLE:
210 hSourceHandle = Ppb->StandardError;
211 break;
212 }
213
214 if (IsConsoleHandle(hSourceHandle))
215 {
216 SourceProcessId = GetProcessId(hSourceProcessHandle);
217 TargetProcessId = GetProcessId(hTargetProcessHandle);
218 if (!SourceProcessId || !TargetProcessId ||
219 SourceProcessId != TargetProcessId ||
220 SourceProcessId != GetCurrentProcessId())
221 {
222 SetLastError(ERROR_INVALID_PARAMETER);
223 return FALSE;
224 }
225
226 *lpTargetHandle = DuplicateConsoleHandle(hSourceHandle, dwDesiredAccess, bInheritHandle, dwOptions);
227 return *lpTargetHandle != INVALID_HANDLE_VALUE;
228 }
229
230 Status = NtDuplicateObject(hSourceProcessHandle,
231 hSourceHandle,
232 hTargetProcessHandle,
233 lpTargetHandle,
234 dwDesiredAccess,
235 bInheritHandle ? OBJ_INHERIT : 0,
236 dwOptions);
237 if (!NT_SUCCESS(Status))
238 {
239 SetLastErrorByStatus (Status);
240 return FALSE;
241 }
242
243 return TRUE;
244 }
245
246
247 /*
248 * @implemented
249 */
250 UINT STDCALL SetHandleCount(UINT nCount)
251 {
252 return(nCount);
253 }
254
255 /* EOF */