CliSAK class - 2021/06/26 AHK v1

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - Updated: 2020/07/31 - AHK v1 / v2-a119

31 Jul 2020, 05:36

Update:2020/07/31
  • Uploaded changes for AHK v2-a119
  • v2 version moved to v2 forum
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2020/09/28

28 Sep 2020, 06:13

Update:2020/09/28
  • Improved consistency of operation across modes, optimized code
  • Added QuitStringCallback() for user defined quit string.
  • Fixed handling of ADB environment. Prompt consistently shown after command execution.
  • Finished documentation comments (top of the script).
  • Added ConsoleSend() method for proper usage of StdIn using mode "m" (thanks @lexikos!)
  • Added GetLastLine(str) method for usage in callbacks to filter output.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: CliSAK class - 2020/09/28

28 Sep 2020, 10:35

Thanks again, @TheArkive!
For example 8, on my Win7 Pro 64-bit system, I needed to modify some code in the example to get it to show output for all the commands. I'm sure there's other ways to do it but here's what worked for me:

Code: Select all

	cmd := "cmd /K dir C:\Windows\System32"
         . " && ping 127.0.0.1"
		 . " && ping 127.0.0.1"
         . " && echo " done ; "done" is set as global above
	options := "mode:m(100,15)orp|ID:modeM|QuitString:" done ; console size = 100 columns / 15 rows
And of course I needed to click "Show Window" to see what was happening. :D
That's some nice functionality you put in there!
Regards,
burque505
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2020/09/28

28 Sep 2020, 11:30

@burque505

Thanks for the reply :D glad it's mostly working for ya.

The "`r`n" separators should be working. The whole point is to "bypass" the char limit on what can be passed to the command line. If you have any error messages or indication of what is going wrong please post. I'll try to replicate this issue on a Win 7 virtual machine.

I'm sorry to say shortly after my initial post (for this version) i did end up making some minor changes/corrections to the AHK v1 version as I was going through the AHK v2 conversion (found a few issues in the v1 version too). If you get a chance please try downloading and trying example 8 again.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: CliSAK class - 2020/09/28

28 Sep 2020, 11:54

@TheArkive, there are no error messages, but for Ex. 8

Code: Select all

cmd /K dir C:\Windows\System32`r`n
runs, but

Code: Select all

         . "ping 127.0.0.1`r`n"
         . "ping 127.0.0.1`r`n"
         . "echo " done ; "done" is set as global above
do not. The command prompt returns in the window following the execution of the first command.
I did re-download, but I think it's the version I already had, judging by the timestamp.

UPDATE: I just ran Ex. 8 on a Win10 machine, works perfectly. Some other differences between Win7 and Win10 (for Ex. 8) are that, on Win7, it's necessary to click "Show Window", while for Win10 it shows in the GUI. On Win7, the message box for the end of the action doesn't appear, which leads me to believe the callback function won't get triggered on Win7. Maybe it's just my system? Maybe someone else will chime in with some results.

Regards,
burque505
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2020/09/28

28 Sep 2020, 12:07

@burque505

Odd, thanks for the info, I'll investigate. Using the latest version of AHK v1, or an earlier version?
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: CliSAK class - 2020/09/28

28 Sep 2020, 12:10

Sorry! 1.1.33.02.
EDIT: On Win10, I need to add

Code: Select all

; AHK v1 
full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}
for example 8 to run.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2020/09/28

28 Sep 2020, 12:53

@burque505

Thanks for the info! :D I hope we can find out why this is so on your machine.

Just wondering... in your previous comment, you said you tried Win10 Ex #8 and it worked perfectly. Did you mean, you had to add this code for Win7 Ex #8 to work?

I just tried it in Win7 32-bit and it worked fine (without your above mentioned code). Do you have User Account Control enabled on your Win7 machine?

Just trying to understand the reason why it doesn't work on your machine without the extra code.

EDIT: Example 8 really is meant to be done in the context of the "wget.exe example", check the comments in the example file around example 8 code.

EDIT2: Are you on Win7 SP1? Or earlier? 64-bit or 32-bit? Just trying to come up with as many reasons as I can think of why it worked for me, and not for you...
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: CliSAK class - 2020/09/28

28 Sep 2020, 13:52

@TheArkive, I believe I have created a red herring, my apologies.
As it turns out, the culprit is running it from Scite4AHK. :headwall: If I just click on the script, it runs fine from both my machines, with no elevation code needed.
Moral - ween myself off Scite4AHK, it just isn't worth the grief.
Regards,
burque505.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2020/09/28

28 Sep 2020, 13:59

@burque505

Hey man no worries :dance: we figured it out, and that's what's most important. Thanks for the info. That gave me a good chuckle ;)

EDIT: Did you get a chance to try out the wget.exe example for #8? It's technically a small achievement, but kinda game changing at the same time for background CLI processes with a fancy GUI front all in AHK.
phacks
Posts: 2
Joined: 09 Apr 2021, 03:05

Re: CliSAK class - 2020/11/12

10 Apr 2021, 05:37

@TheArkive I can't thank you enough!

I've been searching for days for a way to silently listen to tshark (wireshark cli) output from an ahk application. I've tried my own implementations and various other solutions I've found in the forums, but none matched all my needs (hidden cmd, streaming output, not blocking the UI / 100% cpu). When I finally registered here to ask for help, I decided to do one last search... and stumbled across this masterpiece.
Checks all my requirements, quite recent updates, clean code, well documented - What a find! So thank you very much for sharing this publicly.

I have one question regarding proper termination. Since tshark constantly streams network traffic and never terminates, I have to force termination - usually when closing the application.

Code: Select all

OnExit("cleanUp")
cleanUp(){
	if(IsObject(console)){
		console.CtrlBreak()
		console.close()
	}
}
Is this a "proper" way to do it? The .close() description states you should send .ctrlBreak before .close, but .close itself calls .ctrlBreak. So what is really needed to make sure there are no leftovers?
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2020/11/12

10 Apr 2021, 06:26

@phacks

Hah! Good catch :P ... this lib is actually due for an update.

It's probably not necessary to be honest (given that .CtrlBreak() is called already).

The main thing to check is the TaskManager. Be sure that your app is properly terminating. Don't only look for instances of cmd.exe but also conhost.exe. There is a correlation between user run command line, and instances of conhost.exe. Normally, running a command line window (hidden or not) will spawn another instance of conhost.exe. Depending on how you terminate your app, it is possible to actually see cmd.exe go away, but the correlating conhost.exe will remain. This isn't always "bad", but it's important to be aware of so you can avoid leaking resources/instances of command line.

If it is streaming, then you will need to use either .CtrlBreak() or .CtrlC(). If one doesn't work, try the other, then .close().

Hope that answers your question. Thanks for the success story! :D
phacks
Posts: 2
Joined: 09 Apr 2021, 03:05

Re: CliSAK class - 2020/11/12

10 Apr 2021, 07:04

@TheArkive
Thanks for the elaboration on winows process hosting. I did indeed initially check taskmgr for cmd.exe instances but noticed that there are conhost processes spawning and terminating along with it. So I think I'm good :) Thread subscribed!
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: CliSAK class - 2021/06/26 AHK v1

26 Jun 2021, 05:56

Update:2021/06/26
* Please check the change log on the AHK v2 version here. The AHK v1 and v2 versions are now aligned again.
* This might be the last update for the v1 version. We'll see...

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble, ositoMalvado and 130 guests