Solved

Terminating a program through vb

Posted on 1998-07-18
12
165 Views
Last Modified: 2010-05-03
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
Comment
Question by:kermit007
  • 8
  • 4
12 Comments
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 100 total points
ID: 1466147
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
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1466148
oops, a small typo, one line before the end should be:
H = FindWindow("Notepad", "")
0
 
LVL 1

Author Comment

by:kermit007
ID: 1466149
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
 
LVL 1

Author Comment

by:kermit007
ID: 1466151
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
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1466152
Actually, Corpse might be right. Try putting vbNullString as the second parameter in FindWindow
0
 
LVL 1

Author Comment

by:kermit007
ID: 1466153
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:kermit007
ID: 1466154
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
 
LVL 1

Author Comment

by:kermit007
ID: 1466155
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
 
LVL 1

Author Comment

by:kermit007
ID: 1466156
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
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1466157
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
 
LVL 1

Author Comment

by:kermit007
ID: 1466158
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
 
LVL 1

Author Comment

by:kermit007
ID: 1466160
Thanks guys :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now