Lua:getWindowProcessID

From Cheat Engine
Revision as of 20:33, 25 June 2026 by Leunsel (talk | contribs) (Major overhaul of the post.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<> Lua API Reference

function getWindowProcessID(windowhandle) : integer

Returns the process ID of the process that owns the specified window.

Function Parameters[edit]

Parameter Type Description
windowhandle Integer The handle of the window to query.

Returns[edit]

Integer — The process ID of the process that owns the specified window.

Examples[edit]

Get the process ID of a form window[edit]

1 local form = createForm(false)
2 form.Caption = "Process ID Test"
3 
4 local processID = getWindowProcessID(form.Handle)
5 
6 print("Window process ID: " .. tostring(processID))
7 
8 form.destroy()

Compare a window's process ID with the opened process ID[edit]

1 local form = getMainForm()
2 local windowProcessID = nil
3 
4 if form ~= nil then
5   windowProcessID = getWindowProcessID(form.Handle)
6 end
7 
8 print("Window PID: " .. tostring(windowProcessID))
9 print("Opened PID: " .. tostring(getOpenedProcessID()))

Use with the foreground window[edit]

1 local windowHandle = getForegroundWindow()
2 
3 if windowHandle ~= nil and windowHandle ~= 0 then
4   local processID = getWindowProcessID(windowHandle)
5 
6   print("Foreground window PID: " .. tostring(processID))
7 end

Check whether a window belongs to the opened process[edit]

 1 local windowHandle = getForegroundWindow()
 2 local openedProcessID = getOpenedProcessID()
 3 
 4 if windowHandle ~= nil and windowHandle ~= 0 then
 5   local windowProcessID = getWindowProcessID(windowHandle)
 6 
 7   if windowProcessID == openedProcessID then
 8     print("The foreground window belongs to the opened process")
 9   end
10 end

Get a process ID from a created GUI control[edit]

 1 local form = createForm(false)
 2 local button = createButton(form)
 3 
 4 button.Caption = "Button"
 5 button.Parent = form
 6 
 7 local processID = getWindowProcessID(form.Handle)
 8 
 9 print("Form owner process ID: " .. tostring(processID))
10 
11 form.destroy()

Guard against an invalid window handle[edit]

1 local windowHandle = 0
2 
3 if windowHandle ~= nil and windowHandle ~= 0 then
4   local processID = getWindowProcessID(windowHandle)
5 
6   print(processID)
7 else
8   print("Invalid window handle")
9 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine