ReactOS  0.4.13-dev-259-g5ca9c9c
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 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define IsConsoleHandle(h)
Definition: console.h:14
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
Status
Definition: gdiplustypes.h:24
BOOL WINAPI DECLSPEC_HOTPATCH CloseConsoleHandle(HANDLE hHandle)
Definition: console.c:1142

Referenced by DosCloseHandle(), ExitThreadApc(), WahCloseSocketHandle(), and WahCreateSocketHandle().

◆ 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 }
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
#define SetLastError(x)
Definition: compat.h:409
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92

◆ 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 }
DWORD dwOptions
Definition: solitaire.cpp:23
#define TRUE
Definition: types.h:120
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
HANDLE WINAPI DECLSPEC_HOTPATCH DuplicateConsoleHandle(HANDLE hConsole, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions)
Definition: console.c:424
#define NtCurrentThread()
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
static BOOL bInheritHandle
Definition: pipe.c:82
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_INHERIT
Definition: winternl.h:225
#define IsConsoleHandle(h)
Definition: console.h:14
Status
Definition: gdiplustypes.h:24
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

Referenced by _dup2(), ACTION_FinishCustomActions(), chm_dup(), cmd_ctty(), ATL::CAtlFileMappingBase::CopyFrom(), CorDebugProcess_Create(), CreateLobbyMessageReceptionThread(), CRYPT_FileOpenStore(), CRYPT_RegOpenStore(), dll_entry_point(), DP_CreatePlayer(), DPL_CreateAndSetLobbyHandles(), EnumOLEVERB_Clone(), get_droptarget_local_handle(), IDirectPlay4Impl_EnumSessions(), main(), ObjectFromLresult(), PerformRedirection(), read_pipe_test(), 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 }
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
#define SetLastError(x)
Definition: compat.h:409
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92

◆ GetHandleInformation()

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

Definition at line 40 of file handle.c.

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 }
_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
LONG NTSTATUS
Definition: precomp.h:26
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define HANDLE_FLAG_INHERIT
Definition: winbase.h:261
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#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)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define IsConsoleHandle(h)
Definition: console.h:14
Status
Definition: gdiplustypes.h:24
#define HANDLE_FLAG_PROTECT_FROM_CLOSE
Definition: winbase.h:262
unsigned int ULONG
Definition: retypes.h:1
BOOL WINAPI GetConsoleHandleInformation(IN HANDLE hHandle, OUT LPDWORD lpdwFlags)
Definition: console.c:465

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 }
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
#define SetLastError(x)
Definition: compat.h:409
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92

◆ 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,
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 }
_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
LONG NTSTATUS
Definition: precomp.h:26
BOOL WINAPI SetConsoleHandleInformation(IN HANDLE hHandle, IN DWORD dwMask, IN DWORD dwFlags)
Definition: console.c:495
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
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
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#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 IsConsoleHandle(h)
Definition: console.h:14
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
Status
Definition: gdiplustypes.h:24
#define HANDLE_FLAG_PROTECT_FROM_CLOSE
Definition: winbase.h:262
unsigned int ULONG
Definition: retypes.h:1

Referenced by CreateClientProcess(), ExecutePipeline(), fork_helper(), main(), 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 }
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define STD_INPUT_HANDLE
Definition: winbase.h:264
#define STD_ERROR_HANDLE
Definition: winbase.h:266
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
#define NtCurrentPeb()
Definition: FLS.c:19

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