?
Solved

Having  VB close an application that is currently running.

Posted on 2003-03-13
7
Medium Priority
?
691 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
[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
7 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
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!

 

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

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.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 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