如何在VB中关闭一个指定的命令行窗口

ghw 2008-03-27 11:06:53
我想在VB中关闭一个指定标题的命令行窗口(这个命令行窗口可能正在运行一个程序),应该怎么做?
请大家帮帮忙,提前谢谢啦!
...全文
348 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
东方之珠 2008-03-28
sendkeys exit
  • 打赏
  • 举报
回复
cbm6666 2008-03-28
指定标题 计算器, 你先运行 calc.exe

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Dim hProcess&
Private Sub Command1_Click()
hProcess = FindWindow(vbNullString, "计算器")
If hProcess > 0 Then SendMessage hProcess, WM_CLOSE, 0, 0
End Sub

'睡觉去啦...........

  • 打赏
  • 举报
回复
cbm6666 2008-03-28
呵呵, 就是这个思路, 建立 "病毒库列表" , 自动关掉进程

【CBM666 的木马EXE专杀】
http://download.csdn.net/source/358139

主要是随时检测进程中有没有指定列表中的exe, 发现了便停止它的进程并干掉这个exe文件.

楼主要的话请留下邮箱, CSDN上传的资源最近都无法下载了.
  • 打赏
  • 举报
回复
东方之珠 2008-03-28
CloseWindow(hwnd)。hwnd是找到指定窗口的句柄。
  • 打赏
  • 举报
回复
东方之珠 2008-03-28
楼上正解。
  • 打赏
  • 举报
回复
barenx 2008-03-28
Public Sub CloseWindow(ByRef WinText() As String)
将要关闭的标题数组传入 便可以关闭所有含有指定关键词的窗口 连卡巴斯基都可以。。。。
  • 打赏
  • 举报
回复
barenx 2008-03-28

Attribute VB_Name = "Enum_Window"
Option Explicit
Option Base 0
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Const LWA_COLORKEY = &H1
Public Const LWA_ALPHA = &H2
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const WS_EX_TRANSPARENT As Long = &H20&
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public 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
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const WM_CLOSE = &H10
Public Const WM_QUIT = &H12
Public Const MAX_PATH As Integer = 260

