?
Solved

close app

Posted on 2003-02-24
5
Medium Priority
?
188 Views
Last Modified: 2010-04-07
I have an app lets say APP1 that checks for an updated exe.  if APP1.exe has been updated it uses the shell command to run another app lets say APP2 that copys the file to the users machine and renames it. Then i want the app to shut down the APP1 and delete it. Then rename the updated exe then run the APP1 agian

the code i have is

APP1 Code
******************************************
Private Sub Form_Load()
Set FSys = CreateObject("Scripting.FileSystemObject")

Set SourceFile = FSys.getfile("C:\APP1.exe")
SourceFileDateModified = SourceFile.DateLastModified
MsgBox (SourceFileDateModified)

Set CurrentFile = FSys.getfile("\\Machineserver\Database\APP1.exe")
CurrentFileDateModified = CurrentFile.DateLastModified
MsgBox (CurrentFileDateModified)

If SourceFileDateModified <> CurrentFileDateModified Then
Shell "c:\APP2.exe", vbHide
Unload frmLogin
End If
End Sub
****************************************

APP2 code
****************************************
Private Sub Form_Load()
Dim FSys As New Scripting.FileSystemobject

FSys.copyfile "\\Machineserver\Database\APP1.exe", "c:\APP1.tmp", overwrite

'Code to close APP1

Kill "c:\APP1.exe"
Name "c:\APP1.tmp" As "c:\APP1.exe"
Shell "c:\APP1.exe", vbNormalFocus
Unload Form1
End Sub
*********************************************
0
Comment
Question by:Marcusw
[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
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
SilentRage earned 200 total points
ID: 8008999
why are you trying to close App1 from App2?  It's very easy, but it would be easier to have App1 close itself down.  You can have a loop in App2 to try to kill App1 until it's successful.  That way App1 can take as long as it wants to close down, and App2 will wait on it.  Also, since I don't like errors, I'll use the sleep function to try to avoid looped errors.

Example:

APP1 Code
******************************************
Private Sub Form_Load()

'code stuff

If SourceFileDateModified <> CurrentFileDateModified Then
    'execute app2
    Shell "c:\APP2.exe", vbHide
    'shut down immediately
    End
End If
End Sub
****************************************
APP2 code
****************************************
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Dim FSys As New CreateObject("Scripting.FileSystemobject")
    FSys.copyfile "\\Machineserver\Database\APP1.exe", "c:\APP1.tmp", overwrite

    KillApp1()

    Name "c:\APP1.tmp" As "c:\APP1.exe"
    Shell "c:\APP1.exe", vbNormalFocus
    Unload Form1
End Sub
Private Sub KillApp1()
On Error Resume Next
    Do
        Kill "c:\APP1.exe"
        If Err.Number > 0 Then
            Err.Clear
            'pause execution for 2 seconds
            Sleep 2000
        Else
            Exit Do
        End If
    Loop
End Sub
*********************************************

Ok ok, so let's say you would still prefer to close App1 from App2.  *sigh*  ;)  My favorite method of detecting another application is by using FindWindow.  That means you need to find out your main window caption and class combonation, and make sure it's fairly unlikely that another program would have the same combo.  Then you can do something like this:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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
Private Function CloseApp1(WndTitle As String, WndClass As String) As Function
Private Const WM_CLOSE = &H10
Dim hWindow As Long
    'Get the handle of your application main window
    hWindow = FindWindow(WndClass, WndTitle)
    'Send a message to app1 that it can't refuse.  ;)
    CloseApp1 = SendMessage(hWindow, WM_CLOSE, 0, 0) = 0
End Sub

A very not nice alternative:

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private Function TerminateApp1() As Boolean
Dim hWindow As Long, hProcess As Long, hOpenProcess As Long

    hWindow = FindWindow(WndClass, WndTitle)
    'Get process ID associated with hWindow
    GetWindowThreadProcessId hWindow, hProcess
    'retrieve permission to terminate this process
    hOpenProcess = OpenProcess(PROCESS_TERMINATE, False, hProcess)
    If hOpenProcess <> 0 Then
        'slap that bad boy down whether it likes it or not
        TerminateApp1 = TerminateProcess(hOpenProcess, 0) <> 0
    End If
End Function
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8009017
oh yeah, almost forgot.  How do you find the window title and class you say?  There's a nifty program that comes with Visual Studio Professional called Spy++.  Take a peek at it.  With it you can find the class and title of any window.  Just open your app1, and make sure it stays open.  Then use Spy++ FindWindow feature to highlight the window of which you'd like to collect information about.  Then you may use my above code.
0
 
LVL 3

Expert Comment

by:MCummings111400
ID: 8010030
But your code still copies over the executable before closing down the app. You should first close the app, then copy your file, then start it back up again. aviods any file locking issues that may occur.
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8010791
my first example included deleting the old app.  My last 2 code examples were just close app functions meant to replace the

'Code to close APP1

in his example.
0
 

Author Comment

by:Marcusw
ID: 8015247
Fantastic thanks, i still a newbie so all your code and suggestions are very useful
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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

752 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