USUÁRIO:      SENHA:        SALVAR LOGIN ?    Adicione o VBWEB na sua lista de favoritos   Fale conosco 

 

  Fórum

  Visual Basic
Voltar
Autor Assunto:  Gerenciador de Tarefas
Bruno
SÃO PAULO
SP - BRASIL
ENUNCIADA !
Postada em 18/08/2005 10:11 hs            
Pessoal,
 
Alguém sabe se é possível eu fechar programas ou processos que estão na memória pelo VB 6????
Igual ao Ctrl+Alt+Del e finalizar tarefa, mas seria pelo vb, teria como???
 
Obrigado!Emoções
 
   
kerplunk
Pontos: 2843 Pontos: 2843 Pontos: 2843
SÃO PAULO
SP - BRASIL
ENUNCIADA !
Postada em 18/08/2005 10:13 hs         
Tem como, mas não é muito seguro.
   
Mr.Data
SÃO PAULO
SP - BRASIL
ENUNCIADA !
Postada em 18/08/2005 14:05 hs            
Basicamente, vc precisa de 2 coisas: achar os processos (ou seja, os Handles deles) e depois mandar fechar o desejado.
 
Para mandar fechar uma aplicação, é relativamente simples: Ponha o código abaixo em um módulo, e é só chamar a função passado o Hwnd (Handle) da aplicação que você quer derrubar:
 
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Function KillApp(lngHwnd as Long) as Boolean
    On Local Error Resume Next
    Dim lngResult As Long
    Const WM_CLOSE = &H10
    lngResult = PostMessage(lngHwnd, WM_CLOSE, 0&, 0&)
    KillApp = Iif(lngResult = 1, True, False)
    On Local Error Goto 0
End Sub
Para chamá-la:
 
If KillApp(lngHwnd) Then
    Msgbox "Aplicativo fechado"
Else
   Msgbox "Falha ao fechar aplicativo"
End If
 
Para achar o Handle, basta usar as API FindWindow. Há também como puxar tudo que está aberto (inclusive as janelas ocultas, usando as API's GetWindow, GetWindowText e GetClassName (esta última só prá levantar de que classe é a janela, não importa muito ao processo). O exemplo abaixo ilustra isso. Para ver ele funcionando, crie um Form e insira nele um ListView. Nomeie este ListView para lvwBig.
 
Primeiramente, no módulo descrito acima, insira o seguinte:
 
' GetWindow() Constants
Public Const GW_HWNDNEXT = 2
'API Declaration
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Então insira a função abaixo no Form e chame-a pelo Form_Load, num Timer, ou onde você preferir.
 
Private Sub GetApps()
    Dim strResult As String * 128
    Dim lngHwnd As Long
    Dim lngHwnd1 As Long
    Dim lstItem As ListItem
    Dim strClassName As String 'Variable to return the Class Name of Window
    Dim strWindowText As String 'Variable to return the Window Text
   With lvwBig
        .ListItems.Clear
        .ColumnHeaders.Clear
        .ColumnHeaders.Add , , "Window Title", 4000, lvwColumnLeft
        .ColumnHeaders.Add , , "Handle", 1000, lvwColumnCenter
        .ColumnHeaders.Add , , "Class", 3000, lvwColumnLeft
    End With
   
    'Set lngHwnd Min value to the handle's window of the application ...
    lngHwnd = Me.hwnd
   
    Do Until lngHwnd = 0
        'Get window text of the actual window ...
        GetWindowText lngHwnd, strResult, Len(strResult)
        strWindowText = Trim(Mid(strResult, 1, InStr(1, strResult, vbNullChar, vbTextCompare) - 1))
        If InStr(1, strWindowText, Me.Caption, vbTextCompare) = 0 And Trim(strWindowText) <> "" Then
            'Get class name of the actual window ...
            GetClassName lngHwnd, strResult, Len(strResult)
            strClassName = Mid(strResult, 1, InStr(1, strResult, vbNullChar, vbTextCompare) - 1)
            Set lstItem = lvwBig.ListItems.Add(, , strWindowText, , 1)
            lstItem.SubItems(1) = Trim(Str(lngHwnd))
            lstItem.SubItems(2) = strClassName
        End If
        lngHwnd1 = GetWindow(lngHwnd, GW_HWNDNEXT)
        lngHwnd = lngHwnd1
    Loop
End Sub
Então perceba que o lvwBig.SelectedItem.SubItems(1) é o que contém o Handle. Passando ele com o CLng() para a funçãozinha lá em cima (KillApp), ele tentará postar uma mensagem para o aplicativo para quel ele se feche. Pode ser que o aplicativo tenha caixas de diálogo (p.ex., o Outlook), e isso impeça o fechamento. Não testei muito este programa, fiz ele há um bom tempo e deu um trabalhão danado achar ele de novo, hehehe. Enfim, divirta-se ajustando-o as suas necessidades e poste os resultados por aqui. Emoções
 

[ ]'s

-----------------------------------------------------------------------------------------------------------------

Emoções Juliano Lopes - Coordenador de Projetos / Arquiteto de Software Emoções

   
Bruno
SÃO PAULO
SP - BRASIL
ENUNCIADA !
Postada em 18/08/2005 14:13 hs            
Gente,
Valeu pelas Dicas!!! Vou testar aqui!!!!
 
Obrigado!!!!!!!!Emoções
   
Página(s): 1/1    


Seu Nome:

Seu eMail:

ALTERAR PARA MODO HTML
Mensagem:

[:)] = 
[:P] = 
[:(] = 
[;)] = 

HTML DESLIGADO

     
 VOLTAR

  



CyberWEB Network Ltda.    © Copyright 2000-2024   -   Todos os direitos reservados.
Powered by HostingZone - A melhor hospedagem para seu site
Topo da página