Difference between revisions of "Lua:getWindowProcessID"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
Line 1: Line 1:
<p>
+
[[Category:Lua]]
<b>getWindowProcessID(windowhandle)</b>: processid - Returns the processid of the process this window belongs to
+
{{CodeBox|'''function''' getWindowProcessID(''windowhandle'') ''':''' integer}}
</p>
 
  
<pre>
+
Returns the process ID of the process that owns the specified window.
local hwnd = findWindow("WTWindow", "Game")
+
 
local processID,threadID = getWindowProcessID(hwnd)
+
===Function Parameters===
print(processID, threadID)
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
</pre>
+
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|windowhandle
 +
|Integer
 +
|The handle of the window to query.
 +
|}
 +
 
 +
===Returns===
 +
Integer — The process ID of the process that owns the specified window.
 +
 
 +
===Examples===
 +
 
 +
====Get the process ID of a form window====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local form = createForm(false)
 +
form.Caption = "Process ID Test"
 +
 
 +
local processID = getWindowProcessID(form.Handle)
 +
 
 +
print("Window process ID: " .. tostring(processID))
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Compare a window's process ID with the opened process ID====
 +
<syntaxhighlight lang="lua" line highlight="5">
 +
local form = getMainForm()
 +
local windowProcessID = nil
 +
 
 +
if form ~= nil then
 +
  windowProcessID = getWindowProcessID(form.Handle)
 +
end
 +
 
 +
print("Window PID: " .. tostring(windowProcessID))
 +
print("Opened PID: " .. tostring(getOpenedProcessID()))
 +
</syntaxhighlight>
 +
 
 +
====Use with the foreground window====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local windowHandle = getForegroundWindow()
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local processID = getWindowProcessID(windowHandle)
 +
 
 +
  print("Foreground window PID: " .. tostring(processID))
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Check whether a window belongs to the opened process====
 +
<syntaxhighlight lang="lua" line highlight="5">
 +
local windowHandle = getForegroundWindow()
 +
local openedProcessID = getOpenedProcessID()
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local windowProcessID = getWindowProcessID(windowHandle)
 +
 
 +
  if windowProcessID == openedProcessID then
 +
    print("The foreground window belongs to the opened process")
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Get a process ID from a created GUI control====
 +
<syntaxhighlight lang="lua" line highlight="7">
 +
local form = createForm(false)
 +
local button = createButton(form)
 +
 
 +
button.Caption = "Button"
 +
button.Parent = form
 +
 
 +
local processID = getWindowProcessID(form.Handle)
 +
 
 +
print("Form owner process ID: " .. tostring(processID))
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Guard against an invalid window handle====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local windowHandle = 0
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local processID = getWindowProcessID(windowHandle)
 +
 
 +
  print(processID)
 +
else
 +
  print("Invalid window handle")
 +
end
 +
</syntaxhighlight>
 +
 
 +
{{LuaSeeAlso}}

Latest revision as of 20:33, 25 June 2026

<> 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