如何获取当前进程的执行用户名? ( 积分: 200 )

  • 主题发起人 主题发起人 mosker
  • 开始时间 开始时间
M

mosker

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,想写一个程序获取程序运行的用户名,就象win2000 xp中的任务管理器一样可以察看哪个进程是哪个用户运行的,看了很多资料也没有解决,请高人给点思路最好是代码,本人送上200分.
 
如题,想写一个程序获取程序运行的用户名,就象win2000 xp中的任务管理器一样可以察看哪个进程是哪个用户运行的,看了很多资料也没有解决,请高人给点思路最好是代码,本人送上200分.
 
//取得当前登陆系统的用户<br>procedure TForm1.Button1Click(Sender: TObject);<br>Var<br> &nbsp;lpBuffer :Array[1..64] of Char;<br> &nbsp;nSize &nbsp; &nbsp;:Cardinal;<br> &nbsp;sUserName : String;<br>Begin<br> &nbsp;nSize := 64;<br> &nbsp;GetUserName(@lpBuffer, nSize);<br> &nbsp;sUserName := lpBuffer;<br> &nbsp;sUserName := Copy(sUserName, 1, nSize-1);<br> &nbsp;Edit1.Text := sUserName;<br>end;
 
我不是要获取当前登陆的用户,我是要获取当前所有进程中运行的程序的用户名.比如你看任务管理器里面所有进程,有的程序的用户是administrat,有的是system,有的有是local service 等等,我就是要获取任何一正在运行的程序的用户名.有什么办法吗?
 
参考下这贴<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1938095
 
来自:wfzha, 时间:2003-6-13 20:02:00, ID:1950632 <br>上面的例子中用到了GetCurrentProcess函数取得当前进程的句柄,然后调用<br>OpenProcessToken打开当前进程的访问令牌<br>通过GetTokenInformation的参数中有一个TokenInformationClass得东东,它里面有一个TOKEN_OWNER structure<br>它可以返回owner的sID<br>通过LookupAccountSid函数返回用户信息<br>API是这么说的,我的E文不好,不知看得对不对,反正我是没试过<br> <br><br>上面虽然讲了过程,但是没有具体的东西,那些API不会用,所以请会的朋友帮个忙写点代码.
 
分给我吧,我正好要用分,呵呵。。。<br>//取进程所属的用户名和域,xp+DELPHI7下成功<br>function GetUserAndDomainFromPID(ProcessId: DWORD;<br> &nbsp;var User, Domain: string): Boolean;<br>var<br> &nbsp;hToken: THandle; <br> &nbsp;cbBuf: Cardinal; <br> &nbsp;ptiUser: PTOKEN_USER; <br> &nbsp;snu: SID_NAME_USE; <br> &nbsp;ProcessHandle: THandle; <br> &nbsp;UserSize, DomainSize: DWORD; <br> &nbsp;bSuccess: Boolean; <br>begin <br> &nbsp;Result := False; <br> &nbsp;ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId); <br> &nbsp;if ProcessHandle &lt;&gt; 0 then <br> &nbsp;begin<br> &nbsp; &nbsp;if OpenProcessToken(ProcessHandle, TOKEN_QUERY, hToken) then <br> &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp;bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf); <br> &nbsp; &nbsp; &nbsp;ptiUser &nbsp;:= nil; <br> &nbsp; &nbsp; &nbsp;while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do <br> &nbsp; &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp; &nbsp;ReallocMem(ptiUser, cbBuf); <br> &nbsp; &nbsp; &nbsp; &nbsp;bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf, cbBuf); <br> &nbsp; &nbsp; &nbsp;end; <br> &nbsp; &nbsp; &nbsp;CloseHandle(hToken); <br><br> &nbsp; &nbsp; &nbsp;if not bSuccess then <br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit; <br> &nbsp; &nbsp; &nbsp;end; <br><br> &nbsp; &nbsp; &nbsp;UserSize := 0; <br> &nbsp; &nbsp; &nbsp;DomainSize := 0; <br> &nbsp; &nbsp; &nbsp;LookupAccountSid(nil, ptiUser.User.Sid, nil, UserSize, nil, DomainSize, snu); <br> &nbsp; &nbsp; &nbsp;if (UserSize &lt;&gt; 0) and (DomainSize &lt;&gt; 0) then <br> &nbsp; &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp; &nbsp;SetLength(User, UserSize); <br> &nbsp; &nbsp; &nbsp; &nbsp;SetLength(Domain, DomainSize); <br> &nbsp; &nbsp; &nbsp; &nbsp;if LookupAccountSid(nil, ptiUser.User.Sid, PChar(User), UserSize, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PChar(Domain), DomainSize, snu) then <br> &nbsp; &nbsp; &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;User := StrPas(PChar(User)); <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Domain := StrPas(PChar(Domain)); <br> &nbsp; &nbsp; &nbsp; &nbsp;end; <br> &nbsp; &nbsp; &nbsp;end; <br><br> &nbsp; &nbsp; &nbsp;if bSuccess then <br> &nbsp; &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp; &nbsp;FreeMem(ptiUser); <br> &nbsp; &nbsp; &nbsp;end; <br> &nbsp; &nbsp;end; <br> &nbsp; &nbsp;CloseHandle(ProcessHandle); <br> &nbsp;end; <br>end;
 
