Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Terminating a program through vb

Hi.
Is there a way of terminating a program through my vb program?  Say, if I have notepad open and I want to close it through the vb program I'm writing, how would I do it.  Send me code, that would be easier for me, since I am new to vb.  Please make sure that code compiles in vb5.  Thanks
0
kermit007
Asked:
kermit007
  • 8
  • 4
1 Solution
 
MikeP090797Commented:
To close Notepad:

Put this in a module:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long


Put this in the place you want to close notepad:

Dim H as long
H = FindWindow("Notepad"m "")
CloseWindow H
0
 
MikeP090797Commented:
oops, a small typo, one line before the end should be:
H = FindWindow("Notepad", "")
0
 
kermit007Author Commented:
MikeP
I have done as you told me to, but the program is not closing the window.
I have put these lines in a module:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long

the code for my form is:

Private Sub Command1_Click()
    Dim H As Long
    H = FindWindow("notepad", "")
    CloseWindow H
End Sub

It notepad still remains open.  I have tried changing H = FindWindow("notpad", "") to H = FindWindow("untitled - notepad", "") since that is how notepad title appears but that still doesn't work.

Any Ideas??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kermit007Author Commented:
MikeP
I have done as you told me to, but the program is not closing the window.
I have put these lines in a module:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long

the code for my form is:

Private Sub Command1_Click()
    Dim H As Long
    H = FindWindow("notepad", "")
    CloseWindow H
End Sub

It notepad still remains open.  I have tried changing H = FindWindow("notpad", "") to H = FindWindow("untitled - notepad", "") since that is how notepad title appears but that still doesn't work.

Any Ideas??
0
 
MikeP090797Commented:
Actually, Corpse might be right. Try putting vbNullString as the second parameter in FindWindow
0
 
kermit007Author Commented:
Hey CoRpSe
I have tried putting null string for both name and class [ H = FindWindow("", "") ] though no success.  I have tried H = FindWindow("", "notepad"), no success there either.  Another thing I found is everytime the program runs, H returns a value of 0.  Correct me if I'm wrong but shouldn't that be anything but 0?  
0
 
kermit007Author Commented:
tried putting vbNullString as the second parameter.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal vbNullString As String) As Long

Correct me If I'm wrong, but it still doesn't work.
0
 
kermit007Author Commented:
tried putting vbNullString as the second parameter.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal vbNullString As String) As Long

Correct me If I'm wrong, but it still doesn't work.
0
 
kermit007Author Commented:
tried putting vbNullString as the second parameter.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal vbNullString As String) As Long

Correct me If I'm wrong, but it still doesn't work.
0
 
MikeP090797Commented:
Ok, I've checked it, this one works:

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function CloseWindow Lib "user32" (ByVal hwnd 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
Const WM_CLOSE = &H10



Private Sub Form_click()
Dim H As Long
H = FindWindow("Notepad", vbNullString)
SendMessage H, WM_CLOSE, 0, 0

End Sub

0
 
kermit007Author Commented:
Thanks Mike.  The program works.  Just have a question.  What if I want to close someother program not just notepad, say vb, sound recorder, media player or someother program.  What is the process for that?  Would I type in the name of program, the executable, the window caption or something else?
0
 
kermit007Author Commented:
Thanks guys :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now