| 发表于:2007-03-03 11:05:591楼 得分:20 |
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 | | |
|