fixed GetHandleInformation(), SetHandleInformation() and minor fixes to CloseHandle() and DuplicateHandle()

svn path=/trunk/; revision=11165
This commit is contained in:
Thomas Bluemel 2004-10-03 10:05:56 +00:00
parent d8ea61531d
commit c0f9c7505c

View file

@ -1,4 +1,4 @@
/* $Id: handle.c,v 1.16 2004/01/23 21:16:03 ekohl Exp $ /* $Id: handle.c,v 1.17 2004/10/03 10:05:56 weiden Exp $
* *
* COPYRIGHT: See COPYING in the top level directory * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries * PROJECT: ReactOS system libraries
@ -36,28 +36,48 @@ BOOL WINAPI
GetHandleInformation (HANDLE hObject, GetHandleInformation (HANDLE hObject,
LPDWORD lpdwFlags) LPDWORD lpdwFlags)
{ {
PRTL_USER_PROCESS_PARAMETERS Ppb;
OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo; OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
ULONG BytesWritten; ULONG BytesWritten;
NTSTATUS Status; NTSTATUS Status;
DWORD Flags;
Ppb = NtCurrentPeb()->ProcessParameters;
switch ((ULONG)hObject)
{
case STD_INPUT_HANDLE:
hObject = Ppb->hStdInput;
break;
case STD_OUTPUT_HANDLE:
hObject = Ppb->hStdOutput;
break;
case STD_ERROR_HANDLE:
hObject = Ppb->hStdError;
break;
}
Status = NtQueryObject (hObject, Status = NtQueryObject (hObject,
ObjectHandleInformation, ObjectHandleInformation,
&HandleInfo, &HandleInfo,
sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION), sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
&BytesWritten); &BytesWritten);
if (!NT_SUCCESS(Status)) if (NT_SUCCESS(Status))
{
Flags = 0;
if (HandleInfo.Inherit)
Flags |= HANDLE_FLAG_INHERIT;
if (HandleInfo.ProtectFromClose)
Flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE;
*lpdwFlags = Flags;
return TRUE;
}
else
{ {
SetLastErrorByStatus (Status); SetLastErrorByStatus (Status);
return FALSE; return FALSE;
} }
if (HandleInfo.Inherit)
*lpdwFlags &= HANDLE_FLAG_INHERIT;
if (HandleInfo.ProtectFromClose)
*lpdwFlags &= HANDLE_FLAG_PROTECT_FROM_CLOSE;
return TRUE;
} }
@ -69,31 +89,34 @@ SetHandleInformation (HANDLE hObject,
DWORD dwMask, DWORD dwMask,
DWORD dwFlags) DWORD dwFlags)
{ {
PRTL_USER_PROCESS_PARAMETERS Ppb;
OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo; OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
ULONG BytesWritten; ULONG BytesWritten;
NTSTATUS Status; NTSTATUS Status;
Ppb = NtCurrentPeb()->ProcessParameters;
switch ((ULONG)hObject)
{
case STD_INPUT_HANDLE:
hObject = Ppb->hStdInput;
break;
case STD_OUTPUT_HANDLE:
hObject = Ppb->hStdOutput;
break;
case STD_ERROR_HANDLE:
hObject = Ppb->hStdError;
break;
}
Status = NtQueryObject (hObject, Status = NtQueryObject (hObject,
ObjectHandleInformation, ObjectHandleInformation,
&HandleInfo, &HandleInfo,
sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION), sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION),
&BytesWritten); &BytesWritten);
if (!NT_SUCCESS(Status)) if (NT_SUCCESS(Status))
{ {
SetLastErrorByStatus (Status); HandleInfo.Inherit = ((dwFlags & HANDLE_FLAG_INHERIT) ? TRUE : FALSE);
return FALSE; HandleInfo.ProtectFromClose = ((dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE) ? TRUE : FALSE);
}
if (dwMask & HANDLE_FLAG_INHERIT)
{
HandleInfo.Inherit = dwFlags & HANDLE_FLAG_INHERIT;
}
if (dwMask & HANDLE_FLAG_PROTECT_FROM_CLOSE)
{
HandleInfo.ProtectFromClose = dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE;
}
Status = NtSetInformationObject (hObject, Status = NtSetInformationObject (hObject,
ObjectHandleInformation, ObjectHandleInformation,
&HandleInfo, &HandleInfo,
@ -106,6 +129,12 @@ SetHandleInformation (HANDLE hObject,
return TRUE; return TRUE;
} }
else
{
SetLastErrorByStatus (Status);
return FALSE;
}
}
/* /*
@ -120,17 +149,32 @@ BOOL STDCALL CloseHandle(HANDLE hObject)
* If the function fails, the return value is zero * If the function fails, the return value is zero
*/ */
{ {
NTSTATUS errCode; PRTL_USER_PROCESS_PARAMETERS Ppb;
NTSTATUS Status;
Ppb = NtCurrentPeb()->ProcessParameters;
switch ((ULONG)hObject)
{
case STD_INPUT_HANDLE:
hObject = Ppb->hStdInput;
break;
case STD_OUTPUT_HANDLE:
hObject = Ppb->hStdOutput;
break;
case STD_ERROR_HANDLE:
hObject = Ppb->hStdError;
break;
}
if (IsConsoleHandle(hObject)) if (IsConsoleHandle(hObject))
{ {
return(CloseConsoleHandle(hObject)); return(CloseConsoleHandle(hObject));
} }
errCode = NtClose(hObject); Status = NtClose(hObject);
if (!NT_SUCCESS(errCode)) if (!NT_SUCCESS(Status))
{ {
SetLastErrorByStatus (errCode); SetLastErrorByStatus (Status);
return FALSE; return FALSE;
} }
@ -149,8 +193,24 @@ BOOL STDCALL DuplicateHandle(HANDLE hSourceProcessHandle,
BOOL bInheritHandle, BOOL bInheritHandle,
DWORD dwOptions) DWORD dwOptions)
{ {
NTSTATUS errCode; PRTL_USER_PROCESS_PARAMETERS Ppb;
DWORD SourceProcessId, TargetProcessId; DWORD SourceProcessId, TargetProcessId;
NTSTATUS Status;
Ppb = NtCurrentPeb()->ProcessParameters;
switch ((ULONG)hSourceHandle)
{
case STD_INPUT_HANDLE:
hSourceHandle = Ppb->hStdInput;
break;
case STD_OUTPUT_HANDLE:
hSourceHandle = Ppb->hStdOutput;
break;
case STD_ERROR_HANDLE:
hSourceHandle = Ppb->hStdError;
break;
}
if (IsConsoleHandle(hSourceHandle)) if (IsConsoleHandle(hSourceHandle))
{ {
if (FALSE == InternalGetProcessId(hSourceProcessHandle, &SourceProcessId) || if (FALSE == InternalGetProcessId(hSourceProcessHandle, &SourceProcessId) ||
@ -166,16 +226,16 @@ BOOL STDCALL DuplicateHandle(HANDLE hSourceProcessHandle,
return *lpTargetHandle != INVALID_HANDLE_VALUE ? TRUE : FALSE; return *lpTargetHandle != INVALID_HANDLE_VALUE ? TRUE : FALSE;
} }
errCode = NtDuplicateObject(hSourceProcessHandle, Status = NtDuplicateObject(hSourceProcessHandle,
hSourceHandle, hSourceHandle,
hTargetProcessHandle, hTargetProcessHandle,
lpTargetHandle, lpTargetHandle,
dwDesiredAccess, dwDesiredAccess,
(BOOLEAN)bInheritHandle, (BOOLEAN)bInheritHandle,
dwOptions); dwOptions);
if (!NT_SUCCESS(errCode)) if (!NT_SUCCESS(Status))
{ {
SetLastErrorByStatus (errCode); SetLastErrorByStatus (Status);
return FALSE; return FALSE;
} }