Autohotkey script - taskkill.exe error on Windows shutdown - autohotkey

I am using lot of portable application locally on my laptop and if i leave some of them opened when i shutdown Windows it doesn't close them properly and some applications don't save their settings. I am trying to use taskkill option in a autohotkey script which will be executed on windows shutdown and will close properly opened portable application.
I am using this script:
#SingleInstance, force
#Persistent
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
OnExit , closepgs
return
closepgs:
If (A_ExitReason="Shutdown" or A_ExitReason="Logoff")
{
Run, taskkill.exe /F /IM uTorrent.exe
}
ExitApp`
But when i shutdown Windows i receive this taskkill.exe error:
"Application failed to initialize properly (0xc0000142). Click on OK to terminate the application"
If i use the taskkill code outside the script like this it works fine:
^!Home::
Run, taskkill.exe /F /IM uTorrent.exe
But if i use it inside the script i receive taskkill.exe error.
Any idea why this error happening?
I have Windows XP Pro SP3.

Use Process, Close instead of taskkill.exe:
Process, Close, uTorrent.exe
It seems that as of Windows Vista and later, processes aren't allowed to spawn new processes during the shutdown phase. The only source a quick web search yielded was this one.
On a different note, using native functionality instead of calling programs is almost always preferable. Especially when using AHK, I recommend always looking for a built-in function to start with.

Related

Why is the termination behaviour of vscode different with other GUI program (WinMerge) when invoking from PowerShell?

In Windows PowerShell 5.1, after run & code ., a VSCode window opens, and the control returns back to PowerShell immediately. After the PowerShell exists, the VSCode will not be terminated.
On the other hand, when invoke other external program, such as WinMerge, after run & WinMergeU, a WinMerge window opens, and the control does not return back to PowerShell until WinMerge window is closed. And If PowerShell exists, WinMerge will be terminated.
Why the behaviour is different?
the difference is what is actuall happening:
when you run the command code, you are not really running code.exe. its starting a cmd script that spawns a new code.exe process with whatever arguments you passed it.
to see what a command actually executes, use the command get-command 'yourcommand', or with code get-command code.
this will show the follwing source: C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd.
Opening up this will show you:
#echo off
setlocal
set VSCODE_DEV=
set ELECTRON_RUN_AS_NODE=1
"%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" --ms-enable-electron-run-as-node %*
endlocal
so this means that in both cases you are waiting for execution to end, but for code its a code.cmd script and not actually code.exe.
If you want to start new processes and don't wait for them, you can use the command start-process winmergeu

Close a CMD window which opened through MATLAB

I am running a program which calls powershell via the system command multiple times. At the end I end up with many opened windows. I would like either these windows to close after each iteration has been completed or run in the background without opening the command window.
pubPath = 'powershell -inputformat none cd C:some path in my pc &';
[status,publisher] = system(pubPath);
I've tried the exit and exit() commands at the end of my paths but nothing has worked so far. Any help would be great.
For future reference, I found a solution to my issue. Execute this command after the loop executes all its instances and all the spawned command windows will be closed. BECAREFUL, you might kill more processes than you want.
system('taskkill /IM cmd.exe')

ConEmu: Issues starting PowerShell

I can't get the PowerShell started from a ConEmu console.
Trying to start with powershell.exe -NoProfile (works from cmd.exe). When I start it as a task I get:
ConEmuC: Root process was alive less than 10 sec, ExitCode=0.
Press Enter or Esc to close console...
Even if I start a cmd.exe console in ConEmu and execute powershell.exe -NoProfile nothing happens. Tried with cmd.exe /k powershell.exe -NoProfile. No way to get the PowerShell console.
Any hints to debug this strange behaviour?
I was having this same problem at about the same time you did and I couldn't figure it out even after I upgraded to newer versions of CONEMU 180617. I also don't have Admin rights on my work computer and I really wanted my CONEMU setup back for Powershell. Well I finally got it working and I hope this will help you as well. I am able to run Powershell on my Windows 10 machine now!
Create a new task and then use this syntax:
powershell.exe -cur_console:c1:f:i:n
From the ConEmu Docs
cur_console[:switches]
c - force enable ‘Press Enter or Esc to close console’ confirmation
c0 - wait for Enter/Esc silently
c1 - don't close console automatically, even by Enter/Esc
f - force starting console active, useful when starting several consoles simultaneously
i - don't inject ConEmuHk into starting process
n - disable ‘Press Enter or Esc to close console’

Install4j - is there any way killing currently used ports?

When our installer getting failed, a rollback process is starting by default.
Because our installer involves Windows services, we use the Optional Rollback Script property to close and delete these services in case of a failure after their installation.
Our problem is that some processes remain assigned to some ports, a situation which leads the user to not be able to delete the installation directory without killing these ports first.
Now, in the command line, it's quite easy to locate and kill these processes with the commands:
netstat -ano | findstr :<portNumber>
taskkill /PID <processId> /F
My problem is that I'm not able to run these commands through the Run Script action, getting their processes id's and than close them. (not matter what, I cannot get the output back)
Is there any such a build-in option in the Installer? If not, is there any alternative way?
You can use the WinProcesses API to kill processes:
https://resources.ej-technologies.com/install4j/help/api/com/install4j/api/windows/WinProcesses.html
As for calling netstat, you have to call
netstat -ano
with a "Run executable or batch file" action and set its "Redirect stdout" property to "To installer variable". Then you can parse the output in a "Run script" action.

Robocopy fails with error 121 when running in a new window of conemu

I have a long running process that spans robocopy windows to parallelize copying process. It goes well under cmd.exe, but when I try it under conemu, child windows has the following message:
ConEmuC: Root process was alive less than 10 sec, ExitCode=1.
Press Enter or Esc to close console...
And parent window:
[2015-07-08 09:57:20] Error: Could not copy xxxxx. Robocopy ExitCode: 121.
How can I debug and fix that?
Perhaps your batch uses start to start new console with robocopy. You may disable start processing in the ConEmu's Settings. Or may be you are using old ConEmu version. Actually there may be other issues but without exact information nobody can tell you more.