Solved

Terminating a program through vb

Posted on 1998-07-18
12
167 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
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!

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 121
Hide vba in gp 7 113
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 60
Error with a code discussed on this page 5 38
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 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