線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:824
推到 Plurk!
推到 Facebook!

在Service Application中能不能抓到目前登入的使用者?

尚未結案
bakayaro
一般會員


發表:12
回覆:14
積分:5
註冊:2002-10-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-06-20 17:28:47 IP:61.16.xxx.xxx 未訂閱
請問一下,我在一般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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-06-20 18:29:40 IP:202.39.xxx.xxx 未訂閱
參考這篇:  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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-06-21 01:38:24 IP:61.216.xxx.xxx 未訂閱
非常謝謝您的幫忙!!檢查explorer的方法看起來十分可行~ 由於現在已經下班了,我會在星期一到公司試試看您提供的方式,到時再回報結果,再一次的感謝您!!
系統時間:2024-04-28 4:18:38
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!