Plein d'opérations simple pour gérer les process

Contenu du snippet

voici ce que sa permet de faire:
voire si une certaine app est ouverte
retourner le hwnd d'un programme
changer le titre d'une fenetre
obtenir la liste des process
killer des process (+ sécurité pour explorer)
retourner le titre d'un handle
(mettez sa dans un module)

Source / Exemple :


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Const Fermer = &H10
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Public Function GetHandle(Titre As String) As Long
GetHandle = FindWindow(vbNullString, Titre)
End Function

Public Sub SetTitle(hwnd As Long, texte As String)
SetWindowText hwnd, texte
End Sub
Public Function IsAppRunning(Titre As String) As Boolean
Dim handle
handle = FindWindow(vbNullString, Titre)
If handle = 0 Then IsAppRunning = False Else: IsAppRunning = True
End Function
Public Function Fermer_app(Titre As String) As Boolean
Dim handle As Long, thread As Long, proc_handle As Long
handle = FindWindow(vbNullString, Titre)
If handle = 0 Then Fermer_app = False: Exit Function
GetWindowThreadProcessId handle, thread
proc_handle = OpenProcess(PROCESS_TERMINATE, False, thread)
TerminateProcess proc_handle, 4
Fermer_app = True
reloader_explorer
End Function
Public Function GetProcessTitle(hwnd As Long)
  Dim Str As String
    Str = String$(GetWindowTextLength(hwnd) + 1, Chr$(0))
    GetWindowText hwnd, Str, Len(Str)
    GetProcessTitle = Str
End Function

'pas de moi (mais je l'ai amélioré) :
Public Function listedestaches(frm As Form) As String
On Error Resume Next
Dim CurrWnd As Long
Dim Length As Long
Dim NomTache As String
Dim Parent As Long
Dim NbListe As String
NbListe = 0
CurrWnd = GetWindow(frm.hwnd, GW_HWNDFIRST)
While CurrWnd <> 0
    Parent = GetParent(CurrWnd)
    Length = GetWindowTextLength(CurrWnd)
    NomTache = Space$(Length + 1)
    Length = GetWindowText(CurrWnd, NomTache, Length + 1)
    NomTache = Left$(NomTache, Len(NomTache) - 1)
    If Length <> 0 Then
        If NomTache <> Form1.Caption And NomTache <> "" Then
            If IsWindowVisible(CurrWnd) Then
                listedestaches = listedestaches & NomTache & vbCrLf
            End If
        End If
    End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend
End Function
'SÉCURITÉ CAR SI ON FERME UN DOSSIER, EXPLORER SE FERME ;-)
Sub reloader_explorer()
On Error Resume Next
Dim CurrWnd As Long
Dim Length As Long
Dim NomTache As String
Dim Parent As Long
Dim NbListe As String
Dim listedestaches
NbListe = 0
listedestaches = ""
CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
While CurrWnd <> 0
    Parent = GetParent(CurrWnd)
    Length = GetWindowTextLength(CurrWnd)
    NomTache = Space$(Length + 1)
    Length = GetWindowText(CurrWnd, NomTache, Length + 1)
    NomTache = Left$(NomTache, Len(NomTache) - 1)
    If Length <> 0 Then
        If NomTache <> Form1.Caption And NomTache <> "" Then
            If IsWindowVisible(CurrWnd) Then
                listedestaches = listedestaches & NomTache & vbCrLf
            End If
        End If
    End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend
If InStr(1, listedestaches, "Program Manager") <= 1 Then
ouvrirexp
End If
End Sub
Sub ouvrirexp()
Dim fso As FileSystemObject, fold
Set fso = New FileSystemObject
fold = fso.GetSpecialFolder(WindowsFolder)
If Right(fold, 1) = "\" Then
Else
fold = fold & "\"
End If
Shell fold & "explorer.exe"
End Sub

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.