RÉCUPÉRER LE NOM D'UTILISATEUR CORRESPONDANT À UN PROCESSUS

nihonsx5 Messages postés 20 Date d'inscription samedi 21 décembre 2002 Statut Membre Dernière intervention 16 février 2014 - 14 juil. 2006 à 22:59
draluorg Messages postés 625 Date d'inscription vendredi 23 avril 2004 Statut Membre Dernière intervention 25 novembre 2010 - 15 juil. 2006 à 12:48
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/38561-recuperer-le-nom-d-utilisateur-correspondant-a-un-processus

draluorg Messages postés 625 Date d'inscription vendredi 23 avril 2004 Statut Membre Dernière intervention 25 novembre 2010
15 juil. 2006 à 12:48
Salut a tous,

Joli code MadM@tt, perso je conseillerais plutot d'utiliser ta methode que WMi, pas tant pour les performances, mais parceque Wmi peut etre desactiver sur la machine et dans ce cas ca ne marchera pas...

Bonne prog a tous
++
MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 1
15 juil. 2006 à 01:14
C'est vrai que le textbox ralenti beaucoup. J'essayerai dans les jours qui vienne de faire un petit bench au niveau de toutes les infos que je peux tirer grace à ta méthode, histoire de garder la plus rapide (meme sur d'autres infos sur les process).
Merci en tout cas pour ces supers infos et méthodes
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
15 juil. 2006 à 01:09
c'est l'affichage en txt qui prend du temps.
je viens de tester sur mon PC qui a déjà un sacré coup dans l'aile : environ 3 secondes en passant pas une string (affichée à la fin).

et pour les infos principales...., quasi instantané :



Dim sBuffer As String
sBuffer = "===================================================="
Text1.FontName "Courier New": Text1.FontSize 8


On Local Error GoTo Err_Handler
Dim objWMIService As Object, WMI_ObjProps As Object, ObjClsItem As Object

' on ouvre le service
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}" & _
"!\" & Environ$("COMPUTERNAME") & "\root\cimv2")
' recherche
Set WMI_ObjProps = objWMIService.ExecQuery("Select * from Win32_Process", , 48)

' lecture
For Each ObjClsItem In WMI_ObjProps
sBuffer = sBuffer & vbCrLf & "Caption: " & ObjClsItem.Caption
sBuffer = sBuffer & vbCrLf & "S_Id: " & ObjClsItem.SessionId & "P_Id: " & ObjClsItem.ProcessId
sBuffer = sBuffer & vbCrLf & "WorkingSetSize: " & ObjClsItem.WorkingSetSize \ 1024 & "Ko"
sBuffer = sBuffer & vbCrLf & "====================================================" & vbCrLf
Next
Text1.Text = sBuffer
MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 1
15 juil. 2006 à 00:46
PFFFF Immense respect rolala :'(
ouin pourquoi moaaaaa....
Par contre c'est super lent comme méthode non ??? Enfin chez moi (pc pas mal rapide) j'ai bien attendu une 10aine de seconde.

Enfin je t'explique, à la base j'avais besoin absolument de ce code pour récupérer la conso CPU de chaque processus. J'y suis enfin arrivé, et en fait je viens de réaliser que je n'ai pas besoin du tout de ce code en fait, donc tout ce boulot pour rien. Et de toute façon vu comment la méthode que tu propose est lente, je n'aurais pas pu l'utiliser non plus (et oui conso CPU en temps réel, pour chaque process, ça risque d'etre difficile d'attendre 10s entre chaque refresh lol).

Enfin merci beaucoup pour m'avoir apporté ce petit bout de connaissance, mais je suis vraiment désolé,mais je ne m'ouvrirai pas les veines ce soir... je viens juste de réussir à obtenir cette ***** de conso CPU, depuis des année que je la voulai, pas un code sur vbfrance, pas un post dans le forum.... le néant
Pfff enfin c'est le bonheur dans ma chambre la, wouhou !
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
15 juil. 2006 à 00:15
re,
apparemment il faut ensuite aller lire Win32_ComputerSystem (avec les ID récupérés...)

bon en attendant, place une txtbox sur une form, multilignes avec bothscroll, puis copie ce code, F5, et enfin sort ton cutter :p


Private Sub Form_Load()
'Me.AutoRedraw = True
Text1.Text = ""
Text1.FontName = "Courier New"
Text1.FontSize = 8

On Local Error GoTo Err_Handler
Dim objWMIService As Object, WMI_ObjProps As Object, ObjClsItem As Object

' on ouvre le service
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}" & _
"!\" & Environ$("COMPUTERNAME") & "\root\cimv2")
' recherche
Set WMI_ObjProps = objWMIService.ExecQuery("Select * from Win32_Process", , 48)

