如何在Windows中获取控制台的名称?

时间:2012-09-07 18:46:28

标签: windows console

是否可以在Windows中获取控制台的名称?就像在Unix系统中用C函数 ttyname 完成的那样

1 个答案:

答案 0 :(得分:2)

您可以使用WinAPI GetConsoleTitle函数来检索它。

您可能会发现links to all of the console functions有用。

您没有指定语言,因此来自MSDN的C ++中的here's the one

#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <strsafe.h>

int main( void )
{
   TCHAR szOldTitle[MAX_PATH];
   TCHAR szNewTitle[MAX_PATH];

   // Save current console title.

   if( GetConsoleTitle(szOldTitle, MAX_PATH) )
   {
      // Build new console title string.

      StringCchPrintf(szNewTitle, MAX_PATH, TEXT("TEST: %s"), szOldTitle);

      // Set console title to new title
      if( !SetConsoleTitle(szNewTitle) )
      {
         _tprintf(TEXT("SetConsoleTitle failed (%d)\n"), GetLastError());
         return 1;
      }
      else
      {
         _tprintf(TEXT("SetConsoleTitle succeeded.\n"));
      }
   }
   return 0;
}