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

0 Members and 1 Guest are viewing this topic.

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
SHELL (taskkill)
« on: July 19, 2021, 03:31:10 am »
A weak point of QB64 I think is the shell to run DOS commands.
Some work, others don't... it takes a bit of luck.

For example CMD /C TASKKILL /F /IM... it never seems.

To work Tips?

Thank you!

Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SHELL (taskkill)
« Reply #1 on: July 19, 2021, 08:45:08 am »
I think CMD /C is already part of our shell string on the back end. Try it without CMD /C
Shuwatch!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #2 on: July 20, 2021, 03:42:27 am »
Thank you, SpriggsySpriggs
but not even so it works.

Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: SHELL (taskkill)
« Reply #3 on: July 20, 2021, 05:51:06 pm »
I think CMD /C is already part of our shell string on the back end. Try it without CMD /C
When did this change?  I don't knpw better than this has to be added for 'difficult' commands.  Indeed a bit trial and error.

Quote from: Krovit
For example CMD /C TASKKILL /F /IM... it never seems.
Is it possible the command won't execute because it is missing elevated rights (administrator)?

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #4 on: July 21, 2021, 03:44:51 am »
Hello, Zaadstda

Frankly the behavior seems random... can't find constant logic in the behavior... maybe it's me who's wrong. For sure you have to abound with CHR$(34) in code composition.

The problem of Administrator rights exists and depends, of course, by windows (which likes to complicate things).

The problem sometimes, but not always, is solved by running the EXE file as an administrator.

It would be nice to find a reliable system that works to give these rights of administrator within the BAS code.

Things can improve if you launch a BAT file from the inside of the code. The exact same institutions launched with SHELL do not work! But I would not want to be a slave to the presence of the batch file.

Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #5 on: July 21, 2021, 04:51:07 am »
However, several problems remain...

This works
Code: QB64: [Select]
  1. shell "ren c:\"+ chr$(34)+ "example.txt" + chr$(34) + " " + chr$(34) + "example.txt_" + chr$(34)
  2.  


This NOT works
Code: QB64: [Select]
  1. shell "ren "+ chr$(34)+ "c:\example.txt" + chr$(34) + " " + chr$(34) + "c:\example.txt_" + chr$(34)
  2.  


This NOT works
Code: QB64: [Select]
  1. file1$ = "example.txt"
  2. file2$ = file1$+"_"
  3.  
  4. shell "ren "+ chr$(34)+ file1$ + chr$(34) + " " + chr$(34) + file2$ + chr$(34)
  5.  


This NOT works
Code: QB64: [Select]
  1. file1$ = "example.txt"
  2. file2$ = file1$+"_"
  3.  
  4. shell "ren "+ chr$(34)+ "c:\example.txt" + chr$(34) + " " + chr$(34) + "c:\example.txt_" + chr$(34)
  5.  


This works
Code: QB64: [Select]
  1. file1$ = "example.txt"
  2. file2$ = file1$+"_"
  3.  
  4. shell "ren C:\"+ chr$(34)+ file1$ + chr$(34) + " " + chr$(34) + file2$ + chr$(34)


In practice it seems (it seems!) that the file name should NOT contain the path.
The full path to the file MUST be put together with the dos command.
The path can be assigned to a variable, and this can be added to the DOS command, but it can NOT be inside the file name...


Taskkill, instead, it seems to ignore all this...

To the DOS command "Copy", instead, everything is fine... and works with any combination...

