我用SHELLEXECUTE启动一个程序,如果SHELLEXECUTE执行成功的话,能否利用它的返回值来关闭由它启动的那个程序?如果可以的话,怎么实现呢?多谢指教!
解决方案 »
- 怎么样把一个字符串复制道剪贴板上???急!!!!!在线等
- 帮帮忙,用VB6如何来解析XML文件?????
- OCX问题,在线等待,高分相送!
- 亲爱的朋友,你现在是否还在花父母的血汗钱?是否喜欢一样东西但不敢问津?是否有心爱的女孩,却因为囊中羞涩而失之交臂?
- 求助
- 通过什么函数可以得到计算机的ip地址,机器名?
- 请问各位大仙!如何利用ADO返回SQL Server的结果?急!急!急!
- 分组问题
- 怎么回事?打开这个网页时,屏幕左上角有一个圣诞老人!很Cool,怎么做的?
- 请问怎样在数据环境中添加SQL参数,参数的格式是怎样的?详细情况请点
- vb導入數據到word的表格中(急)
- 求助---托利多电子秤开发
Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwiseThe return value is cast as an HINSTANCE for backward compatibility with 16-bit Microsoft® Windows® applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an integer and compare it with the value 32 or one of the error codes below.意思是:执行成功的话返回大于32的整数.返回hinstance是为了向16位的 windows兼容,但它并不是真的hinstance,返回值只用于与32做比较用Shell 返回pid就可以操作了
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As LongPublic Sub KillProcess(ByVal pid As Long)
'Kill Process by pid
On Error GoTo ErrHandler
Dim mhandle As Long, ExitCode As Long
If pid > 0 Then
mhandle = OpenProcess(&H1F0FFF, 0, pid)
ExitCode = GetExitCodeProcess(mhandle, 0)
If ExitCode <> 0 Then
TerminateProcess mhandle, ExitCode
End If
End If
Exit Sub
ErrHandler:
End Sub
Private Sub Command1_Click()
Dim pid As Long
pid = 0
pid = Shell("notepad.exe", vbNormalFocus)
If pid <> 0 Then
Call KillProcess(pid)
End If
End Sub
Option ExplicitPrivate Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_SHOWNOACTIVATE = 4Private Sub Command1_Click()
ShellExecute vbNull, "open", "d:\1.txt", vbNullString, vbNullString, SW_SHOWNOACTIVATE
End SubPrivate Sub Command2_Click()
Dim hwnd As Long
Dim pID As Long
Dim pHandle As Long, exitCode As Long
On Error GoTo errMsg
hwnd = FindWindow(vbNullString, "1.txt - 记事本")
GetWindowThreadProcessId hwnd, pID
pHandle = OpenProcess(&H1F0FFF, False, pID)
exitCode = GetExitCodeProcess(pHandle, 0&)
If exitCode <> 0 Then
TerminateProcess pHandle, exitCode
End If
errMsg:
End Sub
再直接发WM_CLOSE就OK了。