注册 登录
编程论坛 VB6论坛

pid 在任务管理器看不到了 openprocess 为什么还返回非0?

czb27111111 发布于 2015-08-01 22:10, 601 次点击
R T
9 回复
#2
wmf20142015-08-03 10:36
给个例子好不?正常情况下,手动杀死进程后再用api刷新是看不到该进程的。
#3
lianyicq2015-08-03 10:52
回复 楼主 czb27111111
在任务管理器中能不能看到PID是和"查看"中的"选择列"设置有关.
分没分配序号是和进程存不存在有关。
#4
wp2319572015-08-03 11:15
广告是  神马北风网 还是北方网
#5
czb271111112015-08-04 13:29
有一个数组,储存着SHELL函数的返回值。
SHELL运行一个程序,运行完后会自动退出。
用一个TIMER检测程序是否退出了
我判断OPENPROCESS(process_all_access,false,数组中的一个数)是否为零来判断程序是不是退出了
可当程序运行完后openprocess还是返回非零的数
#6
czb271111112015-08-04 13:34
回复 2楼 wmf2014
用api刷新?
#7
czb271111112015-08-04 13:36
回复 2楼 wmf2014
用API刷新?
#8
lianyicq2015-08-04 15:18
试试这个
程序代码:
Const SYNCHRONIZE = &H100000
'一直等待
Const INFINITE = &HFFFF

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
  Dim lPid As Long
  Dim lHnd As Long
  Dim lRet As Long
  lPid = Shell("notepad.exe", vbNormalFocus)
  If lPid <> 0 Then
    lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
    If lHnd <> 0 Then
      lRet = WaitForSingleObject(lHnd, INFINITE)
      CloseHandle (lHnd)
    End If
    MsgBox "已终止.", vbInformation, "Shelled Application"
  End If
End Sub

#9
lianyicq2015-08-04 15:29
程序代码:
Option Explicit

Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long
Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

  Dim c As FILETIME
  Dim e As FILETIME
  Dim k As FILETIME
  Dim u As FILETIME
  Dim lPid As Long
  Dim lHnd As Long
  Dim lRet As Long

Private Sub Command1_Click()

  lPid = Shell("notepad.exe", vbNormalFocus)
  If lPid <> 0 Then
    lHnd = OpenProcess(PROCESS_ALL_ACCESS, 0, lPid)
    Form1.Caption = lHnd
    Timer1.Enabled = True
  End If
End Sub

Private Sub Timer1_Timer()
  Dim temp As Long
  temp = GetProcessTimes(lHnd, c, e, k, u)
  Text1.Text = temp & "," & e.dwHighDateTime & "," & e.dwLowDateTime
  If e.dwHighDateTime <> 0 Or e.dwLowDateTime <> 0 Then
    Form1.Caption = "完成"
    CloseHandle lHnd
    Timer1.Enabled = False
  End If
End Sub
也可以这样
#10
czb271111112015-08-08 10:37
回复 9楼 lianyicq
谢谢,我用
程序代码:

    If TheProcesses(0) <> 0 Then
        Dim i As Long
        For i = UBound(TheProcesses) To 0 Step -1
            Dim a As Long
            a = OpenProcess(PROCESS_ALL_ACCESS, False, TheProcesses(i))
            If a = 0 Then
                Call DeleteFormArray(TheProcesses, i)
            Else
                Dim CreationTime As FileTime, ExitTime As FileTime, KernelTime As FileTime, UserTime As FileTime
                If GetProcessTimes(a, CreationTime, ExitTime, KernelTime, UserTime) = 0 Then
                    MsgBox GetLastError
                    Call DeleteFormArray(TheProcesses, i)
                Else
                    If ExitTime.dwHighDateTime <> 0 And ExitTime.dwLowDateTime <> 0 Then
                        Call DeleteFormArray(TheProcesses, i)
                    End If
                End If
            End If
            Call CloseHandle(a)
        Next i
    End If

解决了
1