• 按键公众号 :
按键精灵电脑版
立即下载

软件版本:2014.06
软件大小:22.9M
更新时间:2021-12-03

按键精灵安卓版
立即下载

软件版本:3.7.2
软件大小:46.2M
更新时间:2023-05-10

按键精灵iOS版
立即下载

软件版本:1.8.0
软件大小:29.2M
更新时间:2023-03-21

按键手机助手
立即下载

软件版本:3.8.0
软件大小:262M
更新时间:2023-05-30

快捷导航

登录 后使用快捷导航
没有帐号? 注册

发新话题 回复该主题

[笨蛋熊] 【按键X】实现一个功能健全的进程管理器(含源代码) [复制链接]

1#
按键X提供了让人惊叹的扩展能力,除了按键2014就支持的COM插件外,还支持Lua插件、Win32SDK API 插件、C语言的 Lua mod 插件。

按键X最初发布的时候,我和神梦、山海师等人设计开发了按键星月扩展库:http://bbs.anjian.com/showtopic-664233-1.aspx

这里展示一下这个扩展库的能力,主要是进程处理部分,也展示一下按键X的扩展调用能力。



先上图最终成品效果图:







接下来解析源代码:

要使用xyAPI扩展库,需要先将 xyapi.dll 放入脚本的附件目录内,然后在脚本里声明相应的功能,这里只声明进程相关的功能:
  1. // 进程库
  2. Declare Function Process_Is64 Lib "xyapi.dll" (ByVal pHdr As Long) As Long
  3. Declare Function PID_Is64 Lib "xyapi.dll" (ByVal pid As Long) As Long
  4. Declare Function Process_CmdLine Lib "xyapi.dll" (ByVal pHdr As Long) As String
  5. Declare Function PID_CmdLine Lib "xyapi.dll" (ByVal pid As Long) As String
  6. Declare Function Process_CurPath Lib "xyapi.dll" (ByVal pHdr As Long) As String
  7. Declare Function PID_CurPath Lib "xyapi.dll" (ByVal pid As Long) As String
  8. Declare Function Process_GetPath Lib "xyapi.dll" (ByVal pHdr As Long) As String
  9. Declare Function PID_GetPath Lib "xyapi.dll" (ByVal pid As Long) As String
  10. Declare Function Process_Parent Lib "xyapi.dll" (ByVal pHdr As Long) As Long
  11. Declare Function PID_Parent Lib "xyapi.dll" (ByVal pid As Long) As Long
  12. Declare Function Process_H2ID Lib "xyapi.dll" (ByVal pHdr As Long) As Long
  13. Declare Function Process_List Lib "xyapi.dll" () As String
  14. Declare Function Process_FindChild Lib "xyapi.dll" (ByVal pHdr As Long) As Long
  15. Declare Function PID_FindChild Lib "xyapi.dll" (ByVal pid As Long) As Long
  16. Declare Function Process_ListChild Lib "xyapi.dll" (ByVal pHdr As Long) As String
  17. Declare Function PID_ListChild Lib "xyapi.dll" (ByVal pid As Long) As String
  18. Declare Function Process_Status Lib "xyapi.dll" (ByVal pHdr As Long) As Long
  19. Declare Function PID_Status Lib "xyapi.dll" (ByVal pid As Long) As Long
  20. Declare Function Process_Kill Lib "xyapi.dll" (ByVal pHdr As Long) As Long
  21. Declare Function PID_Kill Lib "xyapi.dll" (ByVal pid As Long) As Long
复制代码
声明完成后,这些功能就都可以使用了,PID_ 开头的命令使用进程PID来控制进程,Process_ 开头的命令则使用进程句柄控制进程。

之后,在窗口加载时,我们需要将进程列表显示出来,我们先实现进程列表刷新的功能,然后在窗口加载时调用这个功能即可:
  1. // 窗口加载
  2. Event Form1.Load
  3.     Form1.Button1.Click()
  4. End Event

  5. // 刷新列表
  6. Event Form1.Button1.Click
  7.     Dim PList = Split(Process_List(), "\r\n")
  8.     Form1.Grid1.RowCount = UBound(PList) + 1
  9.     For i = 1 To UBound(PList)
  10.         Dim PInfo = Split(PList(i), "|")
  11.         Form1.Grid1.SetItemText(i, 0, PInfo(1))
  12.         Form1.Grid1.SetItemText(i, 1, PInfo(2))
  13.         Form1.Grid1.SetItemText(i, 2, iif(PID_Is64(CLng(PInfo(2))) = 0, "32位", "64位"))
  14.         Form1.Grid1.SetItemText(i, 3, PInfo(3))
  15.         Form1.Grid1.SetItemText(i, 4, PInfo(4))
  16.         Form1.Grid1.SetItemText(i, 5, CStr(PID_Parent(CLng(PInfo(2)))))
  17.     Next
  18. End Event
