返回列表 发帖

[转载] 2000下获取进程的全路径

  1. Option Explicit
  2. '======================用于查找进程和终止进程的API函数常数定义================ =====
  3. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
  4. Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  5. Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  6. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  7. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  8. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  9. Const MAX_PATH As Integer = 260
  10. Private Type PROCESSENTRY32
  11. dwSize As Long
  12. cntUsage As Long
  13. th32ProcessID As Long
  14. th32DefaultHeapID As Long
  15. th32ModuleID As Long
  16. cntThreads As Long
  17. th32ParentProcessID As Long
  18. pcPriClassBase As Long
  19. dwFlags As Long
  20. szExeFile As String * MAX_PATH
  21. End Type
  22. Const TH32CS_SNAPheaplist = &H1
  23. Const TH32CS_SNAPPROCESS = &H2
  24. Const TH32CS_SNAPthread = &H4
  25. Const TH32CS_SNAPmodule = &H8
  26. Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule


  27. '======================在WIN2000下提升本进程权限的API函数常数定义=========== ==========
  28. Const STANDARD_RIGHTS_REQUIRED = &HF0000
  29. Const TOKEN_ASSIGN_PRIMARY = &H1
  30. Const TOKEN_DUPLICATE = (&H2)
  31. Const TOKEN_IMPERSONATE = (&H4)
  32. Const TOKEN_QUERY = (&H8)
  33. Const TOKEN_QUERY_SOURCE = (&H10)
  34. Const TOKEN_ADJUST_PRIVILEGES = (&H20)
  35. Const TOKEN_ADJUST_GROUPS = (&H40)
  36. Const TOKEN_ADJUST_DEFAULT = (&H80)
  37. Const TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or _
  38. TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or _
  39. TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
  40. Const SE_PRIVILEGE_ENABLED = &H2
  41. Const ANYSIZE_ARRAY = 1
  42. Private Type LUID
  43. lowpart As Long
  44. highpart As Long
  45. End Type
  46. Private Type LUID_AND_ATTRIBUTES
  47. pLuid As LUID
  48. Attributes As Long
  49. End Type
  50. Private Type TOKEN_PRIVILEGES
  51. PrivilegeCount As Long
  52. Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
  53. End Type
  54. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  55. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  56. Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
  57. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long


  58. '程序加载
  59. Private Sub Form_Load()
  60. AdjustTokenPrivileges2000
  61. Me.Caption = "WINDOWS 进程管理器"
  62. Command1.Caption = "刷新"
  63. Command2.Caption = "终止进程"
  64. Command3.Caption = "退出"
  65. ListView1.ColumnHeaders.Clear
  66. ListView1.ColumnHeaders.Add , "a", "进程ID", 800
  67. ListView1.ColumnHeaders.Add , "b", "进程名", 4900
  68. ListView1.View = lvwReport
  69. Command1_Click '刷新进程列表
  70. End Sub


  71. '显示当前系统中全部进程
  72. Private Sub Command1_Click()
  73. Dim i As Long, lPid As Long
  74. Dim Proc As PROCESSENTRY32
  75. Dim hSnapShot As Long
  76. ListView1.ListItems.Clear '清空ListView
  77. hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
  78. Proc.dwSize = Len(Proc)
  79. lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据
  80. i = 0
  81. Do While lPid <> 0 '当返回值非零时继续获取下一个进程
  82. ListView1.ListItems.Add , "a" & i, Proc.th32ProcessID & "(&H" & Hex(Proc.th32ProcessID) & ")" '将进程ID添加到ListView1第一列
  83. ListView1.ListItems("a" & i).SubItems(1) = Proc.szExeFile '将进程名添加到ListView1第二列
  84. i = i + 1
  85. lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据
  86. Loop
  87. CloseHandle hSnapShot '关闭进程“快照”句柄
  88. End Sub


  89. '终止指定进程
  90. Private Sub Command2_Click()
  91. Dim lPHand As Long, TMBack As Long
  92. If ListView1.SelectedItem.Text <> "" Then
  93. If MsgBox("确实要结束进程[" & ListView1.SelectedItem.SubItems(1) & "]吗?", vbYesNo) = vbYes Then
  94. lPHand = Val(ListView1.SelectedItem.Text)
  95. lPHand = OpenProcess(1&, True, lPHand) '获取进程句柄
  96. TMBack = TerminateProcess(lPHand, 0&) '关闭进程
  97. If TMBack <> 0 Then
  98. MsgBox ListView1.SelectedItem.SubItems(1) & "已经被终止!"
  99. Else
  100. MsgBox ListView1.SelectedItem.SubItems(1) & "不能被终止!"
  101. End If
  102. CloseHandle lPHand
  103. Command1_Click '刷新进程列表
  104. End If
  105. End If
  106. End Sub


  107. '退出本程序
  108. Private Sub Command3_Click()
  109. Unload Me
  110. End Sub


  111. '这个函数用于在WIN2000系统中,本进程提升权限
  112. Sub AdjustTokenPrivileges2000()
  113. Dim hdlProcessHandle As Long
  114. Dim hdlTokenHandle As Long
  115. Dim tmpLuid As LUID
  116. Dim tkp As TOKEN_PRIVILEGES
  117. Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  118. Dim lBufferNeeded As Long
  119. Dim lP As Long
  120. hdlProcessHandle = GetCurrentProcess()
  121. lP = OpenProcessToken(hdlProcessHandle, TOKEN_ALL_ACCESS, hdlTokenHandle)
  122. lP = LookupPrivilegeValue("", "SeDebugPrivilege", tmpLuid)
  123. tkp.PrivilegeCount = 1
  124. tkp.Privileges(0).pLuid = tmpLuid
  125. tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
  126. lP = AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
  127. End Sub


复制代码
天行健,君子以自强不息
地势坤,君子以厚德载物
黑色海岸线欢迎您

QQ群:7212260
致力于探索WEB技术精髓:http://www.bitechcn.com
点这里加我!

返回列表 回复 发帖