以下代碼在Win2000下沒間題<但也不徹底>,在WinMe下運行時,一按下Command1(執行Command1_click())時就導致死機,是什麼原因呢?
原本的目的是:用他強行關掉當前Winme下已開啟的所有程式
看貼:http://expert.csdn.net/Expert/topic/1686/1686833.xml?temp=.7309839
我是照抄 JennyVenus() :Option ExplicitPrivate Sub Command1_Click()
EnumWindows AddressOf MyEnumProc, ByVal 0&
End Sub------------------------------------
Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
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
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Const PROCESS_TERMINATE = &H1
'Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long''用于结束外部进程,hCloseWnd 是要结束的程序的主窗口的 HWND
Public Function TerminateProcessByHWND(ByVal hCloseWnd As Long) As Boolean
Dim hProcessID As Long
Dim hProcess As Long
On Error GoTo PROC_EXIT
If hCloseWnd = 0 Then GoTo PROC_EXIT
If GetWindowThreadProcessId(hCloseWnd, hProcessID) = 0 Then GoTo PROC_EXIT
hProcess = OpenProcess(PROCESS_TERMINATE, False, hProcessID)
If hProcess = 0 Then GoTo PROC_EXIT
If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT
TerminateProcessByHWND = True
PROC_EXIT:
If Err.Number <> 0 Then
Debug.Print Err.Description
Err.Clear
End If
End Function
#If 0 Then
Public Function remove0(ByVal src As String) As String
Dim i As Integer
Dim s As String
For i = 1 To Len(src)
If Mid(src, i, 1) <> Chr(0) Then
s = s & Mid(src, i, 1)
Else
Exit For
End If
Next i
remove0 = s
End Function
#End If
Public Function MyEnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
If hwnd <> Form1.hwnd Then
TerminateProcessByHWND hwnd
End If
MyEnumProc = 1
End Function
原本的目的是:用他強行關掉當前Winme下已開啟的所有程式
看貼:http://expert.csdn.net/Expert/topic/1686/1686833.xml?temp=.7309839
我是照抄 JennyVenus() :Option ExplicitPrivate Sub Command1_Click()
EnumWindows AddressOf MyEnumProc, ByVal 0&
End Sub------------------------------------
Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
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
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Const PROCESS_TERMINATE = &H1
'Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long''用于结束外部进程,hCloseWnd 是要结束的程序的主窗口的 HWND
Public Function TerminateProcessByHWND(ByVal hCloseWnd As Long) As Boolean
Dim hProcessID As Long
Dim hProcess As Long
On Error GoTo PROC_EXIT
If hCloseWnd = 0 Then GoTo PROC_EXIT
If GetWindowThreadProcessId(hCloseWnd, hProcessID) = 0 Then GoTo PROC_EXIT
hProcess = OpenProcess(PROCESS_TERMINATE, False, hProcessID)
If hProcess = 0 Then GoTo PROC_EXIT
If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT
TerminateProcessByHWND = True
PROC_EXIT:
If Err.Number <> 0 Then
Debug.Print Err.Description
Err.Clear
End If
End Function
#If 0 Then
Public Function remove0(ByVal src As String) As String
Dim i As Integer
Dim s As String
For i = 1 To Len(src)
If Mid(src, i, 1) <> Chr(0) Then
s = s & Mid(src, i, 1)
Else
Exit For
End If
Next i
remove0 = s
End Function
#End If
Public Function MyEnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
If hwnd <> Form1.hwnd Then
TerminateProcessByHWND hwnd
End If
MyEnumProc = 1
End Function
解决方案 »
- 请各位看看这两幅图,问题很清楚了
- 怎么样实现在打开文件对话框里一次选择多个文件? 急! 谢谢!
- 请教高手,如果用代码关闭一个正在运行的exe程序
- 怎么利用安装到电脑中的软件,制作安装盘?(原来的代码和安装盘已经找不到,但是电脑中安装有)
- 一个小问题,但急用,请恢复
- me.hide是隐藏form,我要彻底关掉一个form该怎样做呢?谢谢!马上给分!
- 请教关于使用DSN访问远程Access数据库的问题。
- 我用EXCEL作好一个模板后,忘记录制宏,现在我要向这个EXCEL插值,请问我现在该如何办?我现在不想重做,太麻烦了.那位高手帮我?
- 请问ALT+F4的组合键怎么实现?
- 怎样知道某周是一年中的第周?
- 各位大哥:请问VB现在在成都能得多少K的工资呀,还有ASP的!
- 关于事件冒泡的问题
Option ExplicitPrivate Sub Command1_Click()
EnumWindows AddressOf MyEnumProc, ByVal 0&
End Sub
------------------------------------------------Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
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
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Public Const GWL_STYLE = (-16)
Public Const WS_VISIBLE = &H10000000
'Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long''用于结束外部进程,hCloseWnd 是要结束的程序的主窗口的 HWND
Public Function TerminateProcessByHWND(ByVal hCloseWnd As Long) As Boolean
Dim hProcessID As Long
Dim hProcess As Long
On Error GoTo PROC_EXIT
If hCloseWnd = 0 Then GoTo PROC_EXIT
If GetWindowThreadProcessId(hCloseWnd, hProcessID) = 0 Then GoTo PROC_EXIT
hProcess = OpenProcess(PROCESS_TERMINATE, False, hProcessID)
If hProcess = 0 Then GoTo PROC_EXIT
If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT
TerminateProcessByHWND = True
PROC_EXIT:
If Err.Number <> 0 Then
Debug.Print Err.Description
Err.Clear
End If
End Function
Public Function remove0(ByVal src As String) As String
Dim i As Integer
Dim s As String
For i = 1 To Len(src)
If Mid(src, i, 1) <> Chr(0) Then
s = s & Mid(src, i, 1)
Else
Exit For
End If
Next i
remove0 = s
End Function
Public Function MyEnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
If hwnd <> Form1.hwnd Then
'Dim sy As Long
'sy = GetWindowLong(hwnd, GWL_STYLE)
'If (sy And WS_VISIBLE) = WS_VISIBLE Then
Dim s As String * 255
Dim ss As String
GetClassName hwnd, s, 255
ss = remove0(s)
If ss <> "Shell_TrayWnd" Or ss <> "Progman" Then
TerminateProcessByHWND hwnd
End If
'End If
End If
MyEnumProc = 1
End Function