复制代码
进程列表使用 Process_List 命令获取,他会返回使用 \r\n 分割的进程列表信息,每行一个进程,进程内的信息则用 | 分割。
将信息分割后,我们就可以把进程列表展示出来了。

进程信息里面保存着进程PID,也就是说我们拿到这个列表之后,就可以控制这个列表的进程了,比如获取更详细的信息,或者结束进程、定位文件等。

下面是获取进程信息的代码:
  1. // 进程信息
  2. Event Form1.Button4.Click
  3.     Dim LineE, PID
  4.     PID, PID, LineE, PID = Form1.Grid1.GetSelectedRange()
  5.     PID = CLng(Form1.Grid1.GetItemText(LineE, 1))
  6.     Dim PInfo = "进程信息\r\n\r\n映像名:\t" & Form1.Grid1.GetItemText(LineE, 0)
  7.     PInfo = PInfo & "\r\n进程PID:\t" & PID
  8.     PInfo = PInfo & "\r\n父进程PID:\t" & PID_Parent(PID)
  9.     PInfo = PInfo & "\r\n进程位数:\t" & iif(PID_Is64(PID) = 0, "32位", "64位")
  10.     PInfo = PInfo & "\r\n进程优先级:\t" & Form1.Grid1.GetItemText(LineE, 3)
  11.     PInfo = PInfo & "\r\n线程数量:\t" & Form1.Grid1.GetItemText(LineE, 4)
  12.     PInfo = PInfo & "\r\n进程启动命令行:" & PID_CmdLine(PID)
  13.     PInfo = PInfo & "\r\n进程工作目录:\t" & PID_CurPath(PID)
  14.     PInfo = PInfo & "\r\n进程文件路径:\t" & PID_GetPath(PID)
  15.     PInfo = PInfo & "\r\n进程状态:\t" & iif(PID_Status(PID) = 0, "已结束", "正在运行")
  16.     PInfo = PInfo & "\r\n\r\n子进程列表:\r\n\r\n映像名\t\tPID\t优先级\t线程数量\r\n\r\n" & Replace(PID_ListChild(PID), "|", "\t")
  17.     MsgBox PInfo
  18. End Event
复制代码
xyAPI 支持获取的进程信息还是比较多的,其中很多信息是从 PBI 表里面拿出来的,和系统任务管理器的原理一样,同时支持64位进程和32位进程。

进程信息里可以获取到文件路径,所以我们也可以定位到文件的位置:
  1. // 定位文件
  2. Event Form1.Button5.Click
  3.     Dim LineE, PID
  4.     PID, PID, LineE, PID = Form1.Grid1.GetSelectedRange()
  5.     PID = CLng(Form1.Grid1.GetItemText(LineE, 1))
  6.     RunApp "explorer /n,/select," & PID_GetPath(PID)
  7. End Event

  8. // 定位工作目录
  9. Event Form1.Button6.Click
  10.     Dim LineE, PID
  11.     PID, PID, LineE, PID = Form1.Grid1.GetSelectedRange()
  12.     PID = CLng(Form1.Grid1.GetItemText(LineE, 1))
  13.     RunApp "explorer /n,/root," & PID_CurPath(PID)
  14. End Event
复制代码
当然也可以结束进程或者结束进程树:
  1. // 结束进程树
  2. Event Form1.Button3.Click
  3.     Dim LineE, PID
  4.     PID, PID, LineE, PID = Form1.Grid1.GetSelectedRange()
  5.     PID = CLng(Form1.Grid1.GetItemText(LineE, 1))
  6.     Dim PList = Split(PID_ListChild(PID), "\r\n")
  7.     For i = 1 To UBound(PList)
  8.         Dim PInfo = Split(PList(i), "|")
  9.         PID_Kill(CLng(PInfo(2)))
  10.     Next
  11.     PID_Kill(PID)
  12.     MsgBox "进程结束完毕"
  13. End Event

  14. // 结束进程
  15. Event Form1.Button2.Click
  16.     Dim LineE, PID
  17.     PID, PID, LineE, PID = Form1.Grid1.GetSelectedRange()
  18.     PID = CLng(Form1.Grid1.GetItemText(LineE, 1))
  19.     PID_Kill(PID)
  20.     MsgBox "进程结束完毕"
  21. End Event
复制代码
结束进程树功能使用 PID_ListChild 遍历子进程,然后结束所有子进程,对于脚本开发来说,这个功能也是很有用的,比如运行起来一个程序之后,这个程序还会打开其他乱七八糟的东西,直接结束掉这个进程,它的子进程就残留住了,可以遍历一下,然后全部杀掉!



最终制作完成的小精灵下载:

PM.zip (, 下载次数:213)




按键精灵X版任务管理器源代码下载:

PMsrc.zip (, 下载次数:160)

发新话题 回复该主题