Results 1 to 11 of 11

Thread: Close api

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Can someone give me the code for the api
    called "Close"
    Thanks!


  2. #2
    Guest
    Close what?
    a program by its hwnd?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    No the api does it. and its called
    close .. It minimises the hwnd named

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    u can use the send_message API if u need to close a window.





    [Edited by rammy on 08-22-2000 at 12:10 AM]

  5. #5
    Guest
    Code:
    Private Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long

  6. #6
    Guest
    Here is how to close a Window:

    Code:
    Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long                    
    Declare Function PostMessage Lib "user32" Alias _
    "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long                    
    Public Const WM_CLOSE = &H10  
    
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, "WindowCaption")
    Debug.Print winHwnd
    If winHwnd <> 0 Then
        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)    
    If RetVal = 0 Then
            MsgBox "Error posting message."
        End If
    Else
        MsgBox "The Window is not open."
    End If
    If you want to minimize it:

    Code:
    Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
    Public Const SW_MINIMIZE = 6
    
    Dim winHwnd As Long
    winHwnd = FindWindow(vbNullString, "WindowCaption")
    If winHwnd = 0 Then 
    Msgbox "Window Not Found!", 16
    Else
    X = ShowWindow(winHwnd , SW_MINIMIZE)
    End If



    [Edited by Matthew Gates on 08-22-2000 at 12:21 AM]

  7. #7
    Guest
    What is the difference Between SendMessage and PostMessage ???

  8. #8
    Guest
    Good question. Don't know about PostMessage, but SendMessage sends a message to the application asking it to close. I guess PostMessage posts a message instead of sending one .

    But here is another way to kill an application by it's path.
    And it works very well.

    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:\Apppath\app.exe")

  9. #9
    Guest
    Good question. Don't know about PostMessage, but SendMessage sends a message to the application asking it to close. I guess PostMessage posts a message instead of sending one .

    I know what SendMessage Does.

    does anybody else have a clue to what PostMessage does? or how its different?

  10. #10
    Guest
    PostMessage posts a message in the message queue and returns without waiting for the thread to process the message, whereas SendMessage waits until the message is processed then returns.

    To minimize a window, use the CloseWindow API
    Code:
    Private Private Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long
    
    Private Sub Command1_Click()
        CloseWindow hwnd
    End Sub

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    So there isn't any way to fetch the result when using PostMessage?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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