我在自己的程序里调用了其他程序,但一段时间后又要关闭掉,我先用findwindow
再postmessage,但调用的程序标题会发生变化,这种方法不好。我又用openprocess后再terminateprocess,但好象实际上关闭不了这个进程,是我权限不够吗,还是程序有问题?请高手指教!!
再postmessage,但调用的程序标题会发生变化,这种方法不好。我又用openprocess后再terminateprocess,但好象实际上关闭不了这个进程,是我权限不够吗,还是程序有问题?请高手指教!!
解决方案 »
- ADODB.Recordset修改了数据后为什么重新Open也不会消失?
- 谁用过并用VB开发过PI(普爱)的M-110.1DG运动台与C-863控制器啊
- 鸟枪换炮了,散分~
- 如何制作倒计时?
- 向高手虚心请教:在VB中,如何调用SQL数据库???
- 用winsock怎样实现服务器端和客户端之间传送文件(新手)
- 怎么创建dll?创建dll时参数应该怎么定义?创建后怎么调用?在哪儿声明?
- 我发觉DataGrid和oledb3.51能连接上,但是和4.0就是不能连上!
- 中秋节散分活动1
- vb对excel的操作
- 在VB中如何创建帮助文档,还是需要其它软件?
- 求救,急!关于串口通讯 MSCOMM 控件的使用的几个问题!
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long'ExitProcess GetExitCodeProcess(GetCurrentProcess, 0 )
'关闭EXE文件 PROID=进程句柄
Public Function CloseExe(ProID As Long)
Dim Rc As Long
'中断一个进程
Call TerminateProcess(ProID, 0)
'关闭该进程
Call CloseHandle(ProID)
End Function
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_TERMINATE = &H1
Dim hProcess As LongPrivate Sub Command1_Click()
Dim pid As Long
pid = Shell("notepad", 1)
hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, 0, pid) 'PROCESS_TERMINATE为必须
End SubPrivate Sub Command2_Click()
TerminateProcess hProcess, 0
CloseHandle hProcess
End Sub
再postmessage,但调用的程序标题会发生变化,这种方法不好既然是自己调用的,可以根据pid直接获得窗口句柄,不需要用窗口标题:
Option Explicit
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2
Const WM_CLOSE = &H10
Dim mhWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Command1_Click()
Dim Pid As Long
Pid = Shell("notepad", 1)
mhWnd = InstanceToWnd(Pid)
End SubPrivate Sub Command2_Click()
PostMessage mhWnd, WM_CLOSE, 0&, ByVal 0&
End Sub
http://expert.csdn.net/Expert/topic/2820/2821000.xml?temp=.2655451