But not always always... :(




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

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SHELL (taskkill)
« Reply #6 on: July 21, 2021, 07:41:02 am »
You could also just use http://www.qb64.org/wiki/NAME
Shuwatch!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #7 on: July 21, 2021, 02:51:01 pm »
Thanks for reminding me: NAME I knew (but I had forgotten!); soon I will need.

However the problem on DOS command remains. Maybe it's just mine but something is not convincing (see examples).
Every now and then it happens that if he can't do without it and then you have to invent something.

For TASKKILL (what now interests me most) I'll have to resort to a batch file.

Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SHELL (taskkill)
« Reply #8 on: July 21, 2021, 03:01:17 pm »
What is your intention with taskkill (other than the obvious; killing a task). Are you writing up a program that needs to be able to stop any process by name? Or is this for stopping a particular process on certain intervals or events?
Shuwatch!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #9 on: July 22, 2021, 03:31:03 am »
I use win 7 (but on win 10 it's the same thing).

One of the goals is also to terminate processes that start on their own under certain conditions.
At this time I need to kill a particular program.

For taskkil, more than other dos commands, you need administrator rights.

Getting the rights is a problem!

For taskkill, it seems, it is not enough even to run the EXE file in administrator mode.
And to BAT file, by its nature, cannot be assigned administrator rights for execute.

So you don't get anything: neither with BAS code, nor with the EXE, nor with the BAT file.

From the command line, in a CMD window opened in administrator mode, everything works perfectly !!

But now... I think I'll amaze you!

Sometimes... only a few very rare times... for example this morning when I tried again (the PC had not been turned off) the first time - ONLY THE FIRST TIME - the EXE file, without administrator rights, terminated the program !!!

Then, nothing more. Instead, the execution is planted and does not execute the END command.

Virus? no, there are none; mystery? yes, it is Maybe ghosts...

I'm losing hope...

Code: QB64: [Select]
  1. SHELL "C:\Windows\System32\notepad.exe"
  2. SHELL "taskkill /IM " + CHR$(34) + "notepad.*" + CHR$(34)
  3.  

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

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: SHELL (taskkill)
« Reply #10 on: July 22, 2021, 10:17:18 am »
I tried your broken example with the REN command for dos. I proceeded as follows: instead of calling SHELL directly from QB64, I assigned the entire Dos command to a string variable, pasted it into the clipboard with the _CLIPBOARD $ command, and pasted the string into a .BAT file in a text editor, and added the PAUSE command at the end (same as SLEEP in QB64) (to find out why it doesn't work). The syntax was bad. The REN command did not want the REN format "path / file" "path / file" but REN path / file file. In addition, quotation marks are not required for the dos system.

Otherwise, terminating processes using TASKKILL is not a problem if I get the process ID with the TASKLIST command, which I need to terminate with the TASKKILL / PID [process ID] / T command.


Automatically kill notepad after two seconds:

Code: QB64: [Select]
  1. SHELL _DONTWAIT "C:\Windows\System32\notepad.exe"
  2. SHELL "taskkill /IM " + CHR$(34) + "notepad.*" + CHR$(34)
  3.  

« Last Edit: July 22, 2021, 10:33:50 am by Petr »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SHELL (taskkill)
« Reply #11 on: July 22, 2021, 11:21:50 am »
@krovit I had to redo this code to make it look better. It used to look a little ugly. It was written prior to v1.5 but now it is up to v1.5 syntax. It works in 64 bit and should work in 32 as well.

Replace "untitled" with the filename of a program. exe extension is already assumed. The code right now will kill a program called "untitled.exe". You can also kill a program by PID if you have that available to you.

Code: QB64: [Select]
  1. '$Console:Only
  2.  
  3. Type PROCESSENTRY32
  4.     As _Unsigned Long dwSize, cntUsage, th32ProcessID
  5.     $If 64BIT Then
  6.         As String * 4 padding
  7.     $End If
  8.     As _Unsigned _Offset th32DefaultHeapID
  9.     As _Unsigned Long th32ModuleID, cntThreads, th32ParentProcessID
  10.     As Long pcPriClassbase
  11.     As _Unsigned Long dwFlags
  12.     As String * 260 szExeFile
  13.  
  14. 'Flags
  15. Const TH32CS_INHERIT = &H80000000
  16. Const TH32CS_SNAPHEAPLIST = &H00000001
  17. Const TH32CS_SNAPMODULE = &H00000008
  18. Const TH32CS_SNAPMODULE32 = &H00000010
  19. Const TH32CS_SNAPPROCESS = &H00000002
  20. Const TH32CS_SNAPTHREAD = &H00000004
  21. Const TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST Or TH32CS_SNAPMODULE Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD
  22.  
  23. Const PROCESS_TERMINATE = &H0001
  24.  
  25.     Function CreateToolhelp32Snapshot& (ByVal dwFlags As _Unsigned Long, Byval th32ProcessID As _Unsigned Long)
  26.     Function Process32First& (ByVal hSnapshot As _Offset, Byval lppe As _Offset)
  27.     Function Process32Next& (ByVal hSnapshot As _Offset, Byval lppe As _Offset)
  28.     Function OpenProcess%& (ByVal dwDesiredAccess As _Unsigned Long, Byval bInheritHandle As Long, Byval dwProcessId As _Unsigned Long)
  29.     Function TerminateProcess& (ByVal hProcess As _Offset, Byval uExitCode As _Unsigned Long)
  30.     Sub CloseHandle (ByVal hObject As _Offset)
  31.  
  32. 'NOTE: Processes do NOT close gracefully with this option. It's just to kill something immediately.
  33.  
  34. 'Print KillProcessByName("untitled") 'Doesn't kill gracefully. Think twice before using.
  35. 'Print KillProcessByPID(5660) 'Doesn't kill gracefully. Think twice before using.
  36.  
  37. Function KillProcessByName& (processname As String)
  38.     Dim As Long snap
  39.     snap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0)
  40.     Dim As PROCESSENTRY32 pEntry
  41.     pEntry.dwSize = Len(pEntry)
  42.     Dim As Long hRes
  43.     hRes = Process32First(snap, _Offset(pEntry))
  44.     While hRes
  45.         If Mid$(pEntry.szExeFile, 1, Len(processname + ".exe")) = processname + ".exe" Then
  46.             pEntry.szExeFile = ""
  47.             Dim As _Offset hProcess
  48.             hProcess = OpenProcess(PROCESS_TERMINATE, 0, pEntry.th32ProcessID)
  49.             If hProcess <> 0 Then
  50.                 Dim As Long terminate
  51.                 terminate = TerminateProcess(hProcess, 0)
  52.                 CloseHandle hProcess
  53.                 If terminate Then KillProcessByName = terminate
  54.             End If
  55.         End If
  56.         hRes = Process32Next(snap, _Offset(pEntry))
  57.     Wend
  58.     CloseHandle snap
  59.  
  60. Sub KillProcessByName (processname As String)
  61.     Dim As Long a
  62.     a = KillProcessByName(processname)
  63.  
  64. Function KillProcessByPID& (PID As _Unsigned Long)
  65.     Dim As _Offset hProcess
  66.     hProcess = OpenProcess(PROCESS_TERMINATE, 0, PID)
  67.     If hProcess <> 0 Then
  68.         Dim As Long terminate
  69.         terminate = TerminateProcess(hProcess, 0)
  70.         CloseHandle hProcess
  71.         If terminate Then KillProcessByPID = terminate
  72.     End If
  73.  
  74. Sub KillProcessByPID (PID As _Unsigned Long)
  75.     Dim As Long a
  76.     a = KillProcessByPID(PID)
« Last Edit: July 22, 2021, 11:35:31 am by SpriggsySpriggs »
Shuwatch!

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: SHELL (taskkill)
« Reply #12 on: July 22, 2021, 02:18:50 pm »
However, several problems remain...


In practice it seems (it seems!) that the file name should NOT contain the path.
The full path to the file MUST be put together with the dos command.
The path can be assigned to a variable, and this can be added to the DOS command, but it can NOT be inside the file name...


Taskkill, instead, it seems to ignore all this...

To the DOS command "Copy", instead, everything is fine... and works with any combination...

But not always always... :(

Next to all other solutions in this thread. 

If in doubt that a command is working,  try it in a CMD window (DOS box).  You not only can see error messages, but most commands show a good help /? function:

Code: Text: [Select]
  1. R:\>ren r:\a.txt r:\b.txt
  2. The syntax of the command is incorrect.
  3.  
  4. R:\>ren /?
  5. Renames a file or files.
  6.  
  7. RENAME [drive:][path]filename1 filename2.
  8. REN [drive:][path]filename1 filename2.
  9.  
  10. Note that you cannot specify a new drive or path for your destination file.
  11.  
  12. R:\>

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #13 on: July 23, 2021, 03:57:32 am »
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.


Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: SHELL (taskkill)
« Reply #14 on: July 23, 2021, 04:01:28 am »
Thank you SpriggsySpriggs...

The code works perfectly even on my machines!

I wonders because all these lines of code are needed for a simple taskkill.

Of course I wonder where you found all those references !
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)