Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

PLEIN D'OPÉRATIONS SIMPLE POUR GÉRER LES PROCESS


Information sur la source

Catégorie :API Classé sous : process Niveau : Débutant Date de création : 20/08/2003 Date de mise à jour : 20/08/2003 21:17:04 Vu : 5 652

Note :
7,33 / 10 - par 3 personnes
7,33 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (14)
Ajouter un commentaire et/ou une note

Description

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

  • 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
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
 

Commentaires et avis

signaler à un administrateur
Commentaire de TigerFab le 21/08/2003 23:04:10

Salut,

Intéressantes ces fonctions, mais quelle est la valeur de PROCESS_TERMINATE ? Tu ne l'as pas déclaré.

A+

signaler à un administrateur
Commentaire de gabchampagne le 23/08/2003 06:09:02

je me souviens plus
j'ai créer ça a partir de bouts de code
en t cas, ca marche. je pense que cette valeur est optionnelle

signaler à un administrateur
Commentaire de TigerFab le 24/08/2003 17:23:39

Zut, car j'ai mis plusieurs valeur, il se passe rien, la fenêtre n'est pas tuée. Ca doit être une valeur hexa .. mais laquelle....

signaler à un administrateur
Commentaire de dedebatou le 27/08/2003 17:41:36

Bonjour,

Je veux pouvoir arrêter un exécutable à partir d'un programme VB,
je pense que la fonction "Fermer_app" que tu propose est la solution à mon problème. Mais je n'arrive pas à la faire fonctionner car la variable "handle" utilisée dans la fonction vaut toujours "0".
La chaîne de chacartères "Titre" est bien le nom de l'exécutable à fermer avec son arborescence complète ?

Par avance, merci de ton aide.

signaler à un administrateur
Commentaire de gabchampagne le 28/08/2003 04:48:39

ca dépend de quelle application tu tente de fermer

signaler à un administrateur
Commentaire de gabchampagne le 28/08/2003 04:49:22

Si handle vaut 0 alors tu n'a pas le bon titre. cherche dans la liste des process

signaler à un administrateur
Commentaire de dedebatou le 28/08/2003 10:16:02

Es-tu sûr que ton code fonctionne sous Windows NT ?
Parce que j'ai essayer d'affecter à "handle" le n° d'ouverture du fichier directement et ça ne parche pas quand même.
L'application à fermer est un exécutable Vb, la racine est sous C:, j'ai donc essayé comme titres "test.exe", "C:test.exe", "test" ......
Là je comprend pas...

signaler à un administrateur
Commentaire de gabchampagne le 03/09/2003 23:41:58

c parce que il faut le titre du process: celui qui est dans la barre en haut de la fenetre

signaler à un administrateur
Commentaire de heine le 16/01/2004 16:27:16

un zip ca orai été cool ;) ca permet de voir rapidement l'integrité du code m'enfin bon... je vais tester ce code... ensuite je noterai ;)

signaler à un administrateur
Commentaire de heine le 16/01/2004 17:08:16

bon ben on va dire que c'est sympa mais basic.. aller 5/10 ;)

Enjoy ;)

signaler à un administrateur
Commentaire de Leirn le 28/01/2004 10:24:05

heine a dit:
bon ben on va dire que c'est sympa mais basic.. aller 5/10 ;)
Enjoy ;)

Il me semble que tu y vas un peu fort la...
Son code est une belle collection de fonctions manipuler les process, et c est exactement ce qu il propose. Et c est peut etre une utilisation basique d API (et encore ca fait une belle présentation de leur utilisation), mais en général c est ce qu on demande à un code classé débutant, d être basic...

Pis basic... excuse moi de devoir critiquer tes sources aussi alors (enfin ta source...)... Un zip pour 10 lignes de code c est un peu du temps perdu, et les afficher directement ca permet d eviter de telecharger des programmes trop basiques et inutiles... Ya pas besoin dun zip pour comprendre un code... Une fois lu je sais pas ce qu il y a a tester.

Enfin bref, la prochaine fois reste couché au lieu de dire des conneries,


gabchampagne: 7 pour ton code a cause de l absence de commentaire, sinon c est pas mal

signaler à un administrateur
Commentaire de PaTaTe le 25/04/2005 02:44:21

Bonne source !

Cependant faire dépendre FSO pour une fonction ki pourrait s'en passer ... j'evite FSO des que je peux.

Voilà la version sans FSO de la sub  ouvrirexp()

Sub OuvrirExp()
Dim Chemin As String
Chemin = Environ("WinDir")
If Right(Chemin, 1) <> "\" Then Chemin = Chemin & "\"
Shell Chemin & "explorer.exe"
End Sub


Pour TigerFab :

Je pense que la constante que tu cherches  doit être :

Const PROCESS_TERMINATE = &H1

@++

7/10

signaler à un administrateur
Commentaire de thibaud28 le 23/03/2006 08:32:17

Comment on obtient la liste des process?

signaler à un administrateur
Commentaire de ratala le 04/07/2006 16:56:21

je vois pas le "retourner le hwnd d'un programme"
est-ce que sa retourne le hwnd par exemple si je lui dit "explorer.exe" ?

Ajouter un commentaire

Discussions en rapport avec ce code source dans le forum

Problème de process [ par Xentor ] Bonjour,Je voudrais savoir s'il est possible d'obtenir la liste des adresses mémoires allouées à un process... HELP / liste des process sur un serveur [ par keopsk ] Bonjour,Comment peut on faire pour lire les process sur un serveur sans passer par WMI ?Merci d'avance Tuer un process [ par nicobou ] Bonjour,je cherche à tuer un process précis à partir de son PID. J'arrive à récupérer la liste des process qui tournent, récupérer leur ID, mais je ne Lancer un process avec des droits particuliers [ par cyrilp ] Bonjour,J'aimerai savoir comment faire avec les API windows (NT) pour lancer une application sous un compte particulier.Par exemple, je suis connecté Récupération & kill process en VB [ par 25230 ] Je souhaite pouvoir exécuter une commande, en récupérerle handle de process, pour pouvoir par la suite killer ce process.Je pourrais me contenter de k Kill Process Sous Win2k d'une app lancée avec shell() [ par soulheaven ] voilà je cherches à fermer une application que g lancé avec shell()!!!j'utilises la fonction SendMessage mais ça marche pas sous win2k!! il faudrait q


Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Téléchargements

Logiciels à télécharger sur le même thème :

Comparez les prix Nouvelle version


HTC Magic

Entre 429€ et 429€


Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,437 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.