'***************************************************************************************************************
Public Sub CloseWindow(ByRef WinText() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
For i = 0 To UBound(WinText)
If InStr(1, UCase$(StrName), UCase$(WinText(i))) > 0 Then
PostMessage lngHand, &H11, 0, 0 'public Const WM_QUERYENDSESSION = &H11
PostMessage lngHand, WM_QUIT, 0, 0
PostMessage lngHand, &H16, 0, 0 'public Const WM_ENDSESSION = &H16
PostMessage lngHand, WM_QUIT, 0, 0
End If
Next i
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub

Public Sub GetWindowsList(ByRef WindowsName() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
i = 0
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
ReDim Preserve WindowsName(i)
WindowsName(i) = StrName
Debug.Print StrName
i = i + 1
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub
Public Function StrCutNull(IStr As String) As String
Dim i As Long
i = InStr(1, IStr, Chr(0))
If i <= 1 Then
StrCutNull = vbNullString
Else
StrCutNull = Left$(IStr, i - 1)
End If
End Function

  • 打赏
  • 举报
回复
barenx 2008-03-28

Attribute VB_Name = "Enum_Window"
Option Explicit
Option Base 0
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Const LWA_COLORKEY = &H1
Public Const LWA_ALPHA = &H2
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const WS_EX_TRANSPARENT As Long = &H20&
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public 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
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const WM_CLOSE = &H10
Public Const WM_QUIT = &H12
Public Const MAX_PATH As Integer = 260

'***************************************************************************************************************
Public Sub CloseWindow(ByRef WinText() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
For i = 0 To UBound(WinText)
If InStr(1, UCase$(StrName), UCase$(WinText(i))) > 0 Then
PostMessage lngHand, &H11, 0, 0 'public Const WM_QUERYENDSESSION = &H11
PostMessage lngHand, WM_QUIT, 0, 0
PostMessage lngHand, &H16, 0, 0 'public Const WM_ENDSESSION = &H16
PostMessage lngHand, WM_QUIT, 0, 0
End If
Next i
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub

Public Sub GetWindowsList(ByRef WindowsName() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
i = 0
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
ReDim Preserve WindowsName(i)
WindowsName(i) = StrName
Debug.Print StrName
i = i + 1
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub
Public Function StrCutNull(IStr As String) As String
Dim i As Long
i = InStr(1, IStr, Chr(0))
If i <= 1 Then
StrCutNull = vbNullString
Else
StrCutNull = Left$(IStr, i - 1)
End If
End Function

  • 打赏
  • 举报
回复
barenx 2008-03-28

Attribute VB_Name = "Enum_Window"
Option Explicit
Option Base 0
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Const LWA_COLORKEY = &H1
Public Const LWA_ALPHA = &H2
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const WS_EX_TRANSPARENT As Long = &H20&
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public 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
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const WM_CLOSE = &H10
Public Const WM_QUIT = &H12
Public Const MAX_PATH As Integer = 260

'***************************************************************************************************************
Public Sub CloseWindow(ByRef WinText() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
For i = 0 To UBound(WinText)
If InStr(1, UCase$(StrName), UCase$(WinText(i))) > 0 Then
PostMessage lngHand, &H11, 0, 0 'public Const WM_QUERYENDSESSION = &H11
PostMessage lngHand, WM_QUIT, 0, 0
PostMessage lngHand, &H16, 0, 0 'public Const WM_ENDSESSION = &H16
PostMessage lngHand, WM_QUIT, 0, 0
End If
Next i
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub

Public Sub GetWindowsList(ByRef WindowsName() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
i = 0
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
ReDim Preserve WindowsName(i)
WindowsName(i) = StrName
Debug.Print StrName
i = i + 1
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub
Public Function StrCutNull(IStr As String) As String
Dim i As Long
i = InStr(1, IStr, Chr(0))
If i <= 1 Then
StrCutNull = vbNullString
Else
StrCutNull = Left$(IStr, i - 1)
End If
End Function

  • 打赏
  • 举报
回复
barenx 2008-03-28

Attribute VB_Name = "Enum_Window"
Option Explicit
Option Base 0
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Const LWA_COLORKEY = &H1
Public Const LWA_ALPHA = &H2
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const WS_EX_TRANSPARENT As Long = &H20&
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public 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
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const WM_CLOSE = &H10
Public Const WM_QUIT = &H12
Public Const MAX_PATH As Integer = 260

'***************************************************************************************************************
Public Sub CloseWindow(ByRef WinText() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
For i = 0 To UBound(WinText)
If InStr(1, UCase$(StrName), UCase$(WinText(i))) > 0 Then
PostMessage lngHand, &H11, 0, 0 'public Const WM_QUERYENDSESSION = &H11
PostMessage lngHand, WM_QUIT, 0, 0
PostMessage lngHand, &H16, 0, 0 'public Const WM_ENDSESSION = &H16
PostMessage lngHand, WM_QUIT, 0, 0
End If
Next i
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub

Public Sub GetWindowsList(ByRef WindowsName() As String)
On Error Resume Next
Dim i As Long, lngDeskTopHandle As Long, lngHand As Long, StrName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
i = 0
Do While lngHand <> 0
StrName = Space(MAX_PATH)
GetWindowText lngHand, StrName, MAX_PATH
StrName = StrCutNull(Trim$(StrName))
If StrName <> vbNullString Then
ReDim Preserve WindowsName(i)
WindowsName(i) = StrName
Debug.Print StrName
i = i + 1
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
CloseHandle lngDeskTopHandle
CloseHandle lngHand
End Sub
Public Function StrCutNull(IStr As String) As String
Dim i As Long
i = InStr(1, IStr, Chr(0))
If i <= 1 Then
StrCutNull = vbNullString
Else
StrCutNull = Left$(IStr, i - 1)
End If
End Function


  • 打赏
  • 举报
回复
ghw 2008-03-27
自己顶一下,高手们睡觉了吗
  • 打赏
  • 举报
回复
相关推荐
发帖
VB基础类

7617

社区成员

VB 基础类
社区管理员
  • VB基础类社区
加入社区
帖子事件
创建了帖子
2008-03-27 11:06
社区公告
暂无公告