强制结束进程?

wlings 2005-09-15 03:03:09
想结束一个进程,查了一下api函数有ExitProcess()函数可以结束进程,但要输入参数uExitCode,是指定想中断的那个进程的一个退出代码,我只知道进程的映象名称,如QQ.exe,怎样可以得到进程的退出代码?试过一下用string ls_process='QQ',GetModuleHandle(ls_process)来得到,但返回值总是0,请大家帮帮忙,谢谢了!
...全文
184 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlings 2005-09-16
  • 打赏
  • 举报
回复
按照leio的稍作修改就可以了,谢谢大家
leio 2005-09-15
  • 打赏
  • 举报
回复
上面少写了一个API申明
Function ulong OpenProcess(ulong dwDesiredAccess,ulong bInheritHandle,ulong dwProcessId) Library "kernel32.dll"

leio 2005-09-15
  • 打赏
  • 举报
回复
第一步:先定义一个结构,如下:
$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
WorldMobile 2005-09-15
  • 打赏
  • 举报
回复


function boolean TerminateProcess(ulong hProcess, uint uExitCode) library "kernel32"

先找到进程的ID,然后调用这个API函数即可

找进行ID,还需要用到这几个API

CreateToolhelp32Snapshot
Process32First
Process32Next
OpenProcess

你查一下相关资料吧
langzicup 2005-09-15
  • 打赏
  • 举报
回复
没做过,帮你顶了!
wgsasd311 2005-09-15
  • 打赏
  • 举报
回复
up

680

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder API 调用
社区管理员
  • API 调用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