Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Having  VB close an application that is currently running.

Posted on 2003-03-13
7
Medium Priority
?
694 Views
Last Modified: 2008-03-03
I wrote (or should I say borrowed) some code which closes down an application which is currently running.  It works well on W2000 but not on W98 or W95.  Is there a method of implementing this that will work across all operating systems?  Here's the code that I was using.  As I say I developed it in VB6.0 on W2000, packaged it and installed it on a W98 machine.  Any help would be appreciated.

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                                               (ByVal lpClassName As String, _
                                               ByVal lpWindowName As String) As Long

Declare Function SendMessage Lib "user32" _
                Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
                                      ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Const WM_DESTROY = &H2

------------------------------------------
The code (put in command1_click):

Dim lHandle As Long
Dim lRez As Long

   Do
         ' Find the Menu window id.
      lHandle = FindWindow(vbNullString, "Explorer")
         ' Kill the window politely
      If lHandle Then
         lRez = SendMessage(lHandle, WM_CLOSE, 0, 0)
         ' Kill it (just in case)
         lRez = SendMessage(lHandle, WM_DESTROY, 0, 0)
      End If 'lHandle
   Loop While lHandle <> 0
0
Comment
Question by:eseidl
6 Comments
 
LVL 11

Accepted Solution

by:
supunr earned 300 total points
ID: 8132807
I am not sure whether this is going to help. but try putting a doevents in between.

Do
        ' Find the Menu window id.
     lHandle = FindWindow(vbNullString, "Explorer")
        ' Kill the window politely
     If lHandle Then
        lRez = SendMessage(lHandle, WM_CLOSE, 0, 0)
        DoEvents
        ' Kill it (just in case)
        lRez = SendMessage(lHandle, WM_DESTROY, 0, 0)
        DoEvents
     End If 'lHandle
  Loop While lHandle <> 0

Both FindWindow and SendMessage function should work properly in Windows 95/98/MX/2K/XP.  But remember, for FindWindow to work the window title must me exactly as it appear in the title.  "Somthing - Explorer" windows will not be detected by FindWindow.  If you want to do that then you wnat to do that then read MSDN article "Q147659" (HOWTO: Get a Window Handle Without Specifying an Exact Title)


Good Luck!
0
 

Author Comment

by:eseidl
ID: 8151266
Appreciate the suggestion.  Tried it but it didn't resolve the issue.  Note that this code works fine in W2K.

Thanks
0
 

Author Comment

by:eseidl
ID: 8151565
This issue has been resolved.  Thanks for the help.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:CleanupPing
ID: 9056354
eseidl:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 18

Expert Comment

by:Sethi
ID: 9063964
Recommendation: Points to  supunr
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9692920
Moderator, my recommended disposition is:

    Accept supunr's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0

Featured Post

Technology Partners: 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

581 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