From 9d74a70ad5c41ffc83bc3ca91431ad1fbe7bbb60 Mon Sep 17 00:00:00 2001 From: Peter Oberndorfer Date: Wed, 22 Sep 2010 19:54:38 +0200 Subject: [PATCH] kernel32: Fix last error value of GetStdHandle/SetStdHandle when called with wrong std handle. --- dlls/kernel32/environ.c | 4 ++-- dlls/kernel32/tests/console.c | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/dlls/kernel32/environ.c b/dlls/kernel32/environ.c index 39c14700710..e6634a26729 100644 --- a/dlls/kernel32/environ.c +++ b/dlls/kernel32/environ.c @@ -396,7 +396,7 @@ HANDLE WINAPI GetStdHandle( DWORD std_handle ) case STD_OUTPUT_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdOutput; case STD_ERROR_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdError; } - SetLastError( ERROR_INVALID_PARAMETER ); + SetLastError( ERROR_INVALID_HANDLE ); return INVALID_HANDLE_VALUE; } @@ -412,7 +412,7 @@ BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle ) case STD_OUTPUT_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdOutput = handle; return TRUE; case STD_ERROR_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdError = handle; return TRUE; } - SetLastError( ERROR_INVALID_PARAMETER ); + SetLastError( ERROR_INVALID_HANDLE ); return FALSE; } diff --git a/dlls/kernel32/tests/console.c b/dlls/kernel32/tests/console.c index 1bc0d34146e..072bd01f08f 100644 --- a/dlls/kernel32/tests/console.c +++ b/dlls/kernel32/tests/console.c @@ -1134,6 +1134,41 @@ static void test_VerifyConsoleIoHandle(void) ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error); } +static void test_GetSetStdHandle(void) +{ + HANDLE handle; + DWORD error; + BOOL ret; + + /* get invalid std handle */ + SetLastError(0xdeadbeef); + handle = GetStdHandle(42); + error = GetLastError(); + ok(error == ERROR_INVALID_HANDLE || broken(error == ERROR_INVALID_FUNCTION)/* Win9x */, + "wrong GetLastError() %d\n", error); + ok(handle == INVALID_HANDLE_VALUE, "expected INVALID_HANDLE_VALUE\n"); + + /* get valid */ + SetLastError(0xdeadbeef); + handle = GetStdHandle(STD_INPUT_HANDLE); + error = GetLastError(); + ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error); + + /* set invalid std handle */ + SetLastError(0xdeadbeef); + ret = SetStdHandle(42, handle); + error = GetLastError(); + ok(!ret, "expected SetStdHandle to fail\n"); + ok(error == ERROR_INVALID_HANDLE || broken(error == ERROR_INVALID_FUNCTION)/* Win9x */, + "wrong GetLastError() %d\n", error); + + /* set valid (restore old value) */ + SetLastError(0xdeadbeef); + ret = SetStdHandle(STD_INPUT_HANDLE, handle); + error = GetLastError(); + ok(ret, "expected SetStdHandle to succeed\n"); + ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error); +} START_TEST(console) { @@ -1187,4 +1222,5 @@ START_TEST(console) test_GetConsoleProcessList(); test_OpenConsoleW(); test_VerifyConsoleIoHandle(); + test_GetSetStdHandle(); }