再问在PB中终止其他进程的方法
再问在PB中终止其他进程的方法
楼主doubletoo(秋水)2004-03-25 14:18:11 在 PowerBuilder / API 调用 提问 请问,如何在pb中终止掉其他的进程。也就是说,再运行pb开发的程序时候,如何关闭掉其他应用程序(ps:也是用pb做的exe),最大限度只知道application的名字。
翻阅了FAQ,也搜索了以前的帖子,但,都是治标不治本的方法。请大家帮帮忙,最好有实例,或者有详细的说明。如果有范例更好,请发到doubletoo@163.com。谢谢 问题点数:100、回复次数:23Top
1 楼ldy(罗大佑)回复于 2004-03-25 15:05:35 得分 10
关注Top
2 楼iamxia()回复于 2004-03-25 16:36:43 得分 10
呵呵,用API吧,具体得查查看Top
3 楼zxthello(万有斥力)回复于 2004-03-25 17:41:42 得分 10
http://search.csdn.net/expert/topic/58/5801/2002/2/22/537440.htm
Top
4 楼doubletoo(秋水)回复于 2004-03-25 21:56:55 得分 0
to zxthello(万有斥力)
谢谢回复。
如果窗体名称未知,或者是变的,这个方法就不灵了。
另外,我在问题中说到,最大限度只知道application的名字Top
5 楼lzheng2001(1加1)回复于 2004-03-25 23:52:57 得分 0
获取系统进程,结束进程等API都有相关的函数,具体我要查查API资料才能答你.Top
6 楼doubletoo(秋水)回复于 2004-03-26 08:21:09 得分 0
谢谢 1+1,期待你的答案Top
7 楼doubletoo(秋水)回复于 2004-03-26 15:06:31 得分 0
分值不够,我再加。
晕,最多能给100,不能再加了。Top
8 楼lzheng2001(1加1)回复于 2004-03-26 15:31:58 得分 30
给你一个VB做的例子,这人例子是我以前收藏人家的.请参考其中的API类型声明有API声明:
"窗体中放置一个ListBox,三个Command,不用改名
"Command1 枚举本地进程
"Command2 杀掉在ListBox中选中的进程
"Command3 退出
Option Explicit
Dim ProcessID() As Long " 按list1中的进程顺序存储所有进程ID
"---------- API类型声明 -----------
Private Type PROCESSENTRY32 "进程
dwsize As Long
cntusage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 1024
End Type
Private Type MODULEENTRY32 "模块
dwsize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End Type
Private Type THREADENTRY32 "线程
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
"----------------------------------------- API声明 -------------------------------------------------------
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
"---------------------------------------- API常数声明 ------------------------------------------------------
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const PROCESS_TERMINATE = &H1&
Private Sub Command1_Click()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 " 通常用法
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 " 遍历所有进程直到返回值为False
List1.AddItem Trim(Process.szExeFile)
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 " 遍历所有进程直到返回值为False
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub
Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Command1_Click
Wend
End If
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Top
9 楼jdsnhan(柳荫凉)回复于 2004-03-26 23:53:02 得分 10
值得研究Top
10 楼feixianzhi(函数)回复于 2004-03-27 09:53:57 得分 10
Public Declare Sub ExitProcess Lib "kernel32" Alias "ExitProcess" (ByVal uExitCode As Long) //结束当前进程
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long //获得当前进程号
Public Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long //终止进程
不知道有没有适合的!
Top
11 楼sky2311(菜得不能再菜了!)回复于 2004-03-27 10:42:28 得分 10
关注
Top
12 楼doubletoo(秋水)回复于 2004-03-27 14:45:27 得分 0
to lzheng2001(1加1=0)
我比较笨 :(,能不能告诉我Pb的办法。
好象现在的问题是如果根据应用的名字找到processid。Top
13 楼lzheng2001(1加1)回复于 2004-03-27 19:21:36 得分 0
上面的API已经有足够的信息了,你如果不懂可以查查MSDN.日后有时间的话我会用PB做一个.Top
14 楼lzheng2001(1加1)回复于 2004-03-27 19:23:09 得分 0
上面的API有部分好象是不支持WIN98的!Top
15 楼cxwsoftware(方宇)回复于 2004-03-27 20:17:43 得分 10
关注Top
16 楼doubletoo(秋水)回复于 2004-03-27 22:20:24 得分 0
我还是不会,再请解释的详细一点。
解决会另开100分相赠。Top
17 楼doubletoo(秋水)回复于 2004-03-28 00:25:17 得分 0
to 1+1
我把以上的脚本转换成 Pb的
出现了一个问题
再调用Process32First 时,为什么等到process.szExeFile总是空字符串呢
调用Process32Next也是如此,没有向lb中插入任何东西.Top
18 楼lzheng2001(1加1)回复于 2004-03-28 17:57:41 得分 0
请把代码贴出来我帮你调试一下.Top
19 楼doubletoo(秋水)回复于 2004-03-29 08:53:59 得分 0
$PBExportHeader$w_process.srw
forward
global type w_process from jfc_w_master
end type
type cb_2 from jfc_uo_commandbutton within w_process
end type
type cb_1 from jfc_uo_commandbutton within w_process
end type
type lb_1 from listbox within w_process
end type
end forward
global type w_process from jfc_w_master
cb_2 cb_2
cb_1 cb_1
lb_1 lb_1
end type
global w_process w_process
type prototypes
Function Ulong CloseHandle (Ulong hObject ) LIBRARY "kernel32"
Function Ulong CreateToolhelp32Snapshot (Ulong dwFlags , Ulong th32ProcessID ) LIBRARY "kernel32"
Function Ulong Module32First (Ulong hSnapshot , MODULEENTRY32 lppe) LIBRARY "kernel32"
Function Ulong Module32Next (Ulong hSnapshot , MODULEENTRY32 lppe) LIBRARY "kernel32"
Function Boolean Process32First (Ulong hSnapshot, PROCESSENTRY32 lppe ) LIBRARY "kernel32"
Function Ulong Process32Next (Ulong hSnapshot, PROCESSENTRY32 lppe ) LIBRARY "kernel32"
Function Ulong Thread32First(Ulong hSnapshot, THREADENTRY32 lppe) LIBRARY "kernel32"
Function Ulong Thread32Next(Ulong hSnapshot , THREADENTRY32 lppe) LIBRARY "kernel32"
Function Ulong TerminateProcess (Ulong hProcess, Ulong uExitCode) LIBRARY "kernel32"
Function Ulong OpenProcess (Ulong dwDesiredAccess, Ulong bInheritHandle , Ulong dwProcessId) LIBRARY "kernel32"
Function Ulong GetCurrentProcessId() LIBRARY "kernel32"
Function Long GetLastError() Library "kernel32.dll"
end prototypes
type variables
Constant Long TH32CS_SNAPHEAPLIST = 1
Constant Long TH32CS_SNAPPROCESS = 2
Constant Long TH32CS_SNAPTHREAD = 4
Constant Long TH32CS_SNAPMODULE = 8
//Constant TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Constant Long TH32CS_INHERIT = 8
Constant Long PROCESS_TERMINATE = 1
end variables
on w_process.create
int iCurrent
call super::create
this.cb_2=create cb_2
this.cb_1=create cb_1
this.lb_1=create lb_1
iCurrent=UpperBound(this.Control)
this.Control=this.cb_2
this.Control=this.cb_1
this.Control=this.lb_1
end on
on w_process.destroy
call super::destroy
destroy(this.cb_2)
destroy(this.cb_1)
destroy(this.lb_1)
end on
type cb_2 from jfc_uo_commandbutton within w_process
integer x = 1525
integer y = 656
integer taborder = 20
end type
type cb_1 from jfc_uo_commandbutton within w_process
integer x = 1529
integer y = 381
integer taborder = 20
end type
event clicked;call super::clicked;PROCESSENTRY32 Process
Long ProcSnap
Long cntProcess
cntProcess = 0
lb_1.reset( )
//Process.dwSize = Len(Process)
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
messagebox("",procsnap)
If ProcSnap > 0 Then
Process.dwsize = 1060 // 通常用法
messagebox("",Process32First(ProcSnap,Process))
messagebox("error",GetLastError())
messagebox("",Process.szExeFile)
messagebox("",Process.th32ProcessID)
Do Until Process32Next(ProcSnap, Process) < 1 // 遍历所有进程直到返回值为False
lb_1.AddItem(Process.szExeFile)
cntProcess = cntProcess + 1
Loop
End If
CloseHandle(ProcSnap)
//
//ReDim ProcessID(cntProcess) As Long
Long ProcessID = Process.th32ProcessID
Loop
CloseHandle (ProcSnap)
end event
type lb_1 from listbox within w_process
integer x = 18
integer y = 32
integer width = 1456
integer height = 966
integer taborder = 10
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
boolean hscrollbar = true
boolean vscrollbar = true
borderstyle borderstyle = stylelowered!
end type
Top
20 楼doubletoo(秋水)回复于 2004-03-29 08:54:40 得分 0
请指正其中的错误Top
21 楼doubletoo(秋水)回复于 2004-03-30 10:19:09 得分 0
我还是没找到问题,期望 lzheng2001(1加1=0)尽早帮我解决,谢谢。
Top
22 楼lzheng2001(1加1)回复于 2004-04-01 15:47:48 得分 0
1. 加 PROCESSENTRY32 参数加 Ref 关键字
Function Boolean Process32First (Ulong hSnapshot, ref PROCESSENTRY32 lppe ) LIBRARY "kernel32"
Function Ulong Process32Next (Ulong hSnapshot,ref PROCESSENTRY32 lppe ) LIBRARY "kernel32"
2.修改string szexefile 为 char szexefile
global type processentry32 from structure
long dwsize
long cntusage
long th32processid
long th32defaultheapid
long th32moduleid
long cntthreads
long th32parentprocessid
long pcpriclassbase
long dwflags
char szexefile
end type
你把szexefile转化为String 即可得到进程名称
问题解决!
Top
23 楼renwanly(★★★★)回复于 2004-06-30 15:41:27 得分 0
收Top
-
相关文章
2秒记住本站域名
玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的readygo.com.cn
