Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: byway en 20 Julio 2009, 18:40 pm



Título: Como mostrar y/o maximizar una instancia previa del mismo exe? (solucionado)
Publicado por: byway en 20 Julio 2009, 18:40 pm
bueno suena simple pero no he podido dar con la solución a esto.. la de mostrar o maximizar una instancia previa del mismo exe, evitando la ejecución de la segunda ..

para evitar ejecución del mismo exe 2 veces uso la función api CreateMutex :

Código
  1. Option Explicit
  2.  
  3. 'Code by Adam Verwijs
  4. Const ERROR_ALREADY_EXISTS = 183&
  5. Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As Any, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
  6. Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
  7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  8.  
  9. 'Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
  10. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  12. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  13.  
  14. Const SW_NORMAL = 1
  15.  
  16. Dim ventana As Long
  17.  
  18. Private Sub Form_Load()
  19.    Dim hMutex As Long
  20.    'Try to create a new Mutex
  21.    hMutex = CreateMutex(ByVal 0&, 1, App.Title)
  22.    'Did the mutex already exist?
  23.    If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
  24.        'Clean up
  25.        ReleaseMutex hMutex
  26.        CloseHandle hMutex
  27.        'More than one instance detected
  28.        MsgBox "No se puede ejecutar 2 veces lo mismo"
  29.        Hide
  30.        'busco la ventana con el mismo nombre de la instancia previa
  31.        ventana = FindWindow(vbNullString, Me.Caption)
  32.        'llamo al hwnd de la ventana encontrada
  33.        Call SetForegroundWindow(ventana)
  34.        'muestro ala ventana mediante su hwnd
  35.        Call ShowWindow(ventana, SW_NORMAL)
  36.        End
  37.  
  38.    End If
  39. End Sub
  40.  
  41.  

ayuda y sugerencias.. seran bienvenidas..  ;D

Edit: solucionado.
Código
  1. Option Explicit
  2.  
  3. 'Code by Adam Verwijs
  4. Const ERROR_ALREADY_EXISTS = 183&
  5. Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As Any, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
  6. Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
  7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  8.  
  9. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  10. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  11. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  12.  
  13. Const SW_NORMAL = 1
  14.  
  15. Dim ventana As Long
  16.  
  17. Private Sub Form_Load()
  18.  
  19.    Dim hMutex As Long
  20.    Me.Caption = "INSTANCIA 1"
  21.    'Try to create a new Mutex
  22.    hMutex = CreateMutex(ByVal 0&, 1, App.Title)
  23.    'Did the mutex already exist?
  24.    If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
  25.        'Clean up
  26.        ReleaseMutex hMutex
  27.        CloseHandle hMutex
  28.        Me.Caption = "INSTANCIA 2"
  29.        Hide
  30.        'busco la ventana con el mismo nombre de la instancia previa
  31.        ventana = FindWindow(vbNullString, "INSTANCIA 1")
  32.        'llamo al hwnd de la ventana encontrada
  33.        Call SetForegroundWindow(ventana)
  34.        'muestro ala ventana mediante su hwnd
  35.        Call ShowWindow(ventana, SW_NORMAL)
  36.        End
  37.    End If
  38.  
  39. End Sub
  40.  


Saludos.


Título: Re: Como mostrar y/o maximizar una instancia previa del mismo exe?
Publicado por: Dessa en 20 Julio 2009, 19:08 pm
Algo así ?

Código:

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Const SW_NORMAL = 1



Private Sub Form_Load()

Me.Caption = "INSTANCIA 1"

If App.PrevInstance Then
Me.Caption = "INSTANCIA 2"
MsgBox "NO", , Me.Caption

 If IsIconic(FindWindow(vbNullString, "INSTANCIA 1")) = 0 Then
    MsgBox "no esta minimizado"
    Call SetForegroundWindow(FindWindow(vbNullString, "INSTANCIA 1"))
 Else
    MsgBox "esta minimizado"
    Call ShowWindow(FindWindow(vbNullString, "INSTANCIA 1"), SW_NORMAL)
    Call SetForegroundWindow(FindWindow(vbNullString, "INSTANCIA 1"))
 End If
