Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Interceptar error y reiniciar exe

Estas en el tema de Interceptar error y reiniciar exe en el foro de Visual Basic clásico en Foros del Web. Buenas Noches. Hace un tiempo vi en internet un codigo que hacia que al haber un error del software que hice en vb. El programa ...
  #1 (permalink)  
Antiguo 15/04/2011, 22:38
 
Fecha de Ingreso: marzo-2011
Mensajes: 21
Antigüedad: 13 años
Puntos: 1
Interceptar error y reiniciar exe

Buenas Noches.

Hace un tiempo vi en internet un codigo que hacia que al haber un error del software que hice en vb. El programa se reiniciara, quiero saber si alguien lo tiene o tiene otro ejemplo por alli.

Es si mi programa llega a dar error por cualquier motivo (por ejemplo: error, argumento no valido), me gustaria que se reiniciarse. obviamente que mi programa de algun error no es la idea, pero siempre surge la posibilidad.
  #2 (permalink)  
Antiguo 15/04/2011, 23:11
 
Fecha de Ingreso: marzo-2011
Mensajes: 21
Antigüedad: 13 años
Puntos: 1
Respuesta: Interceptar error y reiniciar exe

Lo encontré y lo comparto con ustedes.


En un modulo bas

Código:
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", "WindowControlerCras h", 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
En el Form con 3 Botones

Código:
Option Explicit

Private Sub Form_Load()
    If Command$ <> "" Then Me.Caption = "Aplicación Reinciada por error: " & Command$
    StarProtect 'comienza la protección
End Sub

Private Sub Form_Unload(Cancel As Integer)
    EndProtect 'Detiene la protección
End Sub


Private Sub Command1_Click()
    MsgBox 1 / 0 'Error Divició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

Fuente: http://foro.elhacker.net
  #3 (permalink)  
Antiguo 16/04/2011, 12:03
Avatar de pkj
pkj
 
Fecha de Ingreso: julio-2006
Ubicación: Órbita sincrónica
Mensajes: 899
Antigüedad: 17 años, 8 meses
Puntos: 29
Respuesta: Interceptar error y reiniciar exe

Un aporte interesante. Se puede adaptar para varias cosas aparte de reiniciar, y se puede hacer que mande datos extra al programa que se ejecuta, aparte del número del error que lo cerró, como el tamaño y posicion del form para abrirlo donde estaba el otro, o datos sobre la función que estaba ejecutandose cuando se cerró.
__________________
No hay preguntas tontas, solo gente estup..., ¡No!, ¿como era? No hay gente que pregunte a tontos... ¡Nooo!... ¡Vaya cabeza!

Etiquetas: exe, reiniciar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 2 personas




La zona horaria es GMT -6. Ahora son las 07:42.