' lecture
For Each ObjClsItem In WMI_ObjProps
'Me.Print ObjClsItem.SessionId & vbTab & "[" & ObjClsItem.ProcessId & "]" & vbTab & vbTab & ObjClsItem.Name
Text1.Text = Text1.Text & "===================================================="
Text1.Text = Text1.Text & vbCrLf & "Caption: " & ObjClsItem.Caption
Text1.Text = Text1.Text & vbCrLf & "CommandLine: " & ObjClsItem.CommandLine
Text1.Text = Text1.Text & vbCrLf & "CreationClassName: " & ObjClsItem.CreationClassName
Text1.Text = Text1.Text & vbCrLf & "CreationDate: " & ObjClsItem.CreationDate
Text1.Text = Text1.Text & vbCrLf & "CSCreationClassName: " & ObjClsItem.CSCreationClassName
Text1.Text = Text1.Text & vbCrLf & "CSName: " & ObjClsItem.CSName
Text1.Text = Text1.Text & vbCrLf & "Description: " & ObjClsItem.Description
Text1.Text = Text1.Text & vbCrLf & "ExecutablePath: " & ObjClsItem.ExecutablePath
Text1.Text = Text1.Text & vbCrLf & "ExecutionState: " & ObjClsItem.ExecutionState
Text1.Text = Text1.Text & vbCrLf & "Handle: " & ObjClsItem.Handle
Text1.Text = Text1.Text & vbCrLf & "HandleCount: " & ObjClsItem.HandleCount
Text1.Text = Text1.Text & vbCrLf & "InstallDate: " & ObjClsItem.InstallDate
Text1.Text = Text1.Text & vbCrLf & "KernelModeTime: " & ObjClsItem.KernelModeTime
Text1.Text = Text1.Text & vbCrLf & "MaximumWorkingSetSize: " & ObjClsItem.MaximumWorkingSetSize
Text1.Text = Text1.Text & vbCrLf & "MinimumWorkingSetSize: " & ObjClsItem.MinimumWorkingSetSize
Text1.Text = Text1.Text & vbCrLf & "Name: " & ObjClsItem.Name
Text1.Text = Text1.Text & vbCrLf & "OSCreationClassName: " & ObjClsItem.OSCreationClassName
Text1.Text = Text1.Text & vbCrLf & "OSName: " & ObjClsItem.OSName
Text1.Text = Text1.Text & vbCrLf & "OtherOperationCount: " & ObjClsItem.OtherOperationCount
Text1.Text = Text1.Text & vbCrLf & "OtherTransferCount: " & ObjClsItem.OtherTransferCount
Text1.Text = Text1.Text & vbCrLf & "PageFaults: " & ObjClsItem.PageFaults
Text1.Text = Text1.Text & vbCrLf & "PageFileUsage: " & ObjClsItem.PageFileUsage
Text1.Text = Text1.Text & vbCrLf & "ParentProcessId: " & ObjClsItem.ParentProcessId
Text1.Text = Text1.Text & vbCrLf & "PeakPageFileUsage: " & ObjClsItem.PeakPageFileUsage
Text1.Text = Text1.Text & vbCrLf & "PeakVirtualSize: " & ObjClsItem.PeakVirtualSize
Text1.Text = Text1.Text & vbCrLf & "PeakWorkingSetSize: " & ObjClsItem.PeakWorkingSetSize
Text1.Text = Text1.Text & vbCrLf & "Priority: " & ObjClsItem.Priority
Text1.Text = Text1.Text & vbCrLf & "PrivatePageCount: " & ObjClsItem.PrivatePageCount
Text1.Text = Text1.Text & vbCrLf & "ProcessId: " & ObjClsItem.ProcessId
Text1.Text = Text1.Text & vbCrLf & "QuotaNonPagedPoolUsage: " & ObjClsItem.QuotaNonPagedPoolUsage
Text1.Text = Text1.Text & vbCrLf & "QuotaPagedPoolUsage: " & ObjClsItem.QuotaPagedPoolUsage
Text1.Text = Text1.Text & vbCrLf & "QuotaPeakNonPagedPoolUsage: " & ObjClsItem.QuotaPeakNonPagedPoolUsage
Text1.Text = Text1.Text & vbCrLf & "QuotaPeakPagedPoolUsage: " & ObjClsItem.QuotaPeakPagedPoolUsage
Text1.Text = Text1.Text & vbCrLf & "ReadOperationCount: " & ObjClsItem.ReadOperationCount
Text1.Text = Text1.Text & vbCrLf & "ReadTransferCount: " & ObjClsItem.ReadTransferCount
Text1.Text = Text1.Text & vbCrLf & "SessionId: " & ObjClsItem.SessionId
Text1.Text = Text1.Text & vbCrLf & "Status: " & ObjClsItem.Status
Text1.Text = Text1.Text & vbCrLf & "TerminationDate: " & ObjClsItem.TerminationDate
Text1.Text = Text1.Text & vbCrLf & "ThreadCount: " & ObjClsItem.ThreadCount
Text1.Text = Text1.Text & vbCrLf & "UserModeTime: " & ObjClsItem.UserModeTime
Text1.Text = Text1.Text & vbCrLf & "VirtualSize: " & ObjClsItem.VirtualSize
Text1.Text = Text1.Text & vbCrLf & "WindowsVersion: " & ObjClsItem.WindowsVersion
Text1.Text = Text1.Text & vbCrLf & "WorkingSetSize: " & ObjClsItem.WorkingSetSize
Text1.Text = Text1.Text & vbCrLf & "WriteOperationCount: " & ObjClsItem.WriteOperationCount
Text1.Text = Text1.Text & vbCrLf & "WriteTransferCount: " & ObjClsItem.WriteTransferCount
Text1.Text = Text1.Text & vbCrLf & "====================================================" & vbCrLf & vbCrLf & vbCrLf
Next

