Solved

Terminating a program through vb

Posted on 1998-07-18
12
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

734 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