Jump to content

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:

30csugl.png

With this code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#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:

1p9sw7.png

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)
EndIf

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
Link to comment
Share on other sites

I had solved this problem this way:

#AutoIt3Wrapper_Change2CUI=y

#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)
Else
    $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)
                ExitLoop
            EndIf
        EndIf
    Next
    MsgBox(0, "Test", "Started from explorer")
EndIf

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...