Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;∙============================================================∙
- #Requires AutoHotkey v2.0
- #SingleInstance Force
- SetWorkingDir A_ScriptDir
- ;;∙============================================∙
- ^t:: { ;;∙------∙🔥∙(Ctrl + T)
- global vWinList
- global CharSet := "UTF-16"
- myGui := Gui("+AlwaysOnTop +Resize +MinSize400x300")
- myGui.SetFont("s10", "Segoe UI")
- myGui.Add("Text", "x10 y8", "Visible Window Owners:")
- vWinList := myGui.Add("ListView", "x10 y30 w640 h400 Grid", ["PID", "Process Name", "User", "Window Title"])
- myGui.Add("Button", "x10 y440 w100", "Refresh").OnEvent("Click", (*) => RefreshList())
- myGui.Add("Button", "x120 y440 w100", "Exit").OnEvent("Click", (*) => ExitApp())
- myGui.Show("w660 h480")
- myGui.Title := "Window User Ownership Viewer"
- RefreshList()
- }
- ;;∙============================================∙
- ;;∙------∙Enumerate All Visible Windows & Retrieves User/Process Info.
- RefreshList()
- {
- global vWinList
- vWinList.Delete()
- idList := WinGetList()
- for hwnd in idList
- {
- try {
- pid := WinGetPID("ahk_id " hwnd)
- title := WinGetTitle("ahk_id " hwnd)
- if (title = "")
- continue ;;∙------∙Skip untitled invisible windows.
- Image := GetProcessName(pid)
- User := GetProcessUser(pid)
- vWinList.Add("", pid, Image, User, title)
- }
- }
- vWinList.ModifyCol() ;;∙------∙Auto size columns.
- }
- ;;∙============================================∙
- ;;∙------∙Retrieve Process Executable Name From Pid.
- GetProcessName(PID)
- {
- global CharSet
- hProcess := DllCall("OpenProcess", "uint", 0x0410, "int", 0, "uint", PID, "ptr")
- if !hProcess
- return "Access Denied"
- FileName := Buffer(260 * 2, 0)
- len := DllCall("psapi.dll\GetModuleBaseNameW", "ptr", hProcess, "ptr", 0, "ptr", FileName, "uint", 260)
- DllCall("CloseHandle", "ptr", hProcess)
- return (len) ? StrGet(FileName, len, CharSet) : "Unknown"
- }
- ;;∙============================================∙
- ;;∙------∙Return Username Owning A Given Process.
- GetProcessUser(PID)
- {
- global CharSet
- TOKEN_QUERY := 0x0008
- PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
- hProcess := DllCall("OpenProcess", "uint", PROCESS_QUERY_LIMITED_INFORMATION, "int", 0, "uint", PID, "ptr")
- if !hProcess
- return "Access Denied"
- hToken := 0
- if !DllCall("advapi32.dll\OpenProcessToken", "ptr", hProcess, "uint", TOKEN_QUERY, "ptr*", &hToken)
- {
- DllCall("CloseHandle", "ptr", hProcess)
- return "Access Denied"
- }
- size := 0
- DllCall("advapi32.dll\GetTokenInformation", "ptr", hToken, "int", 1, "ptr", 0, "uint", 0, "uint*", &size)
- TOKENINFO := Buffer(size, 0)
- if !DllCall("advapi32.dll\GetTokenInformation", "ptr", hToken, "int", 1, "ptr", TOKENINFO, "uint", size, "uint*", &size)
- {
- DllCall("CloseHandle", "ptr", hToken)
- DllCall("CloseHandle", "ptr", hProcess)
- return "Access Denied"
- }
- pSID := NumGet(TOKENINFO, 0, "ptr")
- cchName := 260
- cchDomain := 260
- Name := Buffer(cchName * 2, 0)
- Domain := Buffer(cchDomain * 2, 0)
- peUse := 0
- if !DllCall("advapi32.dll\LookupAccountSidW", "ptr", 0, "ptr", pSID, "ptr", Name, "uint*", &cchName, "ptr", Domain, "uint*", &cchDomain, "uint*", &peUse)
- {
- user := "Unknown"
- }
- else
- {
- user := StrGet(Domain, CharSet) "\" StrGet(Name, CharSet)
- }
- DllCall("CloseHandle", "ptr", hToken)
- DllCall("CloseHandle", "ptr", hProcess)
- return user
- }
Advertisement
Add Comment
Please, Sign In to add comment