VB 斷開指定程序網路連線函式

2021-08-27 04:38:02 字數 4852 閱讀 1318

vb 斷開指定程序網路連線函式,通過指定程序名字,斷開該程序的網路連線。 

private const process_query_information = 1024

private const process_vm_read = 16

private const max_path = 256

private const af_inet6 = 23

private const af_inet = 2

public enum tcp_table_class

tcp_table_basic_listener

tcp_table_basic_connections

tcp_table_basic_all

tcp_table_owner_pid_listener

tcp_table_owner_pid_connections

tcp_table_owner_pid_all

tcp_table_owner_module_listener

tcp_table_owner_module_connections

tcp_table_owner_module_all

end enum

private type mib_tcprow_owner_pid

dwstate as long

dwlocaladdr as long

dwlocalport as long

dwremoteaddr as long

dwremoteport as long

dwowningpid as long

end type

public declare function closehandle lib "kernel32.dll" (byval handle as long) as long

public declare function openprocess lib "kernel32.dll" (byval dwdesiredaccessas as long, byval binherithandle as long, byval dwprocid as long) as long

public declare function getmodulefilenameexa lib "psapi.dll" (byval hprocess as long, byval hmodule as long, byval modulename as string, byval nsize as long) as long

public declare function enumprocessmodules lib "psapi.dll" (byval hprocess as long, byref lphmodule as long, byval cb as long, byref cbneeded as long) as long

private declare function htons lib "ws2_32.dll" (byval dwlong as long) as long

public declare function getextendedtcptable lib "iphlpapi.dll" (ptcptableex as any, lsize as long, byval border as long, byval flags as long, byval tableclass as tcp_table_class, byval breserved as long) as long

public declare sub copymemory lib "kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)

public declare function settcpentry lib "iphlpapi.dll" (byref ptcptable as mib_tcprow_owner_pid) as long

private 程序表指標() as byte

public r行 as long

private 資料參考 as long

public function 重新整理() as boolean

dim 指標大小 as long, 參考 as long

指標大小 = 4

參考 = getextendedtcptable(0&, 指標大小, 1, af_inet, tcp_table_owner_pid_all, 0)

redim 程序表指標(指標大小 - 1)

參考 = getextendedtcptable(程序表指標(0), 指標大小, 1, af_inet, tcp_table_owner_pid_all, 0)

if 參考 = 0 then

copymemory r行, 程序表指標(0), 4

else

重新整理 = false

exit function

end if

if r行 = 0 or 程序表指標(0) then

重新整理 = false

exit function

end if

end function

public function 切斷程式網路鏈結(程序名 as string)

on error resume next

dim i as long

dim tcp表 as mib_tcprow_owner_pid

資料參考 = 0

dim 程序名稱 as string

for i = 0 to r行

程序名稱 = ""

copymemory tcp表, 程序表指標(0 + 資料參考 + 4), lenb(tcp表)

if tcp表.dwremoteaddr <> 0 or 取埠號(tcp表.dwremoteport) <> 0 or 取埠號(tcp表.dwlocalport) <> 0 then

' debug.print "狀態:"; c_state(tcp表.dwstate); ",";

' debug.print "本地ip:"; 獲取ip位址(tcp表.dwlocaladdr); ",";

' debug.print "本地port:"; 取埠號(tcp表.dwlocalport); ",";

' debug.print "遠端ip:"; tcp表.dwremoteaddr; ",";

' debug.print "遠端port:"; 取埠號(tcp表.dwremoteport); ",";

' debug.print "程序id:"; tcp表.dwowningpid; ",";

' debug.print "程序名:"; 獲取程序路徑(tcp表.dwowningpid)

程序名稱 = 獲取ip位址(tcp表.dwremoteaddr) & 獲取程序路徑(tcp表.dwowningpid)

if instr(lcase$(程序名稱), lcase$(程序名)) > 0 then

tcp表.dwstate = 12

settcpentry tcp表

end if

end if

資料參考 = 資料參考 + lenb(tcp表)

doevents

next i

end function

public function 取埠號(byval dwport as long) as long

取埠號 = htons(dwport)

end function

public function 獲取ip位址(dwaddr as long) as string

dim ip資料(3) as byte

copymemory ip資料(0), dwaddr, 4

獲取ip位址 = cstr(ip資料(0)) & "." & cstr(ip資料(1)) & "." & cstr(ip資料(2)) & "." & cstr(ip資料(3))

end function

public function 獲取程序路徑(pid as long) as string

dim cbneeded as long

dim modules(1 to 200) as long

dim nsize as long

dim lret as long

dim modulename as string

dim hprocess as long

if pid = 0 then 獲取程序路徑 = "": exit function

if pid = 4 then 獲取程序路徑 = "": exit function

hprocess = openprocess(process_query_information or process_vm_read, 0, pid)

if hprocess <> 0 then

lret = enumprocessmodules(hprocess, modules(1), 200, cbneeded)

if lret <> 0 then

modulename = space$(max_path)

nsize = max_path

lret = getmodulefilenameexa(hprocess, modules(1), modulename, nsize)

if cbool(instr(1, (left$(modulename, lret)), "", vbtextcompare)) then

獲取程序路徑 = left$(modulename, lret)

end if

end if

end if

lret = closehandle(hprocess)

end function

VB停止程序

遇到乙個問題,就是vb6.0操作excel的過程中,往往會出現無法停掉excel程序的問題。這個問題尤其出現在操作excel2000的過程中,所以想強制終止excel程序。從網上查了好久,不是呼叫api就是,就是那一大堆的 嘻嘻 不過從同事那裡請教倆乙個方法 dim objwmiservice as...

刪除指定程序

include cstring str,prcnum processentry32 processinfo 宣告程序資訊變數 processinfo.dwsize sizeof processinfo 設定processinfo的大小 返回系統中第乙個程序的資訊 bool status proces...

監控指定程序

對於守護中介軟體是非常有用的。中介軟體不可能絕對的穩定而不出問題,中介軟體有可能因比較嚴重的錯誤導致當機或者程序被人為地錯誤地關閉了中介軟體。有了這個自動守護程序的存在,這一切的問題都可以迎刃而解。program monitor uses winapi.windows,system.sysutils...