读书人

PB怎么结束run运行的进程

发布时间: 2013-01-01 14:04:19 作者: rapoo

PB如何结束run运行的进程?
本帖最后由 bombshell 于 2011-11-26 10:34:25 编辑 在程序中用RUN运行了一个进程,想结束某个进程,并且希望RUN程序与PB主程序系统工作,请问该调用那个API?谢谢!
[解决办法]
下面是一个用户对象,用于获得进程PID,然后,可以用后面的脚本结束该进程。
$PBExportHeader$uf_get_exename.sru
$PBExportComments$获得进程
forward
global type uf_get_exename from nonvisualobject
end type
type s_process from structure within uf_get_exename
end type
end forward

type s_process from structure
unsignedlongstructsize
unsignedlongusage
unsignedlongprocessid
unsignedlongdefaultheapid
unsignedlongmoduleid
unsignedlongthreads
unsignedlongparentprocessid
unsignedlongcalssbase
unsignedlongflags
characterfilename[260]
end type

global type uf_get_exename from nonvisualobject
end type
global uf_get_exename uf_get_exename

type prototypes
Function Long CreateToolhelp32Snapshot(Long Flags,Long ProcessId) Library "kernel32.dll" alias for "CreateToolhelp32Snapshot;ansi"
Function Integer Process32First(uLong Snapshot,ref s_Process Process) Library "kernel32.dll" alias for "Process32First;ansi"
Function Integer Process32Next(uLong Snapshot,ref s_Process Process) Library "kernel32.dll" alias for "Process32Next;ansi"



end prototypes
forward prototypes
public function long of_getexe (string as_exename)
end prototypes

public function long of_getexe (string as_exename);//构造对象uf_get_exename的函数of_getexe(String as_exename)

///////////////////////////of_getexe(String as_exename)////////////////////////
//功能:枚举进程并返回指定进程号PID
//传入:String as_exename 文件名
//返回:Long
/////////////////////////////////////////////////////////////

s_Process lst_Process
string ls_filename[100] ,ls_curexename
ulong ln_ProcessID,ln_SameCount,ln_Snapshot,ln_Circle,ln_Count,ul_PID

ul_PID = 0
ln_Snapshot = CreateToolhelp32Snapshot(2,0)
if (ln_Snapshot<1) then return 0 //创建快照失败
lst_Process.StructSize = 296 //创建快照失败 296是windows决定的

if Process32First(ln_Snapshot,lst_Process)=0 then return 0

//枚举当前权限下的进程
debugbreak()
do while true

if Process32Next(ln_Snapshot,lst_Process)=0 then exit
ln_Count = ln_Count + 1
ls_FileName[ln_Count] = lst_Process.FileName
If Lower(ls_FileName[ln_Count]) = as_exename Then
//取得进程号
ul_PID = lst_Process.ProcessID
//messagebox(string(ul_PID),ls_FileName[ln_Count])
End If

loop

return ul_PID


end function

on uf_get_exename.create
call super::create
TriggerEvent( this, "constructor" )
end on

on uf_get_exename.destroy


TriggerEvent( this, "destructor" )
call super::destroy
end on

PB事件中的脚本例子:
这是在窗口关闭前,杀掉快播的两个进程
//用于杀掉qvod的两个进程
uf_get_exename luf_get_exename

INTEGER li_rc,j
ULONG ul_PID
ULONG PROCESS_TERMINATE = 0001
ULONG hwdprocess
String ls_exeName[]

ls_exeName[1] = "qvodplayer.exe" //要结束的进程名
ls_exeName[2] = "qvodterminal.exe"
//If messagebox('结束进程','确定吗?Kill?',question!,yesno!,1) = 2 Then return

//创建实例变量
luf_get_exename = create uf_get_exename

for j = 1 to 2
//获取指定进程号
ul_PID = luf_get_exename.of_getexe(ls_exeName[j])

If ul_PID = 0 Then
Messagebox('结束进程','没有发现进程!')
return
End If

