--- mstools/samples/console/contitle.c 2018/08/09 18:20:36 1.1.1.1 +++ mstools/samples/console/contitle.c 2018/08/09 18:23:16 1.1.1.3 @@ -1,3 +1,14 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + #include #include #include "console.h" @@ -22,7 +33,7 @@ void demoGetTitle(HANDLE hConOut) setConTitle(__FILE__); hStdIn = GetStdHandle(STD_INPUT_HANDLE); - PERR((int) hStdIn != -1,"GetStdHandle"); + PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle"); /* save the console mode */ bSuccess = GetConsoleMode(hStdIn, &dwStdInMode); PERR(bSuccess, "GetConsoleMode"); @@ -42,10 +53,13 @@ void demoGetTitle(HANDLE hConOut) myPuts(hConOut, "\nEnter a new console title:"); bSuccess = ReadFile(hStdIn, szTitleBuf, 256, &dwBytesRead, NULL); PERR(bSuccess, "ReadFile"); - /* null terminate the string - less two for cr/lf */ - szTitleBuf[dwBytesRead - 2] = 0; - bSuccess = SetConsoleTitle(szTitleBuf); - PERR(bSuccess, "SetConsoleTitle"); + if (dwBytesRead > 2) /* did the user type any chars before hitting return? */ + { + /* null terminate the string - less two for cr/lf */ + szTitleBuf[dwBytesRead - 2] = 0; + bSuccess = SetConsoleTitle(szTitleBuf); + PERR(bSuccess, "SetConsoleTitle"); + } /* restore the console title to the original mode */ bSuccess = SetConsoleMode(hStdIn, dwStdInMode); PERR(bSuccess, "SetConsoleMode");