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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

close app

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
Marcusw
Asked:
Marcusw
  • 3
1 Solution
 
SilentRageCommented:
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
 
SilentRageCommented:
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
 
MCummings111400Commented:
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
 
SilentRageCommented:
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
 
MarcuswAuthor Commented:
Fantastic thanks, i still a newbie so all your code and suggestions are very useful
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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now