Solved

Terminating a program through vb

Posted on 1998-07-18
12
164 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

12 Experts available now in Live!

Get 1:1 Help Now