VB 通過程序PID獲取該程式的視窗控制代碼函式

2021-09-29 01:19:03 字數 1310 閱讀 8200

option explicit

private declare function getwindowthreadprocessid lib "user32" (byval hwnd as long, lpdwprocessid as long) as long

private declare function getparent lib "user32" (byval hwnd as long) as long

private declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname as long, byval lpwindowname as long) as long

private declare function getwindow lib "user32" (byval hwnd as long, byval wcmd as long) as long

const gw_hwndnext = 2

' 通過程序id獲得該程序的視窗控制代碼

public function instancetownd(byval target_pid as long) as long

dim test_hwnd as long

dim test_pid as long

dim test_thread_id as long

instancetownd = 0

'on error resume next

' 獲得首個handle.

test_hwnd = findwindow(byval 0&, byval 0&)

' 迴圈查詢直到找到為給定程序id的視窗控制代碼

do while test_hwnd <> 0

'檢查視窗控制代碼是否為頂級視窗

if getparent(test_hwnd) = 0 then

' 是頂級視窗

' 取該視窗所屬的程序id

test_thread_id = getwindowthreadprocessid(test_hwnd, test_pid)

if test_pid = target_pid then

' 是我們指定程序的視窗,則將該視窗的控制代碼返回到函式名,並退出

instancetownd = test_hwnd

exit do

end if

end if

' 取下乙個視窗的控制代碼

test_hwnd = getwindow(test_hwnd, gw_hwndnext)

loop

end function

通過Docker程序pid獲取容器id

雖然docker是通過namespace隔離技術實現容器間程序的隔離,但在執行docker的主機中,docker容器內的程序與主機內執行的程序是在同乙個namespace 假設叫a 的。雖然在docker容器內應用程序的父程序都是pid為1的那個程序 這些程序都是單獨的namespace,這個nam...

linux 下通過pid 定位該程式開啟的埠

首先使用ps aux 檢測到耗資源的程序。4652 mysql 15 0 454m 124m 4752 s 58 3.1 2144 29 mysqld 如mysqld。active internet connections servers and established proto recv q s...

bash shell獲取程序的pid

前幾天在寫乙個指令碼時在獲取程序的pid時出現了一點點意外,當時的情境大概是這樣的 跑乙個任務a,然後任務b要監控a的狀態,一旦a結束,b也要結束。當初第一反應就是獲取b的pid然後判斷a是否在執行,如果沒有則kill掉b。可是當初就是不成功 用的是 於是迫於任務緊急換了一種笨方法 讓b的執行時間稍...