Results 1 to 18 of 18

Thread: [RESOLVED] Close Program

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Resolved [RESOLVED] Close Program

    How do I close a running application via VB 6?

    Thank you,
    Sir Loin

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Close Program

    The SendMessage API should do it using the WM_CLOSE constant. I believe your also gonna need the FindWindow API to find the hWnd of the app to close.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    That would just close the window, I want to unload the entire program itself.

    I found this code on AllAPI.net

    VB Code:
    1. Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
    2. Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    3. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    4.  
    5. Private Sub Form_Load()
    6.     'KPD-Team 1999
    7.     'URL: [url]http://www.allapi.net/[/url]
    8.     'E-Mail: [email][email protected][/email]
    9.     'end this process
    10.     ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
    11. End Sub

    What process would that kill though?

    Thank you,
    Sir Loin

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Close Program

    Try this
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    2. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    4. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    5. ByVal lParam As Long) As Long
    6.  
    7. Private Const WM_CLOSE = &H10
    8.  
    9. Private Sub cmdCloseApp_Click()
    10. Dim lngCloseIt As Long
    11. lngCloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    12. PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    13. End Sub

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Close Program

    SendMessage can do it as well, right?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    That would close a window, I want to kill the entire process.

    For Example, when you open the Windows Task Manager, and go to the "Processes" tab, I want to kill a process via VB 6.

    Thank You,
    Sir Loin

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Close Program

    Quote Originally Posted by Jacob Roman
    SendMessage can do it as well, right?
    PostMessage will close it immediately.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    What Process does GetCurrentProcess get?

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Close Program


  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    Oh, thanks.

    So how would I be able to end FireFox.exe?

    Thank You,
    Sir Loin

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Close Program

    I think the processname is firefox.exe, so
    VB Code:
    1. Shell "tskill firefox.exe"
    will work on XP

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    You need the processID to use tskill, how do I get the process ID?

    Will tskill only work on WinXP?

    Thank You,
    Sir Loin

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Close Program

    I got the classname, and was going to try tskill, but found that this app closed it, or at least prompted me that I was about to close 2 windows.

    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    5. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    6. Const SW_SHOWNORMAL = 1
    7. Const WM_CLOSE = &H10
    8. Const WM_ACTIVATE = &H6
    9. Const gcClassnameMSWord = "OpusApp"
    10. Const gcClassnameMSExcel = "XLMAIN"
    11. Const gcClassnameMSIExplorer = "IEFrame"
    12. Const gcClassnameMSVBasic = "wndclass_desked_gsk"
    13. Const gcClassnameNotePad = "Notepad"
    14. Const gcClassnameMyVBApp = "ThunderForm"
    15.  
    16. Private Sub Form_Load()
    17.     'KPD-Team 1998
    18.     'URL: [url]http://www.allapi.net/[/url]
    19.     'E-Mail: [email][email protected][/email]
    20.     Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
    21.     'Ask for a Window title
    22.     Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
    23.     'Search the window
    24.     WinWnd = FindWindow(vbNullString, Ret)
    25.     If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
    26.     'Show the window
    27.     ShowWindow WinWnd, SW_SHOWNORMAL
    28.     'Create a buffer
    29.     lpClassName = Space(256)
    30.     'retrieve the class name
    31.     RetVal = GetClassName(WinWnd, lpClassName, 256)
    32.     'Show the classname
    33.     MsgBox "Classname: " + Left$(lpClassName, RetVal)
    34.     'Post a message to the window to close itself
    35.     PostMessage WinWnd, WM_CLOSE, 0&, 0&
    36. '    PostMessage WinWnd, WM_ACTIVATE, 0&, 0&
    37. End Sub

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Close Program

    PostMessage will send the message to the windows message queue and return immediatly and SendMessage will send the
    message to the windows message queue and wait for it to process it and then return. Other then that they are the same.

    You can not be sure that if you close a form it will close the entire app. The better solution is to Terminate the Process.


    Or you could just use firefox as it doesnt take much to corrupt it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    That code just gets the classname, and closes the window, I need to end a process...

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Close Program

    I was just trying to get the classname, and postmessage closed it. I was trying to use "tskill.exe"

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Close Program

    Thank you everybody! I got it!

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Close Program

    Its easy to use too
    VB Code:
    1. Private Sub Form_Load()
    2.    TerminateEXE "FireFox.exe"
    3. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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