我用shellexecute调用了一个应用程序,如何在V B里得到这个程序的PID呢?
我用VB编制了一个程序,需要调用的应用程序执行完毕(这个应用程序执行完毕后会自动关闭)后才执行VB程序的shellexecute的下一个语句。我查了一下,要判断调用程序执...
我用VB编制了一个程序,需要调用的应用程序执行完毕(这个应用程序执行完毕后会自动关闭)后才执行VB程序的shellexecute的下一个语句。
我查了一下,要判断调用程序执行是否完成的方法都要用到调用程序的PID,但shellexecute不像shell函数会返回调用程序的PID.那么有什么方法得到正在运行的某个程序的PID呢?
或者有什么其它办法判断shellexecute调用的程序已经运行结束。 展开
我查了一下,要判断调用程序执行是否完成的方法都要用到调用程序的PID,但shellexecute不像shell函数会返回调用程序的PID.那么有什么方法得到正在运行的某个程序的PID呢?
或者有什么其它办法判断shellexecute调用的程序已经运行结束。 展开
2个回答
展开全部
shellexecute确实无法获取PID,采用下面两种方法可以得到正在运行的某个程序的PID:
方法一:
一、把下面代码复制到某个模块中:
Function GetPid(ExeName As String) As Long
On Error Resume Next
Dim objWMIService, colProcessList, objProcess
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & ExeName & "'")
For Each objProcess In colProcessList
GetPid = objProcess.Handle
Exit For
Next
Set objProcess = Nothing
Set colProcessList = Nothing
Set objWMIService = Nothing
End Function
二、调用举例:
dim PID as long
ShellExecute 0&, "open", "calc.exe", 0, 0, 1
PID=GetPid("calc.exe")
MsgBox PID
方法二(推荐):
一、把下面代码复制到某个模块中:
'声明
Private Declare Function FindExecutable Lib _
"shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory _
As String, ByVal lpResult As String) As Long
'自定义函数
Public Function SpeShellExecute(FileName As String) As Long
Dim Ret As Integer
Dim Bfr As String
Dim ExPath As String
Dim Ext As String
If Dir(FileName) = "" Or FileName = "" Then
Exit Function
End If
Bfr = String(300, 32)
Ext = Right$(FileName, 3)
If Ext = "exe" Or Ext = "com" Or Ext = "bat" Then
SpeShellExecute = Shell(FileName, vbNormalFocus)
Else
Ret = FindExecutable(FileName, vbNullString, Bfr)
If Ret > 32 Then
ExPath = Left$(Bfr, InStr(Bfr, Chr$(0)) - 1)
SpeShellExecute = Shell(ExPath & " " & FileName, vbNormalFocus)
Else
MsgBox "No association for file type:" & UCase(Ext)
End If
End If
End Function
二、调用举例:
dim PID as long
PID=SpeShellExecute("c:\1.jpg") '用SpeShellExecute代替ShellExecute!
MsgBox PID
方法一:
一、把下面代码复制到某个模块中:
Function GetPid(ExeName As String) As Long
On Error Resume Next
Dim objWMIService, colProcessList, objProcess
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & ExeName & "'")
For Each objProcess In colProcessList
GetPid = objProcess.Handle
Exit For
Next
Set objProcess = Nothing
Set colProcessList = Nothing
Set objWMIService = Nothing
End Function
二、调用举例:
dim PID as long
ShellExecute 0&, "open", "calc.exe", 0, 0, 1
PID=GetPid("calc.exe")
MsgBox PID
方法二(推荐):
一、把下面代码复制到某个模块中:
'声明
Private Declare Function FindExecutable Lib _
"shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory _
As String, ByVal lpResult As String) As Long
'自定义函数
Public Function SpeShellExecute(FileName As String) As Long
Dim Ret As Integer
Dim Bfr As String
Dim ExPath As String
Dim Ext As String
If Dir(FileName) = "" Or FileName = "" Then
Exit Function
End If
Bfr = String(300, 32)
Ext = Right$(FileName, 3)
If Ext = "exe" Or Ext = "com" Or Ext = "bat" Then
SpeShellExecute = Shell(FileName, vbNormalFocus)
Else
Ret = FindExecutable(FileName, vbNullString, Bfr)
If Ret > 32 Then
ExPath = Left$(Bfr, InStr(Bfr, Chr$(0)) - 1)
SpeShellExecute = Shell(ExPath & " " & FileName, vbNormalFocus)
Else
MsgBox "No association for file type:" & UCase(Ext)
End If
End If
End Function
二、调用举例:
dim PID as long
PID=SpeShellExecute("c:\1.jpg") '用SpeShellExecute代替ShellExecute!
MsgBox PID
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询