PostMessage glHwnd, WM_CLOSE, 0&, 0&

Posted on 2005-04-30
Last Modified: 2006-11-18
I'm using this function to close a window containing certain titles I don't want to appear: PostMessage glHwnd, WM_CLOSE, 0&, 0&

However, if I get a popup window that reads "Internet Explorer Script Error"--it will not close this window. Unfortunately, the user must click either Yes or No in that error popup window box. question is: How can I get my application to automatically close that window?  Maybe I can automate a way in which my app will select No in that error prompt, to close it???
Question by:John Account
    LVL 13

    Expert Comment


    Try the DestroyWindow API

    Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long

    The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it and remove the keyboard focus from it. The function also destroys the window’s menu, flushes the thread message queue, destroys timers, removes clipboard ownership, and breaks the clipboard viewer chain (if the window is at the top of the viewer chain).
    LVL 28

    Expert Comment

    Have you tried with SendMessage?

    Author Comment

    by:John Account
    Hmmmmmnn. Can you give me an example of how to use it, iHadi--how to call it, etc? Thanks

    No, vinnyd79. I don't know how I would do that. You think that would work? If so, how, please?
    LVL 13

    Expert Comment

    Nothing Much

    Declare the API as follows in a module

    Public Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long

    Write the following code in the sub that closes the window as follwos

    tmpNum = DestroyWindow (glHwnd) <-- the handle of the window you want to close

    'if  tmpNum <> 0 then the function succeeded, Else failed
    LVL 29

    Accepted Solution

    Try this...It will close the window based on the doesnt have to be exact it works even if you specify a partial if i type internet    then it will close internet explorer window...etx...

    'Add the following code a .BAS module

    Option Explicit

    Declare Sub ReleaseCapture Lib "user32" ()
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
    Declare Function getwindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long)
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Public Const WM_CLOSE = &H10
        Public AppTitle As String
        Public ApphWnd As Long
    Function GetCaption(WindowhWnd)
       Dim hwndlength As Long
       Dim hWndTitle As String
       Dim a As Long
        hwndlength = GetWindowTextLength(WindowhWnd)
        hWndTitle = String$(hwndlength, 0)
        a = GetWindowText(WindowhWnd, hWndTitle, (hwndlength + 1))
        GetCaption = hWndTitle
    End Function
    Function CheckAllWindows(ByVal hwnd As Long, lParam As Long) As Boolean
        Dim a
        a = LCase(GetCaption(hwnd))

        If InStr(1, a, LCase(AppTitle)) <> 0 Then
            ApphWnd = hwnd
            CheckAllWindows = False
            CheckAllWindows = True
        End If
    End Function

    Sub KillWin(Title As String)
        Dim a
        AppTitle = Title

    EnumWindows AddressOf CheckAllWindows, 0&
        If ApphWnd = 0 Then Exit Sub
        a = PostMessage(ApphWnd, WM_CLOSE, 0&, 0&)
    End Sub
    Public Sub FormDrag(TheForm As Form)
        Call SendMessage(TheForm.hwnd, &HA1, 2, 0&)
    End Sub

    'Form code

    'add a command button and just add the following inside the command1_click

    Call KillWin("Internet Explorer Script")

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now