Author Topic: SHELL (taskkill)  (Read 5041 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SHELL (taskkill)
« Reply #15 on: July 23, 2021, 07:44:56 am »
@krovit All those lines are needed for killing tasks with WinAPI, at least. As for finding all the references, I copied the logic from this C++ snippet on Stack Overflow and then edited it once I found some more references on the MSDN:

Code: C++: [Select]
  1. #include <windows.h>
  2. #include <process.h>
  3. #include <Tlhelp32.h>
  4. #include <winbase.h>
  5. #include <string.h>
  6. void killProcessByName(const char *filename)
  7. {
  8.     HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
  9.     PROCESSENTRY32 pEntry;
  10.     pEntry.dwSize = sizeof (pEntry);
  11.     BOOL hRes = Process32First(hSnapShot, &pEntry);
  12.     while (hRes)
  13.     {
  14.         if (strcmp(pEntry.szExeFile, filename) == 0)
  15.         {
  16.             HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
  17.                                           (DWORD) pEntry.th32ProcessID);
  18.             if (hProcess != NULL)
  19.             {
  20.                 TerminateProcess(hProcess, 9);
  21.                 CloseHandle(hProcess);
  22.             }
  23.         }
  24.         hRes = Process32Next(hSnapShot, &pEntry);
  25.     }
  26.     CloseHandle(hSnapShot);
  27. }
  28. int main()
  29. {
  30.     killProcessByName("notepad++.exe");
  31.     return 0;
  32. }
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SHELL (taskkill)
« Reply #16 on: July 23, 2021, 01:17:59 pm »
Code: QB64: [Select]
  1. SHELL _DONTWAIT "C:\Windows\System32\notepad.exe"
  2. SHELL "taskkill /IM " + CHR$(34) + "notepad.*" + CHR$(34)
  3.  

Thank you Petr and Zaadstra...

I tried the DOS command syntax in every way (obviously starting from the right one) but none worked.

Petr's code on my pc does not work in any way.
At this point I begin to believe that there is something on my PC with windows 7 (but also with 10) that is not good

Maybe a customization maybe something else that makes these commands ineffective.

Could it be a permission, or security, setting?  Turn off all the windows UAC (User Account Control) settings that you can, disable all anti-virus, firewall, and security settings, and give it a test run.  If it works then, one of your “protections” is stopping execution, and all you have to do after that is sort out which thing it is.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #17 on: July 24, 2021, 04:08:09 pm »
Hi SMcNeill...

you put me in doubt...

I tried on a "virgin" PC with win10 just installed and nothing else... Works!
I tried on a PC with win10 much navigated (load of many apps and very very personalized)... Works!

At this point it does not work on a win7 and on a win10 both connected to a corporate LAN

I don't understand what can affect the taskkill command (working locally). I have no idea!

I think it would be important to find out.
If there is something that disturbs the execution of a program must be found and the problem neutralized.

SpriggsySpriggs's code remains very useful, fortunately !

Anyway thanks


« Last Edit: July 24, 2021, 04:13:45 pm by krovit »
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SHELL (taskkill)
« Reply #18 on: July 24, 2021, 07:12:33 pm »
So it’s something specific on your setup….

At this point, I’d check for any corrupted system files:

You can press Windows + X or right-click Start, and select Windows PowerShell (Admin) to run PowerShell as administrator.

Then you can type sfc /scannow in PowerShell window, and hit Enter to run SFC command to check and repair Windows 10 system files. After the SFC scan is finished, you can restart your computer and open Command Prompt again.

I’d suggest running it until you get 2 100% clear checkups in a row.  Sometimes it’ll fix  one issue, but then still find a glitch wrong on a second pass.



If that doesn’t fix the issue, I’d just try disabling things one at a time, to hunt for the problem.  I’d test booting into Safe Mode and seeing it it worked.  Personally, I’d think it should, but if not, you may need to reinstall/repair windows completely.  If it works, reboot normally, then disable anti-virus and test…

Process of elimination to hunt for what’s blocking execution on your PC.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SHELL (taskkill)
« Reply #19 on: July 24, 2021, 07:27:33 pm »
Another thing that can affect taskkill is if the program you’re trying to kill has any child processes, in which case it’ll normally fail unless you add the /T switch.

Try this one:

$CONSOLE:ONLY
SHELL _DONTWAIT "C:\Windows\System32\notepad.exe"
_DELAY 2
SHELL “Taskkill /IM /F /T notepad.exe”
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #20 on: July 26, 2021, 03:26:21 am »
/T  (I checked: there are many child processes to terminate)
_DONTWAIT


On win7/10 in the corporate LAN there is still something that escapes me: in fact it is not clear because on PC without LAN the code works, while on these - on LAN - , /T _DONTWAIT they seem indispensable commands

In short... it's much better... but it is not clear because...


« Last Edit: July 26, 2021, 03:28:50 am by krovit »
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)