Err_Handler:
Set ObjClsItem = Nothing
Set WMI_ObjProps = Nothing
Set objWMIService = Nothing

If Err.Number <> 0 Then Err.Clear
End Sub
'
Private Sub Form_Resize()
Text1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub


paix à ton âme ^^
++
MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 1
14 juil. 2006 à 23:52
PCPT > Merci, ça m'interesserai s'il serai possible de récupérer le nom d'utilisateur, car en effet ça réduit de beaucoup la taille du code. A voir...
En tout cas si quelqu'un trouve, permettez moi de m'ouvrir les veines un ptit peu quand meme, paske la j'ai les api qui me sortent par les oreilles lol, j'ai trop galéré pour trouver ce truc à 2 francs (mais ça me servira dans qqch de bien mieux).
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
14 juil. 2006 à 23:47
salut,


en VB6 çà donnerait çà :


Private Sub Form_Load()
Me.AutoRedraw = True

On Local Error GoTo Err_Handler
Dim objWMIService As Object, WMI_ObjProps As Object, ObjClsItem As Object

' on ouvre le service
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}" & _
"!\" & Environ$("COMPUTERNAME") & "\root\cimv2")
' recherche
Set WMI_ObjProps = objWMIService.ExecQuery("Select * from Win32_Process", , 48)

' lecture
For Each ObjClsItem In WMI_ObjProps
Me.Print ObjClsItem.SessionId & vbTab & "[" & ObjClsItem.ProcessId & "]" & vbTab & vbTab & ObjClsItem.Name
Next

Err_Handler:
Set ObjClsItem = Nothing
Set WMI_ObjProps = Nothing
Set objWMIService = Nothing

If Err.Number <> 0 Then Err.Clear
End Sub


seulement on a pas le nom de l'utilisateur (ni dans ce code ni dans celui de nihonsx5)
faudrait chercher...

en tout cas le code semble propre ;)
++
PCPT [AFCK]
MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 1
14 juil. 2006 à 23:14
Salut Nihonsx,
J'ai été bluffé par le code que tu as mis, c'est vrai que c'est très simple. Seulement c'est du VB.net, et il s'agit d'une source VB.
J'ai quand meme jeté un oeil, mais je n'ai pas trouvé dans les références le System.Management, ou Management tout seul. Je n'y connais vraiment rien au .net alors j'ai du mal.
nihonsx5 Messages postés 20 Date d'inscription samedi 21 décembre 2002 Statut Membre Dernière intervention 16 février 2014
14 juil. 2006 à 22:59
Salut,
on peut utiliser les WMI pour récupérer toutes ces infos. Je pense que c'est encore plus facile.
Un exemple (que j'ai pris dans WMI Code Creator, c'est du VB.NET) :

Imports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

Public Class MyWMIQuery

Public Overloads Shared Function Main() As Integer

Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_Process")

For Each queryObj As ManagementObject in searcher.Get()

Console.WriteLine("-----------------------------------")
Console.WriteLine("Win32_Process instance")
Console.WriteLine("-----------------------------------")
Console.WriteLine("Name: {0}", queryObj("Name"))
Console.WriteLine("ProcessId: {0}", queryObj("ProcessId"))
Console.WriteLine("SessionId: {0}", queryObj("SessionId"))
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Function
End Class
End Namespace


Voila, si ça peut aider. Bon courage !
Rejoignez-nous