|
-
Oct 31st, 2001, 04:43 PM
#1
Thread Starter
Frenzied Member
Close an application
Is it possible to close another application from a VB program if it is not shelled?
seoptimizer2001
VB 6.0, VC++, VI, ASP, JavaScript, HTML,
Perl, XML, SQL Server 2000
If God had intended us to drink beer, He would have given us stomachs.
Please use the [code] and [vbcode] tags in your posts!
If you don't know how to use them please go HERE!

-
Oct 31st, 2001, 04:53 PM
#2
PowerPoster
Certainly is, provided you know the window caption
VB Code:
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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const WM_CLOSE = &H10
Private Sub Command1_Click()
Dim intHwnd As Long
intHwnd = FindWindow(vbNullString, "Calculator") ' Must match the caption of the window exactly
SendMessage intHwnd, WM_CLOSE, 0&, 0&
End Sub
-
Oct 31st, 2001, 05:24 PM
#3
Thread Starter
Frenzied Member
seoptimizer2001
VB 6.0, VC++, VI, ASP, JavaScript, HTML,
Perl, XML, SQL Server 2000
If God had intended us to drink beer, He would have given us stomachs.
Please use the [code] and [vbcode] tags in your posts!
If you don't know how to use them please go HERE!

-
Oct 31st, 2001, 05:30 PM
#4
Member
Can you get the exact name of running tasks from VB?
-
Oct 31st, 2001, 05:32 PM
#5
PowerPoster
This code will fill a listbox with the caption of each open "window". This includes all programs in the task bar and any windows within those programs
In a Module
VB Code:
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Const WM_GETTEXT = &HD
Private aCaptions() As String
Private lCount As Long
Public Function GetAllCaptions() As Variant
lCount = 0
Call EnumWindows(AddressOf EnumWindowsProc, 0&)
If lCount Then ReDim Preserve aCaptions(lCount - 1)
GetAllCaptions = IIf(lCount, aCaptions, Array())
End Function
Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sBuffer As String * 260
If IsWindowVisible(hwnd) Then
ReDim Preserve aCaptions(lCount)
aCaptions(lCount) = Left(sBuffer, SendMessage(hwnd, WM_GETTEXT, 260, ByVal sBuffer))
If Len(Trim(aCaptions(lCount))) Then lCount = lCount + 1
End If
EnumWindowsProc = hwnd
End Function
In a form (with a List1 and a Command1)
VB Code:
Private Sub Command1_Click()
Dim vCaps As Variant
Dim lIndex As Long
vCaps = GetAllCaptions()
List1.Clear
For lIndex = 0 To UBound(vCaps)
List1.AddItem vCaps(lIndex)
Next
End Sub
-
Oct 31st, 2001, 05:37 PM
#6
Thread Starter
Frenzied Member
That's an even bigger help
seoptimizer2001
VB 6.0, VC++, VI, ASP, JavaScript, HTML,
Perl, XML, SQL Server 2000
If God had intended us to drink beer, He would have given us stomachs.
Please use the [code] and [vbcode] tags in your posts!
If you don't know how to use them please go HERE!

-
Oct 31st, 2001, 06:02 PM
#7
Member
Originally posted by chrisjk
This code will fill a listbox with the caption of each open "window". This includes all programs in the task bar and any windows within those
Thanks!
-
Oct 31st, 2001, 07:55 PM
#8
Addicted Member
Code:
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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const WM_CLOSE = &H10
Private Sub Command1_Click()
Dim intHwnd As Long
intHwnd = FindWindow(vbNullString, "Calculator") ' Must match the caption of the window exactly
SendMessage intHwnd, WM_CLOSE, 0&, 0&
End Sub
I have a strong suspicion this won't work on Windows NT. Anyone know for sure ?
-
Oct 31st, 2001, 07:57 PM
#9
PowerPoster
-
Oct 31st, 2001, 08:08 PM
#10
Addicted Member
I'll try it at work tomorrow and let you know.
-
Oct 31st, 2001, 08:14 PM
#11
PowerPoster
yeah, thanks. NT and 2K are similar, but you never know with MS
-
Oct 31st, 2001, 08:55 PM
#12
Originally posted by tcurrier
I have a strong suspicion this won't work on Windows NT. Anyone know for sure ?
I looked at it and I can see no reason why it wouldn't work on WinNT. It should work with no problem.
-
Nov 1st, 2001, 08:16 AM
#13
Addicted Member
Sorry guys! False alarm! It works fine on Windows NT 4.0 !!!
-
Nov 1st, 2001, 08:27 AM
#14
Member
Originally posted by tcurrier
I have a strong suspicion this won't work on Windows NT. Anyone know for sure ?
How about 98/me?
-
Nov 1st, 2001, 12:09 PM
#15
PowerPoster
98 has no problems with that code
-
Nov 1st, 2001, 12:27 PM
#16
Hey chrisjk,
what do u mean by you built it, how do build stuff like that and how do u know which functions to call?
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
|