Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Retrieve the HWND by knowing the process ID
Message
From
14/05/2018 22:01:08
 
 
To
14/05/2018 18:37:49
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 10
Application:
Desktop
Miscellaneous
Thread ID:
01659980
Message ID:
01659983
Views:
70
Hi Alessio, below you have the "List child windows for the Windows desktop" from https://github.com/VFPX/Win32API/blob/master/samples/sample_027.md,
with a slight modification , using GetWindowThreadProcessId() to get the PID
from hWnd , that outputs a table with all the running processes with their hwnd &
corresponding PID's.

Marco
#Define GW_HWNDLAST 1
#Define GW_HWNDNEXT 2
#Define GW_CHILD 5
Do Declare

Create Cursor csResult (HWnd N(12), pid i,isvisible N(1),;
	leftpos i, toppos i, rightpos i, botpos i, wincap C(64), classname C(64))

Local hDesktop, hFirstChild, hLastChild, rc, cWinCap, cWinClass,;
	nVisible, nLeft, nTop, nRight, nBottom

hDesktop = GetDesktopWindow()
hFirstChild = GetWindow(m.hDesktop, GW_CHILD)
hLastChild = GetWindow(m.hFirstChild, GW_HWNDLAST)

hCurrent = m.hFirstChild

Do While .T.

	cWinCap = GetWinText(m.hCurrent)
	cWinClass= GetClsName(m.hCurrent)
	nVisible = IsWindowVisible(m.hCurrent)

	rc = Repli(Chr(0),16)
	= GetWindowRect(m.hCurrent, @rc)
	nLeft = buf2dword(Substr(m.rc, 1,4))
	nTop = buf2dword(Substr(m.rc, 5,4))
	nRight = buf2dword(Substr(m.rc, 9,4))
	nBottom = buf2dword(Substr(m.rc, 13,4))

	pidx = 0
	GetWindowThreadProcessId(m.hCurrent,@pidx)

	Insert Into csResult Values (m.hCurrent, m.pidx, m.nVisible,m.nLeft, m.nTop, m.nRight, m.nBottom, m.cWinCap, m.cWinClass)

	If m.hCurrent = m.hLastChild
		Exit
	Endif
	hCurrent = GetWindow(m.hCurrent, GW_HWNDNEXT)

Enddo

Go Top
Browse Normal Nowait

*------------------------------------------
Function GetClsName(hWindow)
*------------------------------------------
Local nBufsize, cBuffer
cBuffer = Repli(Chr(0), 250)
nBufsize = GetClassName(hWindow, @cBuffer, Len(cBuffer))
Return Substr(cBuffer, 1, nBufsize)

*----------------------------------------
Function GetWinText(hWindow)
*----------------------------------------
Local cBuffer, nResult
cBuffer = Space(250)
nResult = GetWindowText(hWindow, @cBuffer, Len(cBuffer))
Return Substr(cBuffer, 1, nResult)


*----------------------------------------------------
Function buf2dword(lcBuffer)
*----------------------------------------------------
Return Asc(Substr(lcBuffer, 1,1)) + ;
	BitLShift(Asc(Substr(lcBuffer, 2,1)),  8) +;
	BitLShift(Asc(Substr(lcBuffer, 3,1)), 16) +;
	BitLShift(Asc(Substr(lcBuffer, 4,1)), 24)

*----------------------------------------------------
Procedure Declare
*----------------------------------------------------

Declare Integer GetDesktopWindow In user32
Declare Integer GetWindow In user32 Integer HWnd, Integer wFlag
Declare Integer GetWindowRect In user32 Integer HWnd, String @lpRect
Declare Integer IsWindowVisible In user32 Integer HWnd
Declare Integer GetWindowText In user32;
	INTEGER HWnd, String @lpString, Integer cch

Declare Integer GetClassName In user32;
	INTEGER HWnd, String lpClassName, Integer nMaxCount

Declare Integer GetWindowThreadProcessId In user32    Integer HWnd,    Integer @pid
@nfoxdev
github.com/nfoxdev
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform