|
-
Jun 22nd, 2006, 02:52 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Find all open windows to close
I'm trying to make a simple shutdown program, but it takes my slow computer to shutdown 5-10 big programs, so to save time I want the program to shutdown all open windows for me. I'v got a code to close the window, but how would I get a list of open windows and maybe put them in an array.
Shutdown code:
In module
VB Code:
Option Explicit
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) 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
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
'close apps
Private Const WM_CLOSE = &H10
Private Const WM_QUIT = &H12
Private Target As String
' Check a returned task to see if we should kill it.
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
' Get the window's title.
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)
' See if this is the target window.
If InStr(title, Target) <> 0 Then
' Kill the window.
SendMessage app_hWnd, WM_CLOSE, 0, 0
End If
' Continue searching.
EnumCallback = 1
End Function
' Ask Windows for the list of tasks.
Public Sub TerminateTask(app_name As String)
Target = app_name
EnumWindows AddressOf EnumCallback, 0
End Sub
Close code
VB Code:
TerminateTask "Microsoft Visual Basic"
Any help appreciated!
Visit here to learn to make the VB interface fit you!.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison 
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge
-
Jun 22nd, 2006, 03:26 PM
#2
Re: Find all open windows to close
If you force shut down a program you may damage data, which is why Windows takes a long time to shut down. It effectively calls Form_QueryUnload in every running program to see whether it can shut down. If a program says no, Windows stops shutting down.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 22nd, 2006, 03:30 PM
#3
Re: Find all open windows to close
He's not forcing them - he's just posting a WM_CLOSE - which is pretty much what happens at Shutdown anyway...I don't really see the point in doing it.
-
Jun 22nd, 2006, 04:51 PM
#4
PowerPoster
Re: Find all open windows to close
this is forcing ...?
One program that hates just turning off the power .. is FireFox .. im always loosing my Bookmarks cause of that . .. like in Power Outages .. nothing else ever gets corrupted .. just darn firefox .. :-(
VB Code:
Option Explicit
Private Sub Command1_Click()
TerminateProcess ("msmsgs.exe")
End Sub
Private Sub TerminateProcess(app_exe As String)
Dim Process As Variant
For Each Process In GetObject("winmgmts:").ExecQuery("Select * from Win32_Process Where Name = '" & app_exe & "'")
Process.Terminate
Next
End Sub
So i imagine it would be something like this .. though if a program is locked by another program you will have to keep looping until the other program is closed then .. anyway i havent tested this ..
VB Code:
Dim Process As Variant
For Each Process In GetObject("winmgmts:").ExecQuery("Select * from Win32_Process Where Name <> 'myVBProgram.exe'")
Process.Terminate
Next
'shut down now ..
PS. Bush, do you know what tpye the Process would be, instead of using Variant? thanks.
Last edited by rory; Jun 22nd, 2006 at 04:57 PM.
-
Jun 22nd, 2006, 05:42 PM
#5
Re: Find all open windows to close
yes, i believe that is forcing it - that's why Fx goes weird. You can test it by opening a new word document and then trying it - if you don't get a prompt to save then it's forcing it.
Regarding the process type - since you're late-binding you won't be able to declare it as anything more specific than Object.
-
Jun 22nd, 2006, 05:52 PM
#6
PowerPoster
Re: Find all open windows to close
Yep that killed word without the question ..
-
Jun 22nd, 2006, 06:09 PM
#7
Thread Starter
Fanatic Member
Re: Find all open windows to close
Actually, My code works wonderfully (for me), but is there a code to get a list of all open windows? Thanx
EDIT:
WM_CLOSE - which is pretty much what happens at Shutdown anyway...I don't really see the point in doing it.
I'd like to use this in other programs too, like this, so as not to be guessing at the names of programs when trying to work with them, e.g. add a list of all open windows to a list box.
Last edited by ididntdoit; Jun 22nd, 2006 at 06:20 PM.
Visit here to learn to make the VB interface fit you!.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison 
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge
-
Jun 22nd, 2006, 06:17 PM
#8
PowerPoster
Re: Find all open windows to close
all processes .. or just the windows ..?
also, Was just playing a round with the services .. check this code out ..
Turns Themes off then on again .. :-)
VB Code:
Option Explicit
Private Sub Command1_Click()
If RunService("Themes", False) Then
MsgBox "Themes Turned Off"
If RunService("Themes", True) Then MsgBox ("Themes Back on")
Else
MsgBox "Error"
End If
End Sub
Private Function RunService(Name As String, Enabled As Boolean)
Dim colServices As Variant
Dim objService As Variant
Dim ret As Long
Set colServices = GetObject("winmgmts:").ExecQuery _
("Select * from Win32_Service where Name='" & Name & "'")
For Each objService In colServices
If Enabled Then
ret = objService.StartService()
Else
ret = objService.StopService()
End If
If ret = 0 Then
RunService = True
End If
Next
End Function
-
Jun 22nd, 2006, 06:22 PM
#9
PowerPoster
Re: Find all open windows to close
VB Code:
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)
If Len(Trim$(title)) Then Debug.Print title '***** SHOW THE TITLES ****
If InStr(title, Target) <> 0 Then
TargetOpen = True
SendMessage app_hWnd, WM_CLOSE, 0, 0
End If
EnumCallback = 1
End Function
-
Jun 22nd, 2006, 06:28 PM
#10
Re: Find all open windows to close
as rory hasn't explained - you are already enumerating through all the open windows with EnumWindows - just post WM_CLOSE messages to all of them (you'll want to use PostMessage)
VB Code:
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
PostMessage app_hWnd, WM_CLOSE, 0, 0
EnumCallback = True
End Function
but i don't think this does what you (ultimately) want - your trying to recreate something that already happens at the time it would normally happen - i just don't see the point
-
Jun 22nd, 2006, 06:35 PM
#11
PowerPoster
Re: Find all open windows to close
Yep .. that will close the titles .. but wont get all programs .. eg. MSN messenger when minimized in the SYstem tray .. Enum doesnt catch it ..
Here is the same thing above modified with the Processes .. to enter everything in a TExt box ..
Put a Text1 Textbox on your Form1 .. MultiLine, Vertical Scroll bars .. make it tall and wide ..
This replaces the module code above .. this will list all open windows and processes seperately .. doesnt close anything BTW.
VB Code:
Option Explicit
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
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Private Const WM_QUIT = &H12
Private Target As String
Public TargetOpen As Boolean
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)
If Len(Trim$(title)) Then
Form1.Text1 = Form1.Text1 & vbCrLf & title
End If
'If InStr(title, Target) <> 0 Then
' TargetOpen = True
' SendMessage app_hWnd, WM_CLOSE, 0, 0
'End If
EnumCallback = 1
End Function
Public Sub TerminateTask(app_name As String, app_exe As String)
Dim Process As Variant
Form1.Text1 = Form1.Text1 & "WINDOW TITLES:" & vbCrLf & "=====================" & vbCrLf
'TargetOpen = False
'Target = app_name
EnumWindows AddressOf EnumCallback, 0
Form1.Text1 = Form1.Text1 & vbCrLf & vbCrLf & "PROCESS NAMES:" & _
vbCrLf & "=====================" & vbCrLf & vbCrLf
For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process")
Form1.Text1 = Form1.Text1 & Process.Name & vbCrLf
Next
End Sub
Last edited by rory; Jun 22nd, 2006 at 06:39 PM.
-
Jun 22nd, 2006, 06:35 PM
#12
Thread Starter
Fanatic Member
-
Jun 22nd, 2006, 06:44 PM
#13
PowerPoster
Re: Find all open windows to close
same as above ..
CloseAllWindows
VB Code:
Option Explicit
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
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Private Const WM_QUIT = &H12
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
SendMessage app_hWnd, WM_CLOSE, 0, 0
EnumCallback = 1
End Function
Public Sub CloseAllWindows()
EnumWindows AddressOf EnumCallback, 0
End Sub
-
Jun 22nd, 2006, 06:49 PM
#14
PowerPoster
Re: Find all open windows to close
BTW that just brings up the ShutDown Dialog also as it closes the windows .. doesnt terminate all the processes either ..
-
Jun 22nd, 2006, 06:52 PM
#15
Thread Starter
Fanatic Member
Re: Find all open windows to close
I liked the code before my last post, but is there a way to make it only list windows, and only visible ones? (the only program ever in the tray is BitComet, which I always close manually), othere than a few issues of listing a million programs, it was great!
Visit here to learn to make the VB interface fit you!.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison 
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge
-
Jun 24th, 2006, 08:23 AM
#16
Thread Starter
Fanatic Member
Re: Find all open windows to close
If possible, is there also/instead a method to get the class name of a window? I'm working on a panic program that hides any visible programs chosen as 'target' programs at a mouse click or key combo. I know ther's a manual way to do this with Spy++, but can you do it with code (and do you need the window name first?)?
Visit here to learn to make the VB interface fit you!.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison 
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge
-
Jun 24th, 2006, 08:27 AM
#17
Re: Find all open windows to close
use the GetClassName API. you need the handle, but presumably you'll be enumerating all the windows anyway.
-
Jun 24th, 2006, 09:21 AM
#18
Thread Starter
Fanatic Member
-
Jun 24th, 2006, 09:24 AM
#19
Re: Find all open windows to close
you already are - that is what EnumWindows does.
-
Jun 24th, 2006, 10:20 AM
#20
PowerPoster
Re: Find all open windows to close
see post #11 .. title = Window Name of Every Open window ..
-
Jun 24th, 2006, 06:28 PM
#21
Thread Starter
Fanatic Member
-
Jun 24th, 2006, 06:39 PM
#22
Thread Starter
Fanatic Member
Re: Find all open windows to close
Never mind people, I got some code my self.
VB Code:
Option Explicit
Global qwerty As Integer
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
Private 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
' Process And Memory
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 CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32" ( _
ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32" ( _
ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, _
ByVal flProtect As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" ( _
ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, _
Optional lpNumberOfBytesWritten As Long) As Long
Const PROCESS_VM_READ = (&H10)
Const PROCESS_VM_WRITE = (&H20)
Const PROCESS_VM_OPERATION = (&H8)
Const MEM_COMMIT = &H1000
Const MEM_RESERVE = &H2000
Const MEM_RELEASE = &H8000
Const PAGE_READWRITE = &H4
Const WM_USER = &H400
Const TB_ISBUTTONHIDDEN = (WM_USER + 12)
Const TB_BUTTONCOUNT = (WM_USER + 24)
Const TB_GETBUTTONTEXTA = (WM_USER + 45)
Public Sub ShowOpenWindows()
Dim hTaskBar As Long, pID As Long, hProcess As Long
Dim N As Long, lCount As Long, lNum As Long, lLen As Long
Dim sCaption As String * 128, lpCaption As Long
hTaskBar = GetNotificationWindow
GetWindowThreadProcessId hTaskBar, pID
hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, 0, pID)
lpCaption = VirtualAllocEx(hProcess, ByVal 0&, Len(sCaption), MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE)
lNum = SendMessage(hTaskBar, TB_BUTTONCOUNT, 0, ByVal 0&)
Do Until lCount = lNum
lLen = SendMessage(hTaskBar, TB_GETBUTTONTEXTA, N, ByVal lpCaption)
If lLen > -1 Then
If SendMessage(hTaskBar, TB_ISBUTTONHIDDEN, N, 0&) = 0 Then
ReadProcessMemory hProcess, ByVal lpCaption, ByVal sCaption, Len(sCaption)
For qwerty = 0 To 5
Form1.lstwindows(qwerty).AddItem Left$(sCaption, InStr(sCaption, vbNullChar) - 1)
Next qwerty
End If
lCount = lCount + 1
End If
N = N + 1
Loop
VirtualFreeEx 0, lpCaption, 0, MEM_RELEASE
CloseHandle hProcess
End Sub
Private Function GetNotificationWindow() As Long
Dim lhWnd As Long
lhWnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "ReBarWindow32", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "MSTaskSwWClass", vbNullString)
GetNotificationWindow = FindWindowEx(lhWnd, 0&, "ToolbarWindow32", vbNullString)
End Function
Don't recoall now where I got it, but if I got it from VBForums, and it was yours, let me know so I can reputate and thank you.
Visit here to learn to make the VB interface fit you!.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison 
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge
-
Jun 24th, 2006, 07:11 PM
#23
Re: [RESOLVED] Find all open windows to close
I posted that today in answer to my own question: http://www.vbforums.com/showthread.php?t=413154
-
Jun 24th, 2006, 07:24 PM
#24
PowerPoster
Re: [RESOLVED] Find all open windows to close
 Originally Posted by bushmobile
is there a way to get it by Processes ..? or enum windows ..?
-
Jun 24th, 2006, 08:27 PM
#25
Re: [RESOLVED] Find all open windows to close
[NON-VB]
I have set my computer's Power Button to Hibernet. In normal cases, if I need to shutdown/reboot, I use Start>Shutdown.
But in case of a power cut or similar emergency, I just hibernate it by pressing the Power Button.
During shutdown/hibernate process, turning off monitor will give you more battery power.
Also, if you set the Power Button to Shutdown, it will always do a force shutdown.
[/NON-VB]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|