Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the main window handle by PID
#1
I would like to obtain the main window handle using a PID. Is there a simple way to achieve this in QM?
The following code does not work.

Macro T
 
Code:
Copy      Help
out

int i=4696
out win("" "" "" 0 F"threadId={i}")
 
PowerShell implementation code: 
Quote:(Get-Process -Id 4696).MainWindowHandle
#2
C#:
print.it((wnd)Process.GetProcessById(10876).MainWindowHandle);

Notes:
- In Windows API there is no property "main window of a process".
- .NET and PowerShell simply gets the first-in-Z-order visible unowned window of that process. A process may have any number of such windows. `MainWindowHandle` can return a random temporary window such as a tooltip.

This QM function works exactly like the .NET `MainWindowHandle`. And probably like that of PowerShell.

Function Process_MainWindowHandle
Code:
Copy      Help
;/
function# processId

int r
EnumWindows &sub.EnumProc 0
ret r


#sub EnumProc v
function# w _

if IsWindowVisible(w)
,GetWindowThreadProcessId(w &_i)
,if _i=processId
,,if GetWindow(w GW_OWNER) = 0
,,,r = w
,,,ret 0
ret 1
#3
thank you so much!


Forum Jump:


Users browsing this thread: 1 Guest(s)