pb关闭后台进程

转载链接:https://bbs.csdn.net/topics/380070870
将以下内容保存为本地文件nvo_kill_process.sru , 然后导入pbl中(在pbl上右击import)

$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
    unsignedlong        structsize
    unsignedlong        usage
    unsignedlong        processid
    unsignedlong        defaultheapid
    unsignedlong        moduleid
    unsignedlong        threads
    unsignedlong        parentprocessid
    unsignedlong        classbase
    unsignedlong        flags
    character        filename[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:\cc.exe”)打开了外部程序,则可以这样结束该进程:

nvo_kill_process ln
ln.of_kill_process("cc.exe")

本文若有侵权,请联系我删去

你可能感兴趣的:(pb)