取进程用户

本文提供了一个使用VBA从指定进程ID中获取用户名的示例代码,通过调用Windows API函数实现进程句柄的打开及令牌信息的读取,并解析SID得到账户名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Option   Explicit

Private   Const   TOKEN_READ   As   Long   =   &H20008
Private   Const   SECURITY_BUILTIN_DOMAIN_RID   As   Long   =   &H20&
Private   Const   SECURITY_NT_AUTHORITY   As   Long   =   &H5
Private   Const   PROCESS_QUERY_INFORMATION   As   Long   =   1024
Private   Const   PROCESS_VM_READ   As   Long   =   16
Private   Const   DOMAIN_ALIAS_RID_USERS   =   &H221
Private   Const   TokenUser   =   1
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_USER
User   As   SID_AND_ATTRIBUTES
SID(500)   As   Byte
End   Type
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   CloseHandle   Lib   "kernel32"   (ByVal   hObject   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

Public   Function   GetProcessUserName(ByVal   ProcessID   As   Long)   As   String
        Dim   hProcessID   As   Long
        Dim   hToken   As   Long
        Dim   res   As   Long
        Dim   cbBuff   As   Long
        Dim   tiLen   As   Long
        Dim   TU   As   TOKEN_USER
        Dim   cnt   As   Long
        Dim   sAcctName2   As   String
        Dim   cbAcctName   As   Long
        Dim   sDomainName   As   String
        Dim   cbDomainName   As   Long
        Dim   peUse   As   Long
        Dim   barr()   As   Byte
        hProcessID   =   OpenProcess(PROCESS_QUERY_INFORMATION   Or   PROCESS_VM_READ,   0,   ProcessID)
        If   hProcessID   < >   0   Then
                If   OpenProcessToken(hProcessID,   TOKEN_READ,   hToken)   =   1   Then
                        res   =   GetTokenInformation(hToken,   TokenUser,   ByVal   0,   tiLen,   cbBuff)
                        If   res   =   0   And   cbBuff   >   0   Then
                                tiLen   =   cbBuff
                                If   cbBuff   >   Len(TU)   Then   Exit   Function
                                res   =   GetTokenInformation(hToken,   TokenUser,   TU,   tiLen,   cbBuff)
                                If   res   =   1   And   tiLen   >   0   Then
                                        sAcctName2   =   Space$(255)
                                        sDomainName   =   Space$(255)
                                        cbAcctName   =   255
                                        cbDomainName   =   255
                                        res   =   LookupAccountSid(vbNullString,   TU.User.SID,   sAcctName2,   cbAcctName,   sDomainName,   cbDomainName,   peUse)
                                        GetProcessUserName   =   Replace(Trim(sAcctName2),   Chr(0),   "")
                                End   If
                        End   If
                End   If
                If   hToken   Then   CloseHandle   hToken
                CloseHandle   hProcessID
        End   If
End   Function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值