好象漏了点,PTOKEN_USER的定义没有。[:D]<br>type<br> &nbsp;PTOKEN_USER = ^TOKEN_USER;<br> &nbsp;_TOKEN_USER = record<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;User: TSidAndAttributes;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp;TOKEN_USER = _TOKEN_USER;<br><br>借花献佛,改动一点点。<br>function GetUserAndDomainFromPID(ProcessId: DWORD;<br> &nbsp;var User, Domain: string): Boolean;<br>var<br> &nbsp;hToken: THandle;<br> &nbsp;cbBuf: Cardinal;<br> &nbsp;ptiUser: PSIDAndAttributes;<br> &nbsp;snu: SID_NAME_USE;<br> &nbsp;ProcessHandle: THandle;<br> &nbsp;UserSize, DomainSize: DWORD;<br> &nbsp;bSuccess: Boolean;<br>begin<br> &nbsp;Result := False;<br> &nbsp;ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId);<br> &nbsp;if ProcessHandle &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;if OpenProcessToken(ProcessHandle, TOKEN_QUERY, hToken) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf);<br> &nbsp; &nbsp; &nbsp;ptiUser &nbsp;:= nil;<br> &nbsp; &nbsp; &nbsp;while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;ReallocMem(ptiUser, cbBuf);<br> &nbsp; &nbsp; &nbsp; &nbsp;bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf, cbBuf);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;CloseHandle(hToken);<br><br> &nbsp; &nbsp; &nbsp;if not bSuccess then Exit;<br> &nbsp; &nbsp; &nbsp;UserSize := 0;<br> &nbsp; &nbsp; &nbsp;DomainSize := 0;<br> &nbsp; &nbsp; &nbsp;LookupAccountSid(nil, ptiUser.Sid, nil, UserSize, nil, DomainSize, snu);<br> &nbsp; &nbsp; &nbsp;if (UserSize &lt;&gt; 0) and (DomainSize &lt;&gt; 0) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;SetLength(User, UserSize);<br> &nbsp; &nbsp; &nbsp; &nbsp;SetLength(Domain, DomainSize);<br> &nbsp; &nbsp; &nbsp; &nbsp;if LookupAccountSid(nil, ptiUser.Sid, PChar(User), UserSize,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PChar(Domain), DomainSize, snu) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;User := StrPas(PChar(User));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Domain := StrPas(PChar(Domain));<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;FreeMem(ptiUser);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;CloseHandle(ProcessHandle);<br> &nbsp;end;<br>end;
 
呵呵,好的谢谢,等我测试下行了马上给分.
 
多谢拉,[:D]<br>调试通过!
 
晕拉,分给错了,!<br>YuZi应该是150分,晕了,没关系我重新开一个贴给你吧.<br><br>请进:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=3072492
 
andy263,还分来!!![:D]
 
后退
顶部