Private Const PROCESS_READ_CONTROL As Long = &H20000
Private Const TokenUser As Long = 1
Private Const TokenGroups As Long = 2
Private Type SID_IDENTIFIER_AUTHORITY
Value(6) As Byte
End Type
Private Type SID_AND_ATTRIBUTES
Sid As Long
Attributes As Long
End Type
Private Type TOKEN_GROUPS
GroupCount As Long
Groups(500) As SID_AND_ATTRIBUTES
End Type
Private Declare Function GetSecurityInfo Lib "advapi32.dll" (ByVal hObject As Long, ByVal ObjectType As Long, ByVal SecurityInformation As Long, ppsidOwner As Long, ppsidGroup As Long, ppDacl As Long, ppSacl As Long, ppSecurityDescriptor As Long) As Long
Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias "LookupAccountSidA" (ByVal lpSystemName As String, ByVal Sid As Long, ByVal name As String, cbName As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const SE_KERNEL_OBJECT As Long = 6
Private Const OWNER_SECURITY_INFORMATION As Long = 1
Private Const GROUP_SECURITY_INFORMATION As Long = 2
'Private Const PROCESS_READ_CONTROL As Long = &H20000
Public Function GetProcUserNameA(ByVal pid As Long) As String
Dim hToken As Long
Dim hProcess As Long
Dim cbBuff As Long
Dim TG As TOKEN_GROUPS
Dim UserName As String
Dim DomainName As String
Dim UserNameLenght As Long
Dim DomainNameLenght As Long
Dim peUse As Long
Dim ppsidGroup As Long
hProcess = OpenProcess(&H400 Or 16&, 0, pid)
If hProcess Then
If OpenProcessToken(hProcess, &H8, hToken) Then
CloseHandle hProcess
GetTokenInformation hToken, TokenUser, TG, 0, cbBuff
If GetTokenInformation(hToken, TokenUser, TG, cbBuff, 0) Then
CloseHandle hToken
UserNameLenght = 255
UserName = Space$(UserNameLenght)
DomainName = UserName 'Space$(255)
DomainNameLenght = UserNameLenght
LookupAccountSid vbNullString, TG.GroupCount, UserName, UserNameLenght, DomainName, DomainNameLenght, peUse
GetProcUserNameA = Left$(UserName, UserNameLenght)
Exit Function
End If
Else
CloseHandle hProcess
hProcess = OpenProcess(PROCESS_READ_CONTROL, 0, pid)
If hProcess Then
If GetSecurityInfo(hProcess, SE_KERNEL_OBJECT, GROUP_SECURITY_INFORMATION, 0, ppsidGroup, 0, 0, 0) = 0 Then
CloseHandle hProcess
UserNameLenght = 255
UserName = Space$(UserNameLenght)
DomainName = UserName
DomainNameLenght = UserNameLenght
LookupAccountSid vbNullString, ppsidGroup, UserName, UserNameLenght, DomainName, DomainNameLenght, peUse
GetProcUserNameA = Left$(UserName, UserNameLenght)
Exit Function
End If
End If
End If
CloseHandle hProcess
End If
End Function
Seul les admins et l'auteur du code lui même peuvent modifier ce code.