pb 怎么判断一个.exe文件已经在系统里运行?如何关闭一个已经运行的.exe?
pb 怎么判断一个.exe文件已经在系统里运行?如何关闭一个已经运行的.exe?
如题。
[解决办法]
比较麻烦一些:
先构建一个用户对象:
forward
global type uo_get_exename from nonvisualobject
end type
type s_process from structure within uo_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 uo_get_exename from nonvisualobject
end type
global uo_get_exename uo_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);//构造对象uo_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 uo_get_exename.create
call super::create
TriggerEvent( this, "constructor" )
end on
on uo_get_exename.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on
然后在你要判断的事件中写:
//用于杀掉的若干个个进程
uo_get_exename luf_get_exename
INTEGER li_rc,j
ULONG ul_PID
ULONG PROCESS_TERMINATE = 0001
ULONG hwdprocess
String ls_exeName[]
ls_exeName[1] = "app.exe" //要结束的进程名
ls_exeName[2] = "app1.exe"
//If messagebox('结束进程','确定吗?Kill?',question!,yesno!,1) = 2 Then return
//创建实例变量
luf_get_exename = create uo_get_exename
for j = 1 to 2 //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;
试试,上面的用户对象代码可以剪贴到你的PB原码编辑器上,存为.sru文件,再通过库管理界面Import即可。