Attribute VB_Name = "KillMe"

'      Written by:  Mark Thesing
'            Date:  2/12/2001
'    Last Modified  4/17/2002
'
'         Purpose:  To use a batch file to delete an
'                   exe after it has run and then
'                   remove the batch file.
'
'           Usage:  In the QueryUnload event just call
'                   DeleteApp. It could be used if you
'                   only want the app to run once. Or, by
'                   keeping track of the number of useages
'                   you could delete the app after a trial
'                   period.
'
'           Notes:  This code was modified form code found at
'                   VB-World
'                   http://www.vbforums.com/showthread.php?s=&threadid=103148

Option Explicit
'   Holds the complete path and file
'   name to the batch file
Public strBatName As String

Private Declare Function GetShortPathName Lib "kernel32" _
            Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
            ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

Private Sub GetName()

Dim strBatDir As String   '   Holds the directory the
                        '   batch file will be going in
Dim lenBatDir As Long   '   Holds the length of the
                        '   directory for the batch file
Dim I As Long           '   Used to determine how to
                        '   name the batch file
Dim strExe As String    '   Holds the short name of the exe
Dim LenAppDir As Long
Dim strAppDir As String


    '   Get the short version of apps exe name and path
    '   This is where the batch file will
    '   look for the app to delete
    strAppDir = Space(256)                      'Create buffer
    
    If Right(App.Path, 1) <> "\" Then           'Check if the app is in the root dir.
        strExe = "\" & App.EXEName
    Else
        strExe = App.EXEName
    End If
    
    MkDir App.Path & strExe                     'Create a temp dir so we can get a
                                                'full short name to the exe
    LenAppDir = GetShortPathName(App.Path & strExe, strAppDir, 256)
    
    RmDir App.Path & strExe                     'Delete the temp dir since we are
                                                'done with it
    strAppDir = Left(strAppDir, LenAppDir)      'Drop unneeded characters
    strAppDir = strAppDir & ".exe"              'Assemble the full exe name and path
    

    '   Get the short version of the path
    '   to the windows directiory. This is
    '   where the batch file will be written.
    strBatDir = Space(256)
    lenBatDir = GetShortPathName(Environ$("windir"), strBatDir, 256)
    strBatDir = Left(strBatDir, lenBatDir)
    
    
    '   Make sure the directories end
    '   with a backslash
    If Right(strBatDir, 1) <> "\" Then
        strBatDir = strBatDir & "\"
    End If
    
    '   Make sure the name of the batch
    '   file is unique
    I = 1
    Do Until Len(Dir(strBatDir & I & ".bat")) = 0
        I = I + 1
    Loop
    
    '   Determine the name of the batch file,
    '   based on what's in the I variable
    strBatName = strBatDir & I & ".bat"
    
    'Make the batch file
    Open strBatName For Output As #1
        Print #1, "@echo off"
        Print #1, ":redo"
        Print #1, "del "; strAppDir
        Print #1, "if exist "; strAppDir; " goto redo"
        Print #1, "del "; strBatName
    Close #1

End Sub

Public Sub DeleteAPP()
    On Error GoTo IDEerr

    GetName
    
    'Make sure we're not running within the IDE
    Debug.Print 1 \ 0

    '   Start the batch file
    '   to delete the app
    Shell strBatName, vbHide

Exit Sub
IDEerr:
    Kill strBatName
End Sub

