VB question...

GZ

Visiting Expert
Joined
Apr 8, 2012
Posts
1,304
Location
New Jersey
Okay... Yesterday, I spent a few hours putting together a program to act as a simple interface that launches a program and 3 shell functions.

One of the buttons launches "explorer.exe" (Windows Shell GUI)... I want to add the second function of shutting the program down to that button. I am a complete novice.

Code:
Private Sub Wshell_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Wshell.Click
        Process.Start(Environ$("SystemRoot") & ":\explorer.exe")
    End Sub

I am pretty sure I could kill the program by adding something like...

Code:
Shell("Taskkill -pid ShellUI.exe")

Would I just add that line in the sub???

Please bear with me... As I said, I am a complete noob...
 
Okay... That worked... But I am sure there is a better way of doing it...

But for now...

I am using Visual Studio 2010 VB... How can I access the bare code from the form? I want to study what I have done using the design tools.

The only thing showing on Form1 are the private subroutines for the buttons.
 
Hello again :)

I will come back to you on everything else I have yet to answer.

I think that VB and C# provide access to the designer code in the same way. I will check this evening. If they do, click the small + next to Form1.vb, and double click on Form1.Designer.vb.

Please note that you should never, ever, manually change code inside that file, as the designer could well just overwrite your changes, and it could even break the designer.

Richard
 
That is good to know!!! I will just have to find a standalone compiler to test my "changes" lol.
 
Code:
Shell("Taskkill -pid ShellUI.exe")

This is actually bad habit, in this case you're almost limited to as much as a batch script. Use Process methods instead of Shell as well too (from the System.Diagnostics namespace).

There's a few API's you could use as well... Get the handle to that process or just simply it's name, or mediocre way: get the PID when you start the process. Then use the Process.Kill method to kill the process.
 
I am all too aware of the limitations... I can't rename the file or it fails to quit... Luckily, for this application that code works perfectly, but I am trying to learn, so I want to know every possible way that will accomplish this! (and any other function that I want to perform).
 
This is our placeholder for the PID which is for our started process
Code:
Private ProcID As Integer

To start the process:
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
	Dim p As New Process With { _
					.StartInfo = New ProcessStartInfo With { _
						.FileName = "notepad.exe", _
						.Verb = "runas" _
					}, _
					.EnableRaisingEvents = True _
				}
	p.Start()
	ProcID = p.Id
End Sub

To kill that process:
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
	If Process.GetProcesses.Where(Function(p) p.Id = ProcID).Count > 0 Then Process.GetProcessById(ProcID).Kill()
End Sub
 
Last edited:
Okay... That is good to know, but I don't think it will work for this particular application. If I am reading that correctly, launching from button 1 will set the PID as a variable which will be used to kill the operation using button 2.

In this case... The process is launched at winlogon so there would have to be another way to set the PID variable.
 
If it's launched at WinLogon then you can check to see if the process exists then retrieve the PID. Or kill the process by it's name.
 
Is there a way to have the program automatically query the system processes, identify and set the PID for later termination?

I could just use the process name (ShellUI.exe) to kill it as I am doing with Shell...

It would be...

Process.Kill("ShellUI.exe")

No?
 
Is there a way to have the program automatically query the system processes, identify and set the PID for later termination?

Depends on what you mean by "automatically".

I could just use the process name (ShellUI.exe) to kill it as I am doing with Shell...

It would be...

Process.Kill("ShellUI.exe")

No?

Nope... There could be multiple processes with the same name. And Kill() assumes one process. Unless you want to kill them all by that name?

Code:
Array.ForEach(Of Process)(Process.GetProcessesByName("ShellUI.exe"), Sub(x) x.Kill())
 
Here's a way that I came up with to get all the PID's of a certain process name:

Code:
Dim PIDs As Integer() = Process.GetProcessesByName("notepad").Select(Function(p) p.Id).ToArray
If PIDs.Count > 0 Then MessageBox.Show(String.Join(", ", PIDs))

When the program starts up, depending on whether this is a console app or a windows form app, you'll either have the main form constructor, the form_load event or the main() method as the entrypoint for the actual program code to start, in which you could place this code. It would then check as soon as you start the program.
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top