Jump to content

_WinAPI_SetConsoleTitle UDF (With detailed example!)


Kealper
 Share

Recommended Posts

Ok, so this has probably been posted before, but I was looking through the MSDN library randomly just a bit ago, and decided to play around a bit with some of the console-related functions.

AutoIt can make basic console applications to provide users with some basic output (which is generally more efficient than outputting to a GUI), but wouldn't it be cool to be able to name your console windows just like you can name your GUI windows?

Instead of it being called "C:\Users\Foobar\Desktop\MyEchoServer.exe" for instance, you could make your program call itself just "My Echo Server" and such.

Here is the simple UDF on it's own, and below this is a nice little example detailing how you can use the function.

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinAPI_SetConsoleTitle
; Description ...: Change the AutoIt process's attached console title.
; Syntax ........: _WinAPI_SetConsoleTitle($sTitle[, $hDLL = "Kernel32.dll"])
; Parameters ....: $sTitle            - A string value.
;                 $hDLL             - [optional] A handle value. Default is "Kernel32.dll".
; Return values .: True if the title was able to be changed, False if it was not able to be changed.
; Author ........: Ken "Kealper" Piper
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WinAPI_SetConsoleTitle($sTitle, $hDLL = "Kernel32.dll")
    Local $iRet = DllCall($hDLL, "bool", "SetConsoleTitle", "str", $sTitle)
    If $iRet[0] < 1 Then Return False
    Return True
EndFunc

And here is the example... For best results, save this to a file and compile it as a console application.

If it isn't compiled as a console application, there is a quick and dirty DllCall in there to create a "dummy" console if it doesn't exist already which can serve to demonstrate the function anyways (but without any textual output).

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

;Create a new console window if it does not already exist, even if it is not compiled as a console executable.
;This DllCall is not part of the example, it only serves to ensure the example works correctly.
;Don't run through SciTE, the example will fail, and even this DllCall can't fix that.
DllCall("Kernel32.dll", "bool", "AllocConsole") ;Don't just rely on this though, to get the most from this, compile as a console app!

;Begin actual example.

ConsoleWrite("Keep an eye on the console's window title!" & @CRLF)
Sleep(3000)

;First: Setting the title of your console window is as easy as this!
_WinAPI_SetConsoleTitle("This is some custom text!")

;A simple count-down, so the first title change is visible for a few seconds before proceeding to the next example.
For $i = 5 To 1 Step -1
    ConsoleWrite("Proceeding in " & $i & "..." & @CRLF)
    Sleep(1000)
Next
ConsoleWrite("On to example two! This'll count to 50 and then exit." & @CRLF)

;Second: Something a bit more advanced. Changing the console's title rapidly.
;When changing the console's title rapidly, you should first open Kernel32.dll and then pass that handle
;as a second parameter to the function. This makes it use the open handle instead of having to open and
;close Kernel32.dll each time the function is called. This ends up making it more efficient.
$Kernel32 = DllOpen("Kernel32.dll") ;Open Kernel32.dll. $Kernel32 now holds a handle to the opened file.
For $i = 1 To 50 ;Count to 50
    _WinAPI_SetConsoleTitle("Counting..." & $i, $Kernel32) ;Set the new title, and tell the function to use the open handle instead.
    Sleep(100) ;Wait 100ms, so the change can be easily seen by everyone. Without this, the change would happen too fast to notice.
Next
DllClose($Kernel32) ;Finally, because we like our resources, close the open handle since we no longer need it.


; #FUNCTION# ====================================================================================================================
; Name ..........: _WinAPI_SetConsoleTitle
; Description ...: Change the AutoIt process's attached console title.
; Syntax ........: _WinAPI_SetConsoleTitle($sTitle[, $hDLL = "Kernel32.dll"])
; Parameters ....: $sTitle            - A string value.
;                 $hDLL             - [optional] A handle value. Default is "Kernel32.dll".
; Return values .: True if the title was able to be changed, False if it was not able to be changed.
; Author ........: Ken "Kealper" Piper
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WinAPI_SetConsoleTitle($sTitle, $hDLL = "Kernel32.dll")
    Local $iRet = DllCall($hDLL, "bool", "SetConsoleTitle", "str", $sTitle)
    If $iRet[0] < 1 Then Return False
    Return True
EndFunc
Link to comment
Share on other sites

Look out for complete console implementation, e.g. by Mat, progandy, Shaggi, etc. ;)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Look out for complete console implementation, e.g. by Mat, progandy, Shaggi, etc. :)

This?

Me likey a lot ;)

I have to admit, that Console.au3 is something I fully intend on staring at the source to... My console reading function stopped working when AutoIt dropped the raw file read mode, since I was getting console text by reading the device file "con"... Thanks for pointing me in the direction of this!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...