End
End If


End Sub



S2



Título: Re: Como mostrar y/o maximizar una instancia previa del mismo exe?
Publicado por: BlackZeroX en 20 Julio 2009, 19:22 pm
Creo que haci deberia ser no?

Código
  1. Option Explicit
  2.  
  3. 'Code by Adam Verwijs
  4. Const ERROR_ALREADY_EXISTS = 183&
  5. Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As Any, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
  6. Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
  7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  8.  
  9. 'Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
  10. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  12. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  13.  
  14. Const SW_NORMAL = 1
  15.  
  16. Dim ventana As Long
  17.  
  18. Private Sub Form_Load()
  19.    Hide
  20.    Dim hMutex As Long
  21.    Dim Caption2 As String, AntiInstancia As String
  22.    Caption2 = "Hola Mundo"
  23.    Caption = Caption2
  24.    'Try to create a new Mutex
  25.    AntiInstancia = "Mutexrecor"
  26.    hMutex = CreateMutex(ByVal 0&, 1, AntiInstancia)
  27.    'Did the mutex already exist?
  28.    If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
  29.        'Clean up
  30.        ReleaseMutex hMutex
  31.        CloseHandle hMutex
  32.        'More than one instance detected
  33.        'busco la ventana con el mismo nombre de la instancia previa
  34.        caption="Error: Doble Instancia"
  35.        ventana = FindWindow(vbNullString, Caption2)
  36.        'llamo al hwnd de la ventana encontrada
  37.        Call SetForegroundWindow(ventana)
  38.        'muestro ala ventana mediante su hwnd
  39.        Call ShowWindow(ventana, SW_NORMAL)
  40.        End
  41.    End If
  42.    Show
  43. End Sub
  44.  
  45.  


Título: Re: Como mostrar y/o maximizar una instancia previa del mismo exe?
Publicado por: Karcrack en 20 Julio 2009, 19:24 pm
Código
  1. Option Explicit
  2.  
  3. 'Code by Adam Verwijs
  4. Const ERROR_ALREADY_EXISTS = 183&
  5. Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As Any, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
  6. Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
  7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  8.  
  9. 'Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
  10. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  12. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  13.  
  14. Const SW_NORMAL = 1
  15.  
  16. Dim ventana As Long
  17.  
  18. Private Sub Form_Initialize()
  19.    Dim hMutex As Long
  20.    'Try to create a new Mutex
  21.    hMutex = CreateMutex(ByVal 0&, 1, App.Title)
  22.    'Did the mutex already exist?
  23.    If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
  24.        'Clean up
  25.        ReleaseMutex hMutex
  26.        CloseHandle hMutex
  27.        'More than one instance detected
  28.        MsgBox "No se puede ejecutar 2 veces lo mismo"
  29.        'Hide
  30.        'busco la ventana con el mismo nombre de la instancia previa
  31.        ventana = FindWindow(vbNullString, Me.Caption)
  32.        'llamo al hwnd de la ventana encontrada
  33.        Call SetForegroundWindow(ventana)
  34.        'muestro ala ventana mediante su hwnd
  35.        Call ShowWindow(ventana, SW_NORMAL)
  36.        End
  37.  
  38.    End If
  39. End Sub

El código ha de ejecutarse antes de que haya dos ventanas con el mismo titulo... sino FindWindow falla ;D

Si no recuerdo mal Initilize se llama antes que crear la ventan :silbar:


Título: Re: Como mostrar y/o maximizar una instancia previa del mismo exe?
Publicado por: byway en 20 Julio 2009, 19:40 pm
gracias por sus respuestas, lo del artificio de cambiarle el nombre a la ventana de la segunda instancia me lo habian comentado antes, pero ahora ya me lo confirmaron y es mas hasta adecuaron el code..

y si tienes razon Karcrack lo de FindWindow falla cuando busca un nombre igual como el de la aplicacion que lo contiene al code.

Edit: lo de Initialize tampoco da .. hasta intente con sub main con el mismo resultado.

Saludos.