How do i check is some random program is running with VB6:confused:? I want to make a program that detect if a program is running if not open it and if i close the program shall it be reopened. is that eaven possible or am i hoping on to much.
Printable View
How do i check is some random program is running with VB6:confused:? I want to make a program that detect if a program is running if not open it and if i close the program shall it be reopened. is that eaven possible or am i hoping on to much.
Welcome to the forums. :wave:
To check if it is running you need to know something about it. That would not fit the description of a "random" program.
What program are you trying to find?
msn and skype i just want to make them reopen when i close them but i dont know how to make that i think i shall put some code in a timer.
i just want them to reopen if they crash or something
I am making like a console to open some of the games i am playing and i want to reopen msn and skype if the crash becuse i am lazy and want to have a program to do that for me.
You can execute a program using Shell()
Example:
... :wave:Code:Shell "C:\WINDOWS\System32\notepad.exe", vbNormalFocus
i just want to have the window down but the program running and a timer that check if it is running every secound but i dont know how to make the timer check i just know how the timer start the program. if it gets turned off the program will reopen it within a secound but right now the program atemts to open a new copy of it every secound
Requirements
As far as I understood you intented to check if a process/program is running and if it is not then you want to start it.
To check if a process/program is running you need to make a snapshot of the current processes and iterrate through them.
You will need the following APIs for that:
- CreateToolhelp32Snapshot
- Process32First
- Process32Next
- CloseHandle
You also will need the type structure "ProcessEntry32".
To run a process you use the Shell command as previously stated.
Please note this solution is my personal preference as I do not like using other API functions such as FindWindow or similar as using the name of a window is not as reliable as using the name of the actual process you are looking for. Again, that is simply my personal preference my own oppinion and does not reflect best practice or standards.
Solution
In the attached demo I have a main form with 2 textboxes and a button to allow you to specify the name and the full file path of the process/program you want to check/re-start.
In my Demo I use "Notepad.exe" and "c:\windows\SYSTEM32\notepad.exe".
The button simply executes the code and either starts Notepad or tells you if its running.
Change the values on the form as required.
I added a module "ProcessController.bas" which contains the code to iterrate through the processes and poppulates a Process object with the results.
I added a classobject "Process.cls", which represents a single process and will contain the process data.
Samples
The key-code which gets the process data:
VB Code:
'/// <summary> '/// Retrieves the data of a process by the specified name. '/// </summary> '/// <param name="myProcessName">The name of the process to get the data for.</param> '/// <param name="myProcess">The Process object to populate with the data.</param> '/// <remarks> '/// The myProcess parameter is a reference and will be updated with the results. '/// If a match was found the myProcess.IsRunning property will be True. '/// If not match was found of an error occurred, the myProcess.IsRunning property will be False. '/// </remarks> Public Sub GetProcessByName(ByVal myProcessName As String, ByRef myProcess As Process) Dim hSnapshot As Long Dim processEntry As ProcessEntry32 Dim response As Long Dim index As Integer On Error GoTo Catch If (myProcess Is Nothing) Then Set myProcess = New Process End If myProcess.IsRunning = False hSnapshot = CreateToolhelp32Snapshot(SnapshotFlags.TH32CS_SNAPPROCESS, 0) If (hSnapshot = -1) Then Err.Raise 999, App.Title, "Unable to get process snapshot." End If processEntry.dwSize = Len(processEntry) response = Process32First(hSnapshot, processEntry) Do While (response > 0) myProcess.FullName = Left(processEntry.szExeFile, InStr(processEntry.szExeFile & vbNullChar, vbNullChar) - 1) If (UCase(myProcess.FullName) = UCase(myProcessName)) Then myProcess.Identifier = processEntry.th32ProcessID myProcess.ThreadCount = processEntry.cntThreads myProcess.ParentIdentifier = processEntry.th32ParentProcessID myProcess.IsRunning = True response = 0 Else myProcess.FullName = "" response = Process32Next(hSnapshot, processEntry) End If Loop Finally: Exit Sub Catch: Call MsgBox("Error occurred in ''ReadProcessDemo.ProcessController.GetProcessByName''" & vbCrLf & vbCrLf & "(" & Err.Number & ")" & vbCrLf & Err.Description, vbCritical + vbOKOnly, App.Title) Resume Finally End Sub
This is how you use it:
VB Code:
'/// <summary> '/// Checks if the specified process is running and if not start it. '/// </summary> Private Sub StartProcess(myProcessFileName As String, myProcessFilePath As String) Dim myProcess As Process Set myProcess = New Process myProcess.FilePath = myProcessFilePath Call ProcessController.GetProcessByName(myProcessFileName, myProcess) If (Not (myProcess.IsRunning)) Then MsgBox "Process (" & myProcessFileName & ") is not running and will be started." Call Shell(myProcess.FilePath, vbMinimizedNoFocus) Else MsgBox "Process (" & myProcessFileName & ") is allready running." End If End Sub
Download the attachment for the full demo.
Modify the code to your needs.
The APIs will work on Windows95, 98, 2000 and XP but not on Windows NT.
I don't know if they work on VISTA or Windows 7.
I hope this helps.
If it did, please rate my Post.
Edit
Feel free to add a "Start" method to the Process object which will execute the shell command for you, using it's own properties.
Thus when needing to start the process all you do is myProcess.Start.
As the class object has all the required properies populated it will work and it's the proper way of writing the code as well.