Results 1 to 4 of 4

Thread: [RESOLVED] Self Restart an Application

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    35

    Resolved [RESOLVED] Self Restart an Application

    Self Restart an Application:
    How to restart a VB6 created application automatically if crashed...?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Self Restart an Application

    Fix the reason for the crash. Error trapping. Otherwise you can build a separate app that monitors your main app and if it closes without informing that monitoring app, your monitoring app can restart it. Your question is more in line with putting on a band-aid. Determining why it crashed is what I believe to be the right solution.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    441

    Re: Self Restart an Application

    Hi this module Restart the application if the error is standar

    in module public
    Code:
    Option Explicit
    'Autor: Leandro Ascierto
    'Web:   www.leandroascierto.com.ar
    'Date:  28/12/2009
    Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Private Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByRef lpParam As Any) As Long
    Private Declare Function DestroyWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    Private Declare Function SetProp Lib "user32.dll" Alias "SetPropA" (ByVal hwnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
    Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Sub FatalExit Lib "kernel32" (ByVal code As Long)
    
    Dim hWinStatic As Long
    Dim AppPath As String
    Dim LastError As Long
    
    Private Function CallSomeFunction()
        'No borrar esta linea
    End Function
    
    Public Sub StarProtect()
        hWinStatic = CreateWindowEx(0, "Static", "WindowControlerCrash", 0, 0, 0, 0, 0, 0, 0, 0, 0&)
        AppPath = GetAppPath
        SetTimer hWinStatic, 0, 100, AddressOf TimerProc
    End Sub
    
    Public Sub EndProtect()
        KillTimer hWinStatic, 0
        DestroyWindow hWinStatic
    End Sub
    
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        Dim Ret As String
        
        If Err.Number = 40040 Then
            ShellExecute hWinStatic, vbNullString, AppPath, LastError, vbNullString, 1
            FatalExit 1
        Else
            LastError = Err.Number
            Ret = CallSomeFunction
        End If
        
    End Sub
    
    Private Function GetAppPath() As String
        Dim ModuleName As String
        Dim Ret As Long
        ModuleName = String$(255, Chr$(0))
        Ret = GetModuleFileName(App.hInstance, ModuleName, 255)
        GetAppPath = Left$(ModuleName, Ret)
    End Function
    Test: put three Command Button in a Form and compile the project
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        If Command$ <> "" Then Me.Caption = "Aplicaci&#243;n Reinciada por error: " & Command$
        StarProtect 'comienza la protecci&#243;n
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        EndProtect 'Detiene la protecci&#243;n
    End Sub
    
    
    Private Sub Command1_Click()
        MsgBox 1 / 0 'Error Divici&#243;n por cero
    End Sub
    
    Private Sub Command2_Click()
        Dim i As Integer
        i = 8000000000000# 'Error Desvordamiento
    End Sub
    
    Private Sub Command3_Click()
        Dim c As Date
        c = "hola" 'Error no coinciden los tipos
    End Sub
    Original Post
    leandroascierto.com Visual Basic 6 projects

  4. #4

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    35

    Resolved Re: Self Restart an Application

    LaVolpe, you are absolutely right...i have to trap the errors effectively...Thank you very much

    LeandroA ---Your code is working! thanks a lot man

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