Author: ion
Date: Thu Aug 4 22:21:39 2011
New Revision: 53069
URL:
http://svn.reactos.org/svn/reactos?rev=53069&view=rev
Log:
[KERNEL32]: Fix the handle APIs. DuplicateHandle was pretty broken w.r.t console handles,
and the Query/Set APIs didn't handle those at all.
Modified:
trunk/reactos/dll/win32/kernel32/client/handle.c
Modified: trunk/reactos/dll/win32/kernel32/client/handle.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/handle.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/handle.c [iso-8859-1] Thu Aug 4 22:21:39
2011
@@ -1,209 +1,206 @@
-/* $Id$
- *
- * COPYRIGHT: See COPYING in the top level directory
- * PROJECT: ReactOS system libraries
- * FILE: lib/kernel32/misc/handle.c
- * PURPOSE: Object functions
- * PROGRAMMER: Ariadne ( ariadne(a)xs4all.nl)
- * UPDATE HISTORY:
- * Created 01/11/98
- */
-
-/* INCLUDES ******************************************************************/
+/*
+ * PROJECT: ReactOS Win32 Base API
+ * LICENSE: See COPYING in the top level directory
+ * FILE: dll/win32/kernel32/client/handle.c
+ * PURPOSE: Object Handle Functions
+ * PROGRAMMERS: Ariadne ( ariadne(a)xs4all.nl)
+ */
+
+/* INCLUDES *******************************************************************/
#include <k32.h>
#define NDEBUG
#include <debug.h>
-/* GLOBALS *******************************************************************/
-
-HANDLE WINAPI
-DuplicateConsoleHandle (HANDLE hConsole,
- DWORD dwDesiredAccess,
- BOOL bInheritHandle,
- DWORD dwOptions);
-
-/* FUNCTIONS *****************************************************************/
-
-HANDLE FASTCALL
-TranslateStdHandle(HANDLE hHandle)
-{
- PRTL_USER_PROCESS_PARAMETERS Ppb = NtCurrentPeb()->ProcessParameters;
-
- switch ((ULONG)hHandle)
- {
- case STD_INPUT_HANDLE: return Ppb->StandardInput;
- case STD_OUTPUT_HANDLE: return Ppb->StandardOutput;
- case STD_ERROR_HANDLE: return Ppb->StandardError;
- }
-
- return hHandle;
-}
-
-/*
- * @implemented
- */
-BOOL WINAPI
-GetHandleInformation (HANDLE hObject,
- LPDWORD lpdwFlags)
-{
- OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
- ULONG BytesWritten;
- NTSTATUS Status;
- DWORD Flags;
-
- hObject = TranslateStdHandle(hObject);
-
- Status = NtQueryObject (hObject,
- ObjectHandleFlagInformation,
- &HandleInfo,
- sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
- &BytesWritten);
- if (NT_SUCCESS(Status))
- {
+/* PRIVATE FUNCTIONS **********************************************************/
+
+HANDLE
+FASTCALL
+TranslateStdHandle(IN HANDLE hHandle)
+{
+ PRTL_USER_PROCESS_PARAMETERS Ppb = NtCurrentPeb()->ProcessParameters;
+
+ switch ((ULONG)hHandle)
+ {
+ case STD_INPUT_HANDLE: return Ppb->StandardInput;
+ case STD_OUTPUT_HANDLE: return Ppb->StandardOutput;
+ case STD_ERROR_HANDLE: return Ppb->StandardError;
+ }
+
+ return hHandle;
+}
+
+/* PUBLIC FUNCTIONS ***********************************************************/
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+GetHandleInformation(IN HANDLE hObject,
+ OUT LPDWORD lpdwFlags)
+{
+ OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
+ ULONG BytesWritten;
+ NTSTATUS Status;
+ DWORD Flags;
+
+ hObject = TranslateStdHandle(hObject);
+
+ if (IsConsoleHandle(hObject))
+ {
+ /* FIXME: GetConsoleHandleInformation required */
+ UNIMPLEMENTED;
+ BaseSetLastNTError(STATUS_NOT_IMPLEMENTED);
+ return FALSE;
+ }
+
+ Status = NtQueryObject(hObject,
+ ObjectHandleFlagInformation,
+ &HandleInfo,
+ sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
+ &BytesWritten);
+ if (!NT_SUCCESS(Status))
+ {
+ BaseSetLastNTError(Status);
+ return FALSE;
+ }
+
Flags = 0;
- if (HandleInfo.Inherit)
- Flags |= HANDLE_FLAG_INHERIT;
- if (HandleInfo.ProtectFromClose)
- Flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE;
-
+ if (HandleInfo.Inherit) Flags |= HANDLE_FLAG_INHERIT;
+ if (HandleInfo.ProtectFromClose) Flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE;
*lpdwFlags = Flags;
-
return TRUE;
- }
- else
- {
- BaseSetLastNTError (Status);
- return FALSE;
- }
-}
-
-
-/*
- * @implemented
- */
-BOOL WINAPI
-SetHandleInformation (HANDLE hObject,
- DWORD dwMask,
- DWORD dwFlags)
-{
- OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
- ULONG BytesWritten;
- NTSTATUS Status;
-
- hObject = TranslateStdHandle(hObject);
-
- Status = NtQueryObject (hObject,
- ObjectHandleFlagInformation,
- &HandleInfo,
- sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
- &BytesWritten);
- if (NT_SUCCESS(Status))
- {
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+SetHandleInformation(IN HANDLE hObject,
+ IN DWORD dwMask,
+ IN DWORD dwFlags)
+{
+ OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
+ ULONG BytesWritten;
+ NTSTATUS Status;
+
+ hObject = TranslateStdHandle(hObject);
+
+ if (IsConsoleHandle(hObject))
+ {
+ /* FIXME: SetConsoleHandleInformation required */
+ UNIMPLEMENTED;
+ BaseSetLastNTError(STATUS_NOT_IMPLEMENTED);
+ return FALSE;
+ }
+
+ Status = NtQueryObject(hObject,
+ ObjectHandleFlagInformation,
+ &HandleInfo,
+ sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
+ &BytesWritten);
+ if (!NT_SUCCESS(Status))
+ {
+ BaseSetLastNTError(Status);
+ return FALSE;
+ }
+
if (dwMask & HANDLE_FLAG_INHERIT)
- HandleInfo.Inherit = (dwFlags & HANDLE_FLAG_INHERIT) != 0;
+ {
+ HandleInfo.Inherit = (dwFlags & HANDLE_FLAG_INHERIT) != 0;
+ }
+
if (dwMask & HANDLE_FLAG_PROTECT_FROM_CLOSE)
- HandleInfo.ProtectFromClose = (dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE) != 0;
-
- Status = NtSetInformationObject (hObject,
- ObjectHandleFlagInformation,
- &HandleInfo,
- sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION));
- if(!NT_SUCCESS(Status))
- {
- BaseSetLastNTError (Status);
- return FALSE;
- }
-
- return TRUE;
- }
- else
- {
- BaseSetLastNTError (Status);
- return FALSE;
- }
-}
-
-
-/*
- * @implemented
- */
-BOOL WINAPI CloseHandle(HANDLE hObject)
-/*
- * FUNCTION: Closes an open object handle
- * PARAMETERS:
- * hObject = Identifies an open object handle
- * RETURNS: If the function succeeds, the return value is nonzero
- * If the function fails, the return value is zero
- */
-{
- NTSTATUS Status;
-
- hObject = TranslateStdHandle(hObject);
-
- if (IsConsoleHandle(hObject))
- {
- return(CloseConsoleHandle(hObject));
- }
-
- Status = NtClose(hObject);
- if (!NT_SUCCESS(Status))
- {
- BaseSetLastNTError (Status);
- return FALSE;
- }
-
- return TRUE;
-}
-
-
-/*
- * @implemented
- */
-BOOL WINAPI DuplicateHandle(HANDLE hSourceProcessHandle,
- HANDLE hSourceHandle,
- HANDLE hTargetProcessHandle,
- LPHANDLE lpTargetHandle,
- DWORD dwDesiredAccess,
- BOOL bInheritHandle,
- DWORD dwOptions)
-{
- DWORD SourceProcessId, TargetProcessId;
- NTSTATUS Status;
-
- hSourceHandle = TranslateStdHandle(hSourceHandle);
-
- if (IsConsoleHandle(hSourceHandle))
- {
- SourceProcessId = GetProcessId(hSourceProcessHandle);
- TargetProcessId = GetProcessId(hTargetProcessHandle);
- if (!SourceProcessId || !TargetProcessId ||
- SourceProcessId != TargetProcessId ||
- SourceProcessId != GetCurrentProcessId())
- {
- SetLastError(ERROR_INVALID_PARAMETER);
- return FALSE;
- }
-
- *lpTargetHandle = DuplicateConsoleHandle(hSourceHandle, dwDesiredAccess,
bInheritHandle, dwOptions);
- return *lpTargetHandle != INVALID_HANDLE_VALUE;
- }
-
- Status = NtDuplicateObject(hSourceProcessHandle,
- hSourceHandle,
- hTargetProcessHandle,
- lpTargetHandle,
- dwDesiredAccess,
- bInheritHandle ? OBJ_INHERIT : 0,
- dwOptions);
- if (!NT_SUCCESS(Status))
- {
- BaseSetLastNTError (Status);
- return FALSE;
- }
-
- return TRUE;
+ {
+ HandleInfo.ProtectFromClose = (dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE) !=
0;
+ }
+
+ Status = NtSetInformationObject(hObject,
+ ObjectHandleFlagInformation,
+ &HandleInfo,
+ sizeof(HandleInfo));
+ if (NT_SUCCESS(Status)) return TRUE;
+
+ BaseSetLastNTError(Status);
+ return FALSE;
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+CloseHandle(IN HANDLE hObject)
+{
+ NTSTATUS Status;
+
+ hObject = TranslateStdHandle(hObject);
+
+ if (IsConsoleHandle(hObject)) return CloseConsoleHandle(hObject);
+
+ Status = NtClose(hObject);
+ if (NT_SUCCESS(Status)) return TRUE;
+
+ BaseSetLastNTError(Status);
+ return FALSE;
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+DuplicateHandle(IN HANDLE hSourceProcessHandle,
+ IN HANDLE hSourceHandle,
+ IN HANDLE hTargetProcessHandle,
+ OUT LPHANDLE lpTargetHandle,
+ IN DWORD dwDesiredAccess,
+ IN BOOL bInheritHandle,
+ IN DWORD dwOptions)
+{
+ NTSTATUS Status;
+ HANDLE hTargetHandle;
+
+ hSourceHandle = TranslateStdHandle(hSourceHandle);
+
+ if ((IsConsoleHandle(hSourceHandle)) &&
+ ((hSourceHandle != NtCurrentProcess()) &&
+ (hSourceHandle != NtCurrentThread())))
+ {
+ if ((hSourceProcessHandle != NtCurrentProcess()) &&
+ (hTargetProcessHandle != NtCurrentProcess()))
+ {
+ BaseSetLastNTError(STATUS_INVALID_PARAMETER);
+ return FALSE;
+ }
+
+ hTargetHandle = DuplicateConsoleHandle(hSourceHandle,
+ dwDesiredAccess,
+ bInheritHandle,
+ dwOptions);
+ if (hTargetHandle != INVALID_HANDLE_VALUE)
+ {
+ if (lpTargetHandle) *lpTargetHandle = hTargetHandle;
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+
+ Status = NtDuplicateObject(hSourceProcessHandle,
+ hSourceHandle,
+ hTargetProcessHandle,
+ lpTargetHandle,
+ dwDesiredAccess,
+ bInheritHandle ? OBJ_INHERIT : 0,
+ dwOptions);
+ if (NT_SUCCESS(Status)) return Status;
+
+ BaseSetLastNTError(Status);
+ return FALSE;
}
/*
@@ -211,9 +208,9 @@
*/
UINT
WINAPI
-SetHandleCount(UINT nCount)
-{
- return nCount;
+SetHandleCount(IN UINT nCount)
+{
+ return nCount;
}
/*
@@ -240,7 +237,6 @@
DbgPrintEx(0, 0, "Unsupported API - kernel32!CreateSocketHandle()
called\n");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
-
}
/*