Jump to content
Sign in to follow this  

Write to CMD console [NO CUI]

Recommended Posts


Hello guys, long time no see ;)

Weel, i want to write some output to CMD if the software has lanched from the console, not if is lanched by Explorer. After some reseach i have understand i need to use AttachConsole from Kernel32.dll. Work? Yes...but is not the same "output" like the CUI version and i don't know if is a thing can be resolved or not.

I'll explain, this is an image of CUI:


With this code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

ConsoleWrite(@CRLF & "Hello World" & @CRLF)

And is ok, i want to get the same result. Insted this is my output:


Using this code:

#include <WinAPI.au3>

Local $iConsole, $hConsole, $aGetConsoleWin

$iConsole = _WinAPI_AttachConsole(-1)
If $iConsole = 0 Then Exit
$hConsole = _WinAPI_GetStdHandle(1)
If $hConsole = -1 Then Exit
_WinAPI_WriteConsole($hConsole, @CRLF & @CRLF & "Hello World" & @CRLF)
$aGetConsoleWin = DllCall("kernel32", "ptr", "GetConsoleWindow")
If Not @error Then
    DllCall("user32", "bool", "PostMessage", _
            "hwnd", $aGetConsoleWin[0], _
            "uint", 256, _  ; WM_KEYDOWN
            "wparam", 13, _ ; VK_RETURN
            "lparam", 0)

As you can see there is one line more then the CUI version, yes i know i have used two @CRLF because with only one write on C:\Windows\System32>

So there are errors? Can be removed that line to have the same output of a CUI? Thanks and have a nice day

Edited by johnmcloud

Share this post

Link to post
Share on other sites

I had solved this problem this way:


#include <Process.au3>
#include <WinAPIProc.au3>

If _ProcessGetName(_WinAPI_GetParentProcess(@AutoItPID)) = "cmd.exe" Then
    If Not $CmdLine[0] Then ConsoleWrite("Started from a cmd box" & @CRLF)
    $var = WinList()
    For $i = 1 To $var[0][0]
        If $var[$i][0] <> "" And BitAND(WinGetState($var[$i][1]), 2) Then
            If WinGetProcess($var[$i][0], "") = @AutoItPID Then
                WinSetState($var[$i][1], "", @SW_HIDE)
    MsgBox(0, "Test", "Started from explorer")

Might be not the elegant way.

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.