Results 1 to 7 of 7

Thread: Closing my own program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    At my computer
    Posts
    187

    Post

    I have written an application which is loaded on startup by the computer. It is hidden from the user and can only be seen in the Ctrl+Alt+Delete list. Now, if someone downloaded this program and installed it, everything would work fine. But what if they tried to uninstall it? The uninstaller would not be able to delete the EXE, since it is open. So, I need to provide the user with a way to close the program. There are three options:

    1. Make them use Ctrl+Alt+Delete to close it.
    This is bad.

    2. Create a System Tray icon and have an Exit command on that.
    This is pretty good, but I don't like System Tray icons because everybody has TONS of them. Why add another?

    3. Create an icon in the program group for the program called "Close PersonalSaver."
    The user selects this icon and the program closes. The command line would be "C:\PersonalSaver\PersonalSaver.exe /close" I know how to set up command line parameters, but I don't know how to make the program close itself - the other one of it that is running..
    - Visual Basic 6.0
    - Windows XP Home

  2. #2
    Guest
    Could you make it listen on a port on the local machine?
    Then you just another app which send a 'Die' command to it and it gracefully closes down.

  3. #3
    Guest
    This will close an application:

    Code:
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * MAX_PATH
        End Type
    
    
    Public Function KillApp(myName As String) As Boolean
        Const PROCESS_ALL_ACCESS = 0
        Dim uProcess As PROCESSENTRY32
        Dim rProcessFound As Long
        Dim hSnapshot As Long
        Dim szExename As String
        Dim exitCode As Long
        Dim myProcess As Long
        Dim AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        
        Const TH32CS_SNAPPROCESS As Long = 2&
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do While rProcessFound
            i = InStr(1, uProcess.szexeFile, Chr(0))
            szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function
    
    Usage:
    
    Call KillApp("C:\myprog\myprog.exe")

  4. #4
    Guest

    Wink

    Hi Sir:
    This is not related to your question, but i am interested in how to run the program at the startup? can you show me please? and also, how can we run without letting the user seen it...
    thanks

  5. #5
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    This reg ket lists programs that are runned at startup.
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    At my computer
    Posts
    187
    Thanks a lot, Matthew Gates! After a few tweaks, I got the code to do what I needed.

    dragonyian, what exactly do you need? Windows starts applications from three places, the Startup menu, the Registry, and somewhere in one of the Windows INI files. (System.ini or Win.ini, I think).

    If you do not want the user to see the application start or know it is starting, use the Registry. The list of applications Windows will run is at:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

    OR:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

    If you want the application to run for only the currently logged in user, use the second location.

    If you have a Registry module, you can simply write to one of the those locations, when your program starts, like this:
    Code:
    SetKeyValue HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "MyProg", App.Path & "\MyProg.exe"
    This is from my Registry module. If you want a Registry module, I can email it to you. Let me know if I can help you any more.
    - Visual Basic 6.0
    - Windows XP Home

  7. #7
    Guest
    To make your program startup (using the registry):

    Code:
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const ERROR_SUCCESS = 0&
    Private Const REG_SZ = 1
    Private Const KEY_SET_VALUE = &H2
    
    Public Function SetProgramStartup(pProgramName As String, pProgramPath As String) As Boolean
        Dim lKeyHandle As Long
        Dim lRet As Long
        Dim strBuffer As String
        Dim strKey As String
        
        strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_SET_VALUE, lKeyHandle)
        If lRet = ERROR_SUCCESS Then
          lRet = RegSetValueEx(lKeyHandle, pProgramName, 0, REG_SZ, ByVal pProgramPath, Len(pProgramPath))
          RegCloseKey lKeyHandle
        End If
    End Function
    
    
    Private Sub Command1_Click()
        Call SetProgramStartup("MyEliteProg", "C:\Elite-prog.exe")
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width