| |

VerySource

 Forgot password?
 Register
Search
View: 1469|Reply: 7

I use SHELL to open an executable file, how can I close it?

[Copy link]

3

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-11-4 13:00:01
| Show all posts |Read mode
I use SHELL to open an executable file, how can I close it?
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-11-4 16:00:01
| Show all posts
The first parameter of OpenProcess() refers to the capabilities of the obtained hProcess. For example, PROCESS_QUERY_INFORMATION allows GetExitCode() to obtain the status of the process pointed to by hProcess, and PROCESS_TERMINATE is to let TerminateProcess(hProcess. .) command can take effect, that is, different parameter settings make hProcess have different permissions and capabilities.

So, you only need to set the first parameter of OpenProcess() to PROCESS_TERMINATE to close it. Of course, for TerminateProcess(hProcess, 3838) you'd better set the first parameter to PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, the following is the modified The code:

Option Explicit
Const PROCESS_QUERY_INFORMATION =&H400
Const PROCESS_TERMINATE =&H1
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Sub main()
       Dim ProcessId As Long
       Dim hProcess As Long
       ProcessId = Shell("notepad.exe", 1)'Here is the task ID returned by Shell when the function is used
       hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId)
       Call TerminateProcess(hProcess, 3838)
End Sub
Reply

Use magic Report

1

Threads

11

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-11-4 17:30:01
| Show all posts
The program window opened by the shell is easy to monitor
Reply

Use magic Report

1

Threads

11

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-11-4 17:45:02
| Show all posts
Close the program from SHELL
  Option Explicit
  Const PROCESS_QUERY_INFORMATION =&H400
  Const PROCESS_TERMINATE =&H1
   
  Dim ProcessId As Long
  Dim hProcess As Long
   
  Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
   
  Private Sub Command1_Click()
  ProcessId = Shell("notepad.exe", 1)'Here is the task ID returned by Shell when the function is used
  End Sub
   
  Private Sub Command2_Click()
  hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId)
  Call TerminateProcess(hProcess, 3838)
  End Sub

Save the handle and end it
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-11-4 18:00:02
| Show all posts
Haha~ Learned and collected!
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-11-4 18:15:01
| Show all posts
How good is it to use CloseWindow...
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-11-4 18:30:01
| Show all posts
How good is it to use CloseWindow -----------------------------
You also need findwindow for this
Reply

Use magic Report

1

Threads

2

Posts

1.00

Credits

Newbie

Rank: 1

Credits
1.00

 China

Post time: 2021-2-2 16:12:08
| Show all posts
Have you ever done a desktop management project? Ask for advice on how to implement the function of the desktop right button
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list