ReactOS 0.4.15-dev-7704-gc07eb9f
handle.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for handle.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

HANDLE TranslateStdHandle (IN HANDLE hHandle)
 
BOOL WINAPI GetHandleInformation (IN HANDLE hObject, OUT LPDWORD lpdwFlags)
 
BOOL WINAPI SetHandleInformation (IN HANDLE hObject, IN DWORD dwMask, IN DWORD dwFlags)
 
BOOL WINAPI CloseHandle (IN HANDLE hObject)
 
BOOL WINAPI DuplicateHandle (IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
 
UINT WINAPI SetHandleCount (IN UINT nCount)
 
DWORD WINAPI GetHandleContext (IN HANDLE Handle)
 
HANDLE WINAPI CreateSocketHandle (VOID)
 
BOOL WINAPI SetHandleContext (IN HANDLE Handle, IN DWORD Context)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file handle.c.

Function Documentation

◆ CloseHandle()

BOOL WINAPI CloseHandle ( IN HANDLE  hObject)

Definition at line 129 of file handle.c.

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}
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
BOOL WINAPI DECLSPEC_HOTPATCH CloseConsoleHandle(HANDLE hHandle)
Definition: console.c:1142
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
Status
Definition: gdiplustypes.h:25
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define IsConsoleHandle(h)
Definition: console.h:14

◆ CreateSocketHandle()

HANDLE WINAPI CreateSocketHandle ( VOID  )

Definition at line 231 of file handle.c.

232{
233 /* This is Windows behavior, not a ReactOS Stub */
234 DbgPrintEx(0, 0, "Unsupported API - kernel32!CreateSocketHandle() called\n");
236 return FALSE;
237}
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define SetLastError(x)
Definition: compat.h:752
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)

◆ DuplicateHandle()

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 at line 149 of file handle.c.

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}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
HANDLE WINAPI DECLSPEC_HOTPATCH DuplicateConsoleHandle(HANDLE hConsole, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions)
Definition: console.c:424
#define OBJ_INHERIT
Definition: winternl.h:225
static BOOL bInheritHandle
Definition: pipe.c:82
#define NtCurrentProcess()
Definition: nt_native.h:1657
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
DWORD dwOptions
Definition: solitaire.cpp:25
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define NtCurrentThread()

Referenced by _dup2(), ACTION_FinishCustomActions(), appbar_message(), chm_dup(), cmd_ctty(), ATL::CAtlFileMappingBase::CopyFrom(), CorDebugProcess_Create(), CreateLobbyMessageReceptionThread(), CRYPT_FileOpenStore(), CRYPT_RegOpenStore(), custom_client_thread(), dll_entry_point(), DoDuplicateHandle(), DP_CreatePlayer(), DPL_CreateAndSetLobbyHandles(), elf_map_file(), EnumOLEVERB_Clone(), get_droptarget_local_handle(), IDirectPlay4Impl_EnumSessions(), main(), MyDuplicateHandle(), ObjectFromLresult(), PerformRedirection(), read_pipe_test(), SeclCreateProcessWithLogonW(), SHMapHandle(), StartupWindowThread(), test_Console(), test_CreateRemoteThread(), Test_DuplicateHandle(), test_DuplicateHandle(), test_event_security(), test_ExitProcess(), test_file_access(), test_file_disposition_information(), test_file_security(), test_filemap_security(), test_flags_NtQueryDirectoryFile(), test_GetMappedFileName(), test_Handles(), test_handles(), test_mapping(), test_MapViewOfFile(), test_mutex_security(), test_named_pipe_security(), test_process_access(), test_process_security_child(), test_semaphore_security(), test_TerminateProcess(), test_thread_security(), WahOpenCurrentThread(), WSPDuplicateSocket(), WTSQueryUserToken(), and xmlGetGlobalState().

◆ GetHandleContext()

DWORD WINAPI GetHandleContext ( IN HANDLE  Handle)

Definition at line 218 of file handle.c.

219{
220 /* This is Windows behavior, not a ReactOS Stub */
221 DbgPrintEx(0, 0, "Unsupported API - kernel32!GetHandleContext() called\n");
223 return FALSE;
224}

◆ GetHandleInformation()

BOOL WINAPI GetHandleInformation ( IN HANDLE  hObject,
OUT LPDWORD  lpdwFlags 
)

Definition at line 40 of file handle.c.

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}
NTSTATUS NtQueryObject(IN HANDLE Handle, IN OBJECT_INFO_CLASS ObjectInformationClass, OUT PVOID ObjectInformation, IN ULONG ObjectInformationLength, OUT PULONG ReturnLength)
BOOL WINAPI GetConsoleHandleInformation(IN HANDLE hHandle, OUT LPDWORD lpdwFlags)
Definition: console.c:465
unsigned long DWORD
Definition: ntddk_ex.h:95
uint32_t ULONG
Definition: typedefs.h:59
_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 HANDLE_FLAG_PROTECT_FROM_CLOSE
Definition: winbase.h:265
#define HANDLE_FLAG_INHERIT
Definition: winbase.h:264
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170

Referenced by do_spawnT(), test_close(), test_DuplicateHandle(), test_handles(), and WsSockFindIfsSocket().

◆ SetHandleContext()

BOOL WINAPI SetHandleContext ( IN HANDLE  Handle,
IN DWORD  Context 
)

Definition at line 244 of file handle.c.

246{
247 /* This is Windows behavior, not a ReactOS Stub */
248 DbgPrintEx(0, 0, "Unsupported API - kernel32!SetHandleContext() called\n");
250 return FALSE;
251}

◆ SetHandleCount()

UINT WINAPI SetHandleCount ( IN UINT  nCount)

Definition at line 208 of file handle.c.

209{
210 return nCount;
211}

◆ SetHandleInformation()

BOOL WINAPI SetHandleInformation ( IN HANDLE  hObject,
IN DWORD  dwMask,
IN DWORD  dwFlags 
)

Definition at line 78 of file handle.c.

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}
BOOL WINAPI SetConsoleHandleInformation(IN HANDLE hHandle, IN DWORD dwMask, IN DWORD dwFlags)
Definition: console.c:495
NTSTATUS NTAPI NtSetInformationObject(IN HANDLE ObjectHandle, IN OBJECT_INFORMATION_CLASS ObjectInformationClass, IN PVOID ObjectInformation, IN ULONG Length)
Definition: oblife.c:1824
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1176

Referenced by CreateClientProcess(), ExecutePipeline(), fork_helper(), main(), read_reg_output_(), and test_DuplicateHandle().

◆ TranslateStdHandle()

HANDLE TranslateStdHandle ( IN HANDLE  hHandle)

Definition at line 19 of file handle.c.

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}
#define NtCurrentPeb()
Definition: FLS.c:22
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
#define STD_INPUT_HANDLE
Definition: winbase.h:267
#define STD_ERROR_HANDLE
Definition: winbase.h:269

Referenced by CloseHandle(), DuplicateHandle(), FlushFileBuffers(), GetFileType(), GetHandleInformation(), ReadFile(), RegisterWaitForSingleObject(), RegisterWaitForSingleObjectEx(), SetHandleInformation(), SignalObjectAndWait(), WaitForMultipleObjectsEx(), WaitForSingleObjectEx(), and WriteFile().