在Service Application中能不能抓到目前登入的使用者? |
尚未結案
|
bakayaro
一般會員 發表:12 回覆:14 積分:5 註冊:2002-10-28 發送簡訊給我 |
請問一下,我在一般AP下使用GetUserName(),可以成功取得Login Name,但是當我將程式寫成Service之後,抓到的都是SYSTEM..... 我也參考過 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=357 這邊的方法,但是依然只能抓到
UserName : SYSTEM
Domain : NT AUTHORITY 請問一下有解決的方式嗎?謝謝各位的幫忙!!
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
參考這篇:
http://groups.google.com.tw/groups?hl=zh-TW&lr=&ie=UTF-8&oe=UTF-8&selm=VA.00000ae4.00033fa0%40gbhstorewkss490&rnum=11
它用另一種方法:
uses windows, psapi; function GetExplorerUser : string; var explorerProcessHandle : THandle; tokenhandle : THandle; puser : PSidAndAttributes; infoLen : DWORD; userName, domainName : array [0..256] of char; userNameLen, domainNameLen : DWORD; use : Sid_Name_Use; function OpenProcessHandle (const process : string) : THandle; var buffer, pid : PDWORD; bufLen, cbNeeded : DWORD; hp : THandle; fileName : array [0..256] of char; i : Integer; begin result := 0; bufLen := 65536; GetMem (buffer, bufLen); try if EnumProcesses (buffer, bufLen, cbNeeded) then begin pid := buffer; for i := 0 to cbNeeded div sizeof (DWORD) - 1 do begin hp := OpenProcess (PROCESS_VM_READ or PROCESS_QUERY_INFORMATION, False, pid^); if hp <> 0 then try if (GetModuleBaseName (hp, 0, fileName, sizeof (fileName)) > 0) and (CompareText (fileName, process) = 0) then begin result := hp; break end finally if result = 0 then CloseHandle (hp) end; Inc (pid) end end finally FreeMem (buffer) end end; begin result := ''; explorerProcessHandle := OpenProcessHandle ('explorer.exe'); if explorerProcesshandle <> 0 then try if OpenProcessToken (explorerProcessHandle, TOKEN_QUERY, tokenHandle) then try GetMem (puser, 65536); try if GetTokenInformation (tokenHandle, TokenUser, puser, 65536, infoLen) then begin if LookupAccountSID (nil, puser^.Sid, userName, userNameLen, domainName, domainNameLen, use) then result := userName end finally FreeMem (puser) end finally CloseHandle (tokenHandle) end finally CloseHandle (explorerProcessHandle) end end;--- 每個人都是一本書 |
bakayaro
一般會員 發表:12 回覆:14 積分:5 註冊:2002-10-28 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |