Results 1 to 10 of 10

Thread: [RESOLVED] how do i open and reopen programs?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Resolved [RESOLVED] how do i open and reopen programs?

    How do i check is some random program is running with VB6? 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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how do i open and reopen programs?

    Welcome to the forums.

    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?

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: how do i open and reopen programs?

    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.
    Last edited by Healingbot; Feb 17th, 2010 at 07:20 AM.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: how do i open and reopen programs?

    Quote Originally Posted by Healingbot View Post
    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.
    Why you want to do that...????

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: how do i open and reopen programs?

    i just want them to reopen if they crash or something

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: how do i open and reopen programs?

    Quote Originally Posted by Healingbot View Post
    i just want them to reopen if they crash or something
    Why don't you open it manually, on crashing...???
    Could you explain the situation...???

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: how do i open and reopen programs?

    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.

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: how do i open and reopen programs?

    You can execute a program using Shell()

    Example:
    Code:
    Shell "C:\WINDOWS\System32\notepad.exe", vbNormalFocus
    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: how do i open and reopen programs?

    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
    Last edited by Healingbot; Feb 17th, 2010 at 08:11 AM. Reason: wrote some things wrong

  10. #10
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: [RESOLVED] how do i open and reopen programs?

    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:
    1. '/// <summary>
    2. '/// Retrieves the data of a process by the specified name.
    3. '/// </summary>
    4. '/// <param name="myProcessName">The name of the process to get the data for.</param>
    5. '/// <param name="myProcess">The Process object to populate with the data.</param>
    6. '/// <remarks>
    7. '/// The myProcess parameter is a reference and will be updated with the results.
    8. '/// If a match was found the myProcess.IsRunning property will be True.
    9. '/// If not match was found of an error occurred, the myProcess.IsRunning property will be False.
    10. '/// </remarks>
    11. Public Sub GetProcessByName(ByVal myProcessName As String, ByRef myProcess As Process)
    12.     Dim hSnapshot As Long
    13.     Dim processEntry As ProcessEntry32
    14.     Dim response As Long
    15.     Dim index As Integer
    16.    
    17.     On Error GoTo Catch
    18.    
    19.     If (myProcess Is Nothing) Then
    20.         Set myProcess = New Process
    21.     End If
    22.    
    23.     myProcess.IsRunning = False
    24.    
    25.     hSnapshot = CreateToolhelp32Snapshot(SnapshotFlags.TH32CS_SNAPPROCESS, 0)
    26.    
    27.     If (hSnapshot = -1) Then
    28.         Err.Raise 999, App.Title, "Unable to get process snapshot."
    29.     End If
    30.    
    31.     processEntry.dwSize = Len(processEntry)
    32.    
    33.     response = Process32First(hSnapshot, processEntry)
    34.    
    35.     Do While (response > 0)
    36.         myProcess.FullName = Left(processEntry.szExeFile, InStr(processEntry.szExeFile & vbNullChar, vbNullChar) - 1)
    37.        
    38.         If (UCase(myProcess.FullName) = UCase(myProcessName)) Then
    39.             myProcess.Identifier = processEntry.th32ProcessID
    40.             myProcess.ThreadCount = processEntry.cntThreads
    41.             myProcess.ParentIdentifier = processEntry.th32ParentProcessID
    42.             myProcess.IsRunning = True
    43.            
    44.             response = 0
    45.         Else
    46.             myProcess.FullName = ""
    47.             response = Process32Next(hSnapshot, processEntry)
    48.         End If
    49.     Loop
    50.  
    51. Finally:
    52.     Exit Sub
    53.    
    54. Catch:
    55.     Call MsgBox("Error occurred in ''ReadProcessDemo.ProcessController.GetProcessByName''" & vbCrLf & vbCrLf & "(" & Err.Number & ")" & vbCrLf & Err.Description, vbCritical + vbOKOnly, App.Title)
    56.     Resume Finally
    57. End Sub

    This is how you use it:
    VB Code:
    1. '/// <summary>
    2. '/// Checks if the specified process is running and if not start it.
    3. '/// </summary>
    4. Private Sub StartProcess(myProcessFileName As String, myProcessFilePath As String)
    5.     Dim myProcess As Process
    6.    
    7.     Set myProcess = New Process
    8.     myProcess.FilePath = myProcessFilePath
    9.    
    10.     Call ProcessController.GetProcessByName(myProcessFileName, myProcess)
    11.    
    12.     If (Not (myProcess.IsRunning)) Then
    13.         MsgBox "Process (" & myProcessFileName & ") is not running and will be started."
    14.         Call Shell(myProcess.FilePath, vbMinimizedNoFocus)
    15.     Else
    16.         MsgBox "Process (" & myProcessFileName & ") is allready running."
    17.     End If
    18. 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.
    Attached Files Attached Files
    Last edited by Optional; Feb 17th, 2010 at 11:11 AM.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width