以下代碼在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

解决方案 »

  1.   

    这样再看看
    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
      

  2.   

    忘了声明了,我的环境是xp,类似于2000,不同于winme。