第一步:先定义一个结构,如下:
$PBExportHeader$s_process.srs
global 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[260]
end type
第二步:申明API,如下:
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 Boolean TerminateProcess (ulong hProcess, uint uExitCode) Library "kernel32.dll"
第三步:编写一个函数
$PBExportHeader$f_getprocessid.srf
global type f_getprocessid from function_object
end type
forward prototypes
global function unsignedlong f_getprocessid (string as_exename)
end prototypes
global function unsignedlong f_getprocessid (string as_exename);//Create by Leio 2005.9.15
//根据指定的文件名查找进程号,找到则返回进程号,否则返回0
s_Process lst_Process
ulong ul_Snapshot
ulong ul_processid = 0
ul_Snapshot = CreateToolhelp32Snapshot(2,0)
if (ul_Snapshot < 1) then return 0
lst_Process.StructSize = 296
if Process32First(ul_Snapshot,lst_Process) = 0 then return 0
do while true
if lst_Process.FileName = as_exename then
ul_processid = lst_Process.ProcessID
exit
end if
if Process32Next(ul_Snapshot,lst_Process) = 0 then exit
loop
return ul_processid
end function
第四步:写脚本如下
uint lui_exitcode
ulong lul_processid,lul_mpid
boolean lb_return
lul_processid = f_getprocessid('QQ.EXE')
if lul_processid > 0 then
lul_mpid = openprocess(1,-1,lul_processid)
lb_return = TerminateProcess(lul_mpid, lui_exitcode)
if lb_return then
messagebox('信息','QQ.EXE已被终止运行。',Information!)
end if
end if