script>
script>
Close DOS window
script>
The test method is as follows
Open two DOS windows first
Run Project1.exe
Open two DOS windows
Dance, the DOS window is opened directly, and the first open appearance is closed immediately after the window is closed, the speed depends on the Timer setting.
Test environment: XP, VB6
code show as below
'Form
Option expedition
Private sub flow_load ()
Me.Autoredraw = TRUE
OldwinProc = getWindowlong (me.hwnd, gwl_wndproc)
SetWindowlong Me.hWnd, GWL_WndProc, Addressof Onmenu
End Sub
Private Sub Form_Unload (Cancel AS Integer)
Setwindowlong Me.hwnd, GWL_WndProc, OldwinProc
End Sub
Private sub timer1_timer ()
Closeconsole1
End Sub
'Module
Option expedition
Public const wm_syscommand = & h112
Public const wm_queryendsession = & h11
Public const process_terminate = & h1
Public const gwl_Wndproc = (-4)
Public Declare Function SetWindowlong Lib "User32" Alias "SetWindowlonga" (Byval Nindex As Long, Byval Dwnewlong As Long) As long
Public Declare Function CallWindowProc LIB "User32" Alias "CallWindowProca" (Byval Hwndfunc As Long, Byval Hwnd As Long, Byval Msg As Long, Byval WParam As Long) As long
Public Declare Function GetWindowlong Lib "User32" Alias "getWindowlonga" (Byval Nindex 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 stay, BYVAL NMAXCOUNT AS long) As long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPrivate 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) AS Long
Private Declare Function FindWindow Lib "User32" Alias "Findwindowa" (Byval LpwindownAme As String) AS LONG
Public Declare Function GetWindowText Lib "User32" Alias "getWindowTexta" (Byval Hwnd As String, Byval Cch as long) AS Long
Public Declare Function FindwindowEx Lib "User32" Alias "FindWindowexa" (Byval Hwnd2 As Long, Byval Lpsz1 AS String, Byval LPSZ2 AS STRING) AS LONG
Public OldwinProc As Long
Public g__caption as string
Public G__hWnd As Long
'Query if it is turned off and close the DOS window
Public Function Onmeu (Byval Hwnd As Long, Byval WParam As Long, BYVAL LPARAM AS Long) As long
SELECT CASE WMSG
Case WM_QUERYENDSession
Form1.windowState = 0
FORM1.CLS
FORM1.PRINT "The system is turned off"
Closeconsole
Case WM_SYSCOMMAND
Case Else
End SELECT
Onmenu = CallWindowProc (OldwinProc, HWND, WMSG, WPARAM, LPARAM)
END FUNCTION
'Enumeration and close
Public Function Closeconsole () As Long
Enumwindows Addressof Enumproc, 0
CloseConsole = 1
END FUNCTION
'Enumeration back function
Public Function Enumproc (Byval Hwnd As Long, Byval LParam As Long) AS Long
DIM S AS STRING * 255
DIM S1 As String
DIM I as integer
GetclassName Hwnd, S, 255
For i = 1 to 255
IF MID (S, I, 1) <> chr (0) THEN
S1 = S1 & MID (S, I, 1) ELSE
EXIT for
END IF
Next i
IF S1 = "consolewindowclass" then
GetWindowText Hwnd, S, 255
TerminateProcessbyHWND HWND
Form1.print S
Form1.print hwnd
END IF
Enumproc = 1
END FUNCTION
'Close the window of any normal app
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
'Regular querying DOS window with unable to end
Public Function Closeconsole1 () AS Long
DIM Console As Long
Console = FindWindow ("consolewindowclass", vbnullstring) "Find no DOS window
DIM S AS STRING * 255
g__caption = ""
DIM I as integer
IF console kil
GetWindowText Console, S, 255
For i = 1 to 255
IF MID (S, I, 1) <> chr (0) THEN
g__caption = g__caption & mid (s, i, 1)
Else
EXIT for
END IF
Next I
'Debug.print g__caption
g__hwnd = console
ENUMWINDOWS ADDRESSOF ENUMPROC1, 0
END IF
CloseConsole1 = 1
END FUNCTION
'Whether the enumeration appears immediately ends
PUBLIC FUNC1 (BYVAL HWND As Long, Byval LParam As Long) As long
DIM S AS STRING * 255
DIM S1 As String
DIM I as integer
GetclassName Hwnd, S, 255
For i = 1 to 255
IF MID (S, I, 1) <> chr (0) THEN
S1 = S1 & MID (S, I, 1)
Else
EXIT for
END IF
Next I
IF S1 = "# 32770" THENS1 = ""
GetWindowText Hwnd, S, 255
For i = 1 to 255
IF MID (S, I, 1) <> chr (0) THEN
S1 = S1 & MID (S, I, 1)
Else
EXIT for
END IF
Next i
IF S1 = "End Procedure -" & g__caption Then
TerminateProcessbyHWND HWND
TerminateProcessByhwnd g__hwnd
END IF
END IF
Enumproc1 = 1
END FUNCTION