PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel32/misc/handle.c

http://nativecmd.googlecode.com/
C | 218 lines | 144 code | 36 blank | 38 comment | 19 complexity | c38a328b6326174c94ad7c77f2a7d80d MD5 | raw file
Possible License(s): GPL-2.0
  1. /* $Id: handle.c 37763 2008-11-30 11:42:05Z sginsberg $
  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. /* INCLUDES ******************************************************************/
  12. #include <k32.h>
  13. //#define NDEBUG
  14. #include <debug.h>
  15. /* GLOBALS *******************************************************************/
  16. HANDLE WINAPI
  17. DuplicateConsoleHandle (HANDLE hConsole,
  18. DWORD dwDesiredAccess,
  19. BOOL bInheritHandle,
  20. DWORD dwOptions);
  21. /* FUNCTIONS *****************************************************************/
  22. HANDLE FASTCALL
  23. TranslateStdHandle(HANDLE hHandle)
  24. {
  25. PRTL_USER_PROCESS_PARAMETERS Ppb = NtCurrentPeb()->ProcessParameters;
  26. switch ((ULONG)hHandle)
  27. {
  28. case STD_INPUT_HANDLE: return Ppb->StandardInput;
  29. case STD_OUTPUT_HANDLE: return Ppb->StandardOutput;
  30. case STD_ERROR_HANDLE: return Ppb->StandardError;
  31. }
  32. return hHandle;
  33. }
  34. /*
  35. * @implemented
  36. */
  37. BOOL WINAPI
  38. GetHandleInformation (HANDLE hObject,
  39. LPDWORD lpdwFlags)
  40. {
  41. OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
  42. ULONG BytesWritten;
  43. NTSTATUS Status;
  44. DWORD Flags;
  45. // hObject = TranslateStdHandle(hObject);
  46. Status = NtQueryObject (hObject,
  47. ObjectHandleFlagInformation,
  48. &HandleInfo,
  49. sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
  50. &BytesWritten);
  51. if (NT_SUCCESS(Status))
  52. {
  53. Flags = 0;
  54. if (HandleInfo.Inherit)
  55. Flags |= HANDLE_FLAG_INHERIT;
  56. if (HandleInfo.ProtectFromClose)
  57. Flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE;
  58. *lpdwFlags = Flags;
  59. return TRUE;
  60. }
  61. else
  62. {
  63. SetLastErrorByStatus (Status);
  64. return FALSE;
  65. }
  66. }
  67. /*
  68. * @implemented
  69. */
  70. BOOL WINAPI
  71. SetHandleInformation (HANDLE hObject,
  72. DWORD dwMask,
  73. DWORD dwFlags)
  74. {
  75. OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
  76. ULONG BytesWritten;
  77. NTSTATUS Status;
  78. hObject = TranslateStdHandle(hObject);
  79. Status = NtQueryObject (hObject,
  80. ObjectHandleFlagInformation,
  81. &HandleInfo,
  82. sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
  83. &BytesWritten);
  84. if (NT_SUCCESS(Status))
  85. {
  86. if (dwMask & HANDLE_FLAG_INHERIT)
  87. HandleInfo.Inherit = (dwFlags & HANDLE_FLAG_INHERIT) != 0;
  88. if (dwMask & HANDLE_FLAG_PROTECT_FROM_CLOSE)
  89. HandleInfo.ProtectFromClose = (dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE) != 0;
  90. Status = NtSetInformationObject (hObject,
  91. ObjectHandleFlagInformation,
  92. &HandleInfo,
  93. sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION));
  94. if(!NT_SUCCESS(Status))
  95. {
  96. SetLastErrorByStatus (Status);
  97. return FALSE;
  98. }
  99. return TRUE;
  100. }
  101. else
  102. {
  103. SetLastErrorByStatus (Status);
  104. return FALSE;
  105. }
  106. }
  107. /*
  108. * @implemented
  109. */
  110. BOOL WINAPI CloseHandle(HANDLE hObject)
  111. /*
  112. * FUNCTION: Closes an open object handle
  113. * PARAMETERS:
  114. * hObject = Identifies an open object handle
  115. * RETURNS: If the function succeeds, the return value is nonzero
  116. * If the function fails, the return value is zero
  117. */
  118. {
  119. NTSTATUS Status;
  120. hObject = TranslateStdHandle(hObject);
  121. if (IsConsoleHandle(hObject))
  122. {
  123. return(CloseConsoleHandle(hObject));
  124. }
  125. Status = NtClose(hObject);
  126. if (!NT_SUCCESS(Status))
  127. {
  128. SetLastErrorByStatus (Status);
  129. return FALSE;
  130. }
  131. return TRUE;
  132. }
  133. /*
  134. * @implemented
  135. */
  136. BOOL WINAPI DuplicateHandle(HANDLE hSourceProcessHandle,
  137. HANDLE hSourceHandle,
  138. HANDLE hTargetProcessHandle,
  139. LPHANDLE lpTargetHandle,
  140. DWORD dwDesiredAccess,
  141. BOOL bInheritHandle,
  142. DWORD dwOptions)
  143. {
  144. DWORD SourceProcessId, TargetProcessId;
  145. NTSTATUS Status;
  146. hSourceHandle = TranslateStdHandle(hSourceHandle);
  147. if (IsConsoleHandle(hSourceHandle))
  148. {
  149. SourceProcessId = GetProcessId(hSourceProcessHandle);
  150. TargetProcessId = GetProcessId(hTargetProcessHandle);
  151. if (!SourceProcessId || !TargetProcessId ||
  152. SourceProcessId != TargetProcessId ||
  153. SourceProcessId != GetCurrentProcessId())
  154. {
  155. SetLastError(ERROR_INVALID_PARAMETER);
  156. return FALSE;
  157. }
  158. *lpTargetHandle = DuplicateConsoleHandle(hSourceHandle, dwDesiredAccess, bInheritHandle, dwOptions);
  159. return *lpTargetHandle != INVALID_HANDLE_VALUE;
  160. }
  161. Status = NtDuplicateObject(hSourceProcessHandle,
  162. hSourceHandle,
  163. hTargetProcessHandle,
  164. lpTargetHandle,
  165. dwDesiredAccess,
  166. bInheritHandle ? OBJ_INHERIT : 0,
  167. dwOptions);
  168. if (!NT_SUCCESS(Status))
  169. {
  170. SetLastErrorByStatus (Status);
  171. return FALSE;
  172. }
  173. return TRUE;
  174. }
  175. /*
  176. * @implemented
  177. */
  178. UINT WINAPI SetHandleCount(UINT nCount)
  179. {
  180. return(nCount);
  181. }
  182. /* EOF */