If ul_PID <> 0 Then
//获取指定进程号的进程句柄
hwdprocess = OpenProcess(PROCESS_TERMINATE,1,ul_PID)
//结束进程,成功返回非零
li_rc = TerminateProcess(hwdprocess,0)
//If li_rc <> 0 Then Messagebox('结束进程','成功结束进程!')
End If
next


Destroy luf_get_exename;

[解决办法]
将以下内容保存为本地文件nvo_kill_process.sru , 然后导入pbl中

$PBExportHeader$nvo_kill_process.sru
forward
global type nvo_kill_process from nonvisualobject
end type
type s_process from structure within nvo_kill_process
end type
end forward

type s_process from structure
unsignedlongstructsize
unsignedlongusage
unsignedlongprocessid
unsignedlongdefaultheapid
unsignedlongmoduleid
unsignedlongthreads
unsignedlongparentprocessid
unsignedlongclassbase
unsignedlongflags
characterfilename[200]
end type

global type nvo_kill_process from nonvisualobject autoinstantiate
end type

type prototypes
Function Long GetCurrentProcessId() Library "kernel32.dll"
Function Long CreateToolhelp32Snapshot(Long Flags,Long ProcessId) Library "kernel32.dll"
Function Integer Process32First(uLong Snapshot,ref s_Process Process) Library "kernel32.dll"
Function Integer Process32Next(uLong Snapshot,ref s_Process Process) Library "kernel32.dll"
Function ulong TerminateProcess(long hProcess,ulong uExitCode) LIBRARY "kernel32.dll"
FUNCTION ulong OpenProcess(ulong dwDesiredAccess,ulong bInheritHandle,ulong dwProcessId) LIBRARY "kernel32.dll"

end prototypes

forward prototypes
public function integer of_kill_process (string as_pro)
end prototypes

public function integer of_kill_process (string as_pro);s_Process lst_Process //进程结构
String ls_FileName[],ls_CurExeName //最多100个进程,可改进
ulong ln_ProcessID,ln_Snapshot,ln_Circle,ln_Count //,ln_SameCount
string ls_Kill_File
Long ll_PID,ll_PID2

IF as_pro='' OR isnull(as_pro) Then Return 1

ln_ProcessID = GetCurrentProcessId() //取当前进程的ID

if IsNull(ln_ProcessID) or ln_ProcessID<1 then return -1 //出错则返回



ln_Snapshot = CreateToolhelp32Snapshot(2,0) //在堆上创建进程快照

if (ln_Snapshot<1) then return -1 //出错则返回

lst_Process.StructSize = 296 //Win32api的Process结构大小

if Process32First(ln_Snapshot,lst_Process)=0 then
return -1 //取第一个进程失败则返回
End IF

ls_Kill_File = lst_Process.FileName

IF pos(lower(as_pro),lower(ls_Kill_File))>0 Then
ll_pid=Long(lst_process.processid)
ll_pid2 = Long(OpenProcess(1,0,ll_PID))//使该进程可读写
TerminateProcess(ll_PID2,1) //Kill Process
End IF

if lst_Process.ProcessID=ln_ProcessID then //如果为当前进程,不允许删除
ls_CurExeName=lst_Process.FileName
END IF

do while true //循环取列举的进程名称
if Process32Next(ln_Snapshot,lst_Process)=0 then exit //列举完毕
IF lst_Process.ProcessID=ln_ProcessID Then continue //如果是当前进程.不执行下述杀除操作.
ls_Kill_File = lst_Process.FileName
IF pos(lower(as_pro),lower(ls_Kill_File))>0 Then
ll_pid=Long(lst_process.processid)
ll_pid2 = Long(OpenProcess(1,0,ll_PID))//使该进程可读写
TerminateProcess(ll_PID2,1) //Kill Process
End IF
loop

Return 1

end function

on nvo_kill_process.create
call super::create
TriggerEvent( this, "constructor" )
end on

on nvo_kill_process.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on



假设你通过run("c:\abc.exe")打开了外部程序,则可以这样结束该进程:
nvo_kill_process ln
ln.nvo_kill_process("abc.exe")

读书人网 >PB

热点推荐