RobertDC67

Untitled

Oct 19th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;∙============================================================∙
  2. #Requires AutoHotkey v2.0
  3. #SingleInstance Force
  4. SetWorkingDir A_ScriptDir
  5.  
  6. ;;∙============================================∙
  7. ^t:: {    ;;∙------∙🔥∙(Ctrl + T)
  8.     global vWinList
  9.     global CharSet := "UTF-16"
  10.    
  11.     myGui := Gui("+AlwaysOnTop +Resize +MinSize400x300")
  12.     myGui.SetFont("s10", "Segoe UI")
  13.     myGui.Add("Text", "x10 y8", "Visible Window Owners:")
  14.     vWinList := myGui.Add("ListView", "x10 y30 w640 h400 Grid", ["PID", "Process Name", "User", "Window Title"])
  15.     myGui.Add("Button", "x10 y440 w100", "Refresh").OnEvent("Click", (*) => RefreshList())
  16.     myGui.Add("Button", "x120 y440 w100", "Exit").OnEvent("Click", (*) => ExitApp())
  17.     myGui.Show("w660 h480")
  18.     myGui.Title := "Window User Ownership Viewer"
  19.    
  20.     RefreshList()
  21. }
  22.  
  23. ;;∙============================================∙
  24. ;;∙------∙Enumerate All Visible Windows & Retrieves User/Process Info.
  25. RefreshList()
  26. {
  27.     global vWinList
  28.     vWinList.Delete()
  29.    
  30.     idList := WinGetList()
  31.    
  32.     for hwnd in idList
  33.     {
  34.         try {
  35.             pid := WinGetPID("ahk_id " hwnd)
  36.             title := WinGetTitle("ahk_id " hwnd)
  37.            
  38.             if (title = "")
  39.                 continue  ;;∙------∙Skip untitled invisible windows.
  40.            
  41.             Image := GetProcessName(pid)
  42.             User := GetProcessUser(pid)
  43.             vWinList.Add("", pid, Image, User, title)
  44.         }
  45.     }
  46.    
  47.     vWinList.ModifyCol()  ;;∙------∙Auto size columns.
  48. }
  49.  
  50. ;;∙============================================∙
  51. ;;∙------∙Retrieve Process Executable Name From Pid.
  52. GetProcessName(PID)
  53. {
  54.     global CharSet
  55.     hProcess := DllCall("OpenProcess", "uint", 0x0410, "int", 0, "uint", PID, "ptr")
  56.     if !hProcess
  57.         return "Access Denied"
  58.    
  59.     FileName := Buffer(260 * 2, 0)
  60.     len := DllCall("psapi.dll\GetModuleBaseNameW", "ptr", hProcess, "ptr", 0, "ptr", FileName, "uint", 260)
  61.     DllCall("CloseHandle", "ptr", hProcess)
  62.    
  63.     return (len) ? StrGet(FileName, len, CharSet) : "Unknown"
  64. }
  65.  
  66. ;;∙============================================∙
  67. ;;∙------∙Return Username Owning A Given Process.
  68. GetProcessUser(PID)
  69. {
  70.     global CharSet
  71.     TOKEN_QUERY := 0x0008
  72.     PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
  73.    
  74.     hProcess := DllCall("OpenProcess", "uint", PROCESS_QUERY_LIMITED_INFORMATION, "int", 0, "uint", PID, "ptr")
  75.     if !hProcess
  76.         return "Access Denied"
  77.    
  78.     hToken := 0
  79.     if !DllCall("advapi32.dll\OpenProcessToken", "ptr", hProcess, "uint", TOKEN_QUERY, "ptr*", &hToken)
  80.     {
  81.         DllCall("CloseHandle", "ptr", hProcess)
  82.         return "Access Denied"
  83.     }
  84.    
  85.     size := 0
  86.     DllCall("advapi32.dll\GetTokenInformation", "ptr", hToken, "int", 1, "ptr", 0, "uint", 0, "uint*", &size)
  87.     TOKENINFO := Buffer(size, 0)
  88.    
  89.     if !DllCall("advapi32.dll\GetTokenInformation", "ptr", hToken, "int", 1, "ptr", TOKENINFO, "uint", size, "uint*", &size)
  90.     {
  91.         DllCall("CloseHandle", "ptr", hToken)
  92.         DllCall("CloseHandle", "ptr", hProcess)
  93.         return "Access Denied"
  94.     }
  95.    
  96.     pSID := NumGet(TOKENINFO, 0, "ptr")
  97.     cchName := 260
  98.     cchDomain := 260
  99.     Name := Buffer(cchName * 2, 0)
  100.     Domain := Buffer(cchDomain * 2, 0)
  101.     peUse := 0
  102.    
  103.     if !DllCall("advapi32.dll\LookupAccountSidW", "ptr", 0, "ptr", pSID, "ptr", Name, "uint*", &cchName, "ptr", Domain, "uint*", &cchDomain, "uint*", &peUse)
  104.     {
  105.         user := "Unknown"
  106.     }
  107.     else
  108.     {
  109.         user := StrGet(Domain, CharSet) "\" StrGet(Name, CharSet)
  110.     }
  111.    
  112.     DllCall("CloseHandle", "ptr", hToken)
  113.     DllCall("CloseHandle", "ptr", hProcess)
  114.    
  115.     return user
  116. }
Advertisement
Add Comment
Please, Sign In to add comment