Jump to content

Free console after _WinAPI_AttachConsole()


Simpel
 Share

Recommended Posts

Hi.

Thanks to this post (https://www.autoitscript.com/forum/topic/189553-writing-to-cmd/?do=findComment&comment=1361142) I can now write a helpfile to the cmd when for instance passing parameter -h or -help at cmd. But then cmd is blocked by the script (I had to free it with CTRL+BREAK):

#include <WinAPI.au3>
_WinAPI_AttachConsole()
$hConsole = _WinAPI_GetStdHandle(1)
_WinAPI_WriteConsole($hConsole, "Print helpfile................" & @CRLF)

Inside MSDN Library then I found this sentence: A process can use the FreeConsole function to detach itself from its console.

But I don't find something like _WinAPI_FreeConsole(). How can I do it in another way?

Regards, Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Well, looking at the source code for the _WinAPI_AttachConsole in the WinAPI.au3 UDF and the FreeConsole MSDN page, I would think (unsure) it could be called using something like this:

Func _WinAPI_FreeConsole()
    Local $aResult = DllCall("kernel32.dll", "bool", "FreeConsole")
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_FreeConsole

It is curious that there isn't a function for this in the UDF.  Perhaps it is not essential...hopefully it works and fixes you issue.

Link to comment
Share on other sites

Hi.

Inside my script $aResult[0]=1 so it means it works. But when I start test.exe (it should be compiled) from cmd.exe the caret is only just blinking further and not stopping output and showing promt including caret again:

#include <WinAPI.au3>

_WinAPI_AttachConsole()
Local $hConsole = _WinAPI_GetStdHandle(1)
_WinAPI_WriteConsole($hConsole, @CRLF & "Helpfile Line 1" & @CRLF)
_WinAPI_WriteConsole($hConsole, "Helpfile Line 2" & @CRLF)
_WinAPI_WriteConsole($hConsole, "Helpfile Line 3" & @CRLF)
_WinAPI_WriteConsole($hConsole, "Helpfile Line 4" & @CRLF)
Local $iSuccess = _WinAPI_FreeConsole()
MsgBox(0, 'FreeConsole', $iSuccess)
Exit

Func _WinAPI_FreeConsole() ; provided by spudw2k
    Local $aResult = DllCall("kernel32.dll", "bool", "FreeConsole")
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_FreeConsole

cmd looks like:

console not detached.PNG

cmd should look like:

should be like this.PNG

What am I missing?

At the end I want to write out a help file if someone is starting the test.exe with parameter h or help. And it should definitely give console free.

Regards, Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

I don't want to shut the window. I just want to give the power over the cmd.exe back to system.

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Even if closing the handle the cmd.exe isn't released:

#include <WinAPI.au3>
#include <Array.au3>

_WinAPI_AttachConsole()
Local $hConsole = _WinAPI_GetStdHandle(1) ; output
_WinAPI_WriteConsole($hConsole, @CRLF & "Helpfile Line 1" & @CRLF)
_WinAPI_WriteConsole($hConsole, "Helpfile Line 2" & @CRLF)
_WinAPI_WriteConsole($hConsole, "Helpfile Line 3" & @CRLF)
_WinAPI_WriteConsole($hConsole, "Helpfile Line 4" & @CRLF)
Local $iSuccess, $aSuccess
$aSuccess = DllCall("kernel32.dll","BOOL","CloseHandle","handle",$hConsole)
MsgBox(0, 'CloseHandle', $aSuccess[0])
$iSuccess = _WinAPI_FreeConsole()
MsgBox(0, 'FreeConsole', $iSuccess)
Exit

Func _WinAPI_FreeConsole() ; provided by spudw2k
    Local $aResult = DllCall("kernel32.dll", "bool", "FreeConsole")
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_FreeConsole

CloseHandle and FreeConsole both return 1 but I can't see the cmd prompt again except pressing Ctrl+Break.

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Interesting...not sure the correct way to address this.  Doing some googling for the symptom brought up similar cases, but couldn't find a clear and applicable solution.  I assume you are going this route because you are making a hybrid GUI/console script?

Just a minor comment (not particularly useful), but I notice an Enter key also returns to the the prompt.  I would understand that if the process is being held or looping that a Ctrl + C or break would force close the app, but I can see the process terminate.  Not sure if there is another way to close the script to force a return to the prompt, but I couldn't find anything and it doesn't look like it's really "holding" anything.  I'm curious if someone else solves this in a proper manner.

Link to comment
Share on other sites

11 minutes ago, spudw2k said:

I assume you are going this route because you are making a hybrid GUI/console script?

I’m facing this problem because I want to give an overview to the user about all possible arguments or parameters the GUI-script provides. Many of the tools I use will show such an overview if you start the prog in cmd.exe with argument -help.

I don’t like the idea showing a message box. Because you have to click ok and then all infos are gone. Other ideas?

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

You can always do what the windows management tools does, and launch a gui with all the parameters listed and defined...or write the help file to a .txt, and launch via notepad run command.

All my attempts to exit the attach failed as well, (unless compiled as a command line exe)

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

@Simpel I agree with your position and approach, but I was wondering if you tried using the #AutoIt3Wrapper_Change2CUI=Y directive and the native ConsoleWrite function and if that wouldn't work for you.

I've used that method with some of my scripts--for example, my System Uptime script.  Admittedly though, none with GUI elements...but a quick test seemed to still allow GUI function and operation.

Edited by spudw2k
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...