View Full Version : [ Vb.Net ] User Processi - Genymus
Salve, sto progettando un'applicazione per la gestione dei programmi e avrei bisogno di poter maneggiare solo quelli aperti da un solo utente ad esempio mario.
Del tipo:
Marco apre il processo iexplorer.exe
Poi cambia utente lasciando però aperta la sua sessione.
Mario arriava e si logga con il suo user.
Faccio finta che Mario non possa aprire internet explorer perchè io non voglio.
Ecco questo programma deve killare iexplorer.exe di mario lasciando attivo lo stesso processo dalla parte di marco
Come ottengo l'username che stà utilizzando il processo?
Grazie
Genymus
matteo micanti
09-04-2009, 21:15
http://www.dreamincode.net/code/snippet3334.htm
Ciao!
pultroppo da errori e non riesco a farlo funzionare:
Messo così come sta, inserendo anche: Imports System.Management
Public Function KillProcessByUser(ByRef user As String) As Object
Try
'System.Management instances needed
Dim query As New SelectQuery("Win32_Process")
Dim searcher As New ManagementObjectSearcher(query)
Dim observer As New ManagementOperationObserver()
'loop through each item in the collection
For Each process As ManagementObject In searcher.Get()
Try
'this string will hold the information returned
'from InvokeMethod("GetOwner")
Dim info(1) As String
'get the information on the current process
process.InvokeMethod("GetOwner", CType(info, Object()))
'now make sure the owner is correct
If info(0).ToString().ToUpper() = user.ToUpper() Then
'kill the process
process.InvokeMethod(observer, "Terminate", Nothing)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return False
End Try
Next
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return False
End Try
Return True
End Function
Dà questi errori:
[> Tipo 'SelectQuery' non definito.
[> Tipo 'ManagementObjectSearcher' non definito.
[> Tipo 'ManagementOperationObserver' non definito.
[> Tipo 'ManagementObject' non definito.
Che cosa devo fare???
Grazie
Genymus
matteo micanti
09-04-2009, 22:22
Progetto >> Aggiungi Riferimento >> .NET >> System.Management ;)
Ciao!
Lo ho già fatto mettendo:
<< Imports System.Management >>
Come già segnalato, aggiungi un Rif. allo spacename .Net System.Management, e usa il sistema della query solo per ricavare il nome dello User di ogni Processo. Per tutto il resto ti consiglio di dare un'occhiata alle ottime possibilità offerte dallo spacename System.Diagnostics e System.Diagnostics.Process.
Ad esempio, io risolverei così :
1. Funzione che mi restituisce l'Owner di un Process ( uso di System.Management ) :
Private Function GetUserProcesso(ByVal P As Process) As String
Dim Q As New System.Management.ObjectQuery("SELECT * FROM Win32_Process Where ProcessID = '" & P.Id & "'")
Dim OS As New System.Management.ManagementObjectSearcher(Q)
If OS.Get.Count = 0 Then Return "N.D."
Dim arrayUsr(1) As String
For Each MO As System.Management.ManagementObject In OS.Get
MO.InvokeMethod("GetOwner", DirectCast(arrayUsr, Object()))
Next
Return arrayUsr(0)
End Function
2. Popolo una ListBox ( lst_processi ) , con Pid, nome e owner di ogni processo :
Dim idProcesso As Integer
Dim nomeProcesso As String
Dim utenteProcesso As String
Dim P As System.Diagnostics.Process
For Each P In System.Diagnostics.Process.GetProcesses()
idProcesso = P.Id
nomeProcesso = P.ProcessName
utenteProcesso = GetUserProcesso(P)
lst_processi.Items.Add(idProcesso & " / " & nomeProcesso & " / " & utenteProcesso)
Next
'Ordino alfabeticamente
lst_processi.Sorted = True
Il solo motivo di ciò è che per qualche oscuro motivo un System.Diagnostics.Process non ha un .Owner o un .getOwner(), altrimenti farei tutto da System.Diagnostics... ;)
Funziona... come fare senza di voi.
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.