| 
| 
 | Вопрос # 5 543/ вопрос решён / | 
 |  Здравствуйте, уважаемые эксперты!Помогите пожалуйста
 Как можно определить имя пользователя из сервиса?
 
 Следующий код определяет имя пользователя в системе:
 
  function TForm1.GetUserFromWindows: string;
 var
   UserName : string;
   UserNameLen : dword;
 begin
   UserNameLen := 255;
   SetLength(userName, UserNameLen);
   if GetUserName(PChar(UserName), UserNameLen) then
      Result := Copy(UserName,1,UserNameLen - 1)
   else
     Result := 'Unknown';
  end;Все прекрасно работает, но потом потребовалось запустить данную программу через сервис.
 В результате, имя пользователя стало определяеться как SYSTEM.
 Как теперь можно определить имя активного пользователя в системе?
 
|  |   Вопрос задал: neon (статус: Посетитель)Вопрос отправлен: 30 июля 2011, 11:20
 Состояние вопроса: решён, ответов: 0.
 |  
 Мини-форум вопросаВсего сообщений: 2; последнее сообщение — 4 августа 2011, 06:16; участников в обсуждении: 2. 
|   | DNK (статус: Студент), 30 июля 2011, 13:16 [#1]:Почитай здесь. "Digital Networked Knight" |  30 июля 2011, 19:47: Вопрос перемещён из тематического раздела Delphi » Программирование с помощью API в раздел Delphi » Взаимодействие с Windows модератором Ерёмин А.А. 
|   | neon (статус: Посетитель), 4 августа 2011, 06:16 [#2]:Решилось только так: https://groups.google.com/group/borland.public.delphi.winapi/browse_thread/thread/18baae31c9b4173d/51a1fc3efaeaa18d%2351a1fc3efaeaa18d?hl=mo
 
 
 
 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; |  4 августа 2011, 06:16: Статус вопроса изменён на решённый (изменил автор вопроса — neon) Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте. |