Jump to content

Kill Window by module name


inferno123
 Share

Recommended Posts

Hi everyone

I have written a piece of code that is capable of killing(closing) a window not by its name but by the module name that has inititiated a window.

In the environment I work killing windows by name was too much trouble because we support 35 countries and New hardware wizard windows name is different in every language. I enclose the code that kills "New Hardware Wizard Windows" by module name.

If it has already been done in any form I apologize in advance.

Thanks

; ====================================================================================================


========================
; Name...........: $Kill_hwd_wizard_windows
; Description ...: Script enumerates exisitng windows(both active and hidden) and compare smodule names.If there is match it sends
; ...............: WM_CLOSE message to the window control in order to close it.
; Author ........: Pawel Szuszkiewicz(Inferno123)
; Remarks .......:
; ====================================================================================================


===========================
#include<Security.au3>
#include<StructureConstants.au3>
#include<WinAPI.au3>
#include<Constants.au3>
#include <Array.au3>

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Global Const $WM_CLOSE = 0x0010
Global Const $HWD_module_name = "C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\COMCTL32.dll" 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$list_all_windows = WinList()   
Do 
    WinList()
    Sleep(1000)
Until $list_all_windows[0][0] <> 0  

$handle = DLLCallbackRegister ("_EnumWindowsProc", "int", "hwnd;lparam")
For $a = 1 To 20 Step + 1
    Hardware_wizard_main()
Next
DllCallbackFree ($handle)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FUNCTIONS START;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func Hardware_wizard_main()
    $HWD_Main = DllCall("user32.dll", "int", "EnumWindows", "ptr", DllCallbackGetPtr ($handle), "lparam", 10)
    If @error Then
        MsgBox(0, "", "@ERROR: " & @error & @CRLF & "@EXTENDED: " & @extended)
    Else
        Return False
        $WinAPI_Error = _WinAPI_GetLastError ()
        If $WinAPI_Error <> 0 Then
            MsgBox(0, "EnumWindows", "Error " & _WinAPI_GetLastError () & @CRLF & _WinAPI_GetLastErrorMessage ())
            Exit
        EndIf
        Return $HWD_Main [0] <> 0
    EndIf
EndFunc ;==>Hardawer_wizard_main

Func _EnumWindowsProc($hwnd, $lParam)
    Local $FileName
    $ModuleFileName = GetWindowModuleFileName($hwnd, $FileName, 250)
    If WinGetTitle($hwnd) <> "" And BitAND(WinGetState($hwnd), 2) And $ModuleFileName <> "" Then
        If $ModuleFileName = $HWD_module_name Then
            SendMessage($hwnd, $WM_CLOSE, 0, 0)
    ;SendMessage($hWnd,0x0112,0xF060,0)
            Sleep(1000)
        EndIf
    EndIf
    Return $hwnd
EndFunc ;==>_EnumWindowsProc

Func SendMessage($hwnd, $Msg, $wParam, $lParam)
    
    $result = DllCall("user32.dll", "int", "SendMessage", "hwnd", $hwnd, "int", $Msg, "int", $wParam, "int", $lParam)
    If @error Then
        MsgBox(0, "", "@ERROR: " & @error & @CRLF & "@EXTENDED: " & @extended)
    Else
        $WinAPI_Error = _WinAPI_GetLastError ()
        If $WinAPI_Error <> 0 Then
            MsgBox(0, "SendMessage", "Error " & _WinAPI_GetLastError () & @CRLF & _WinAPI_GetLastErrorMessage ())
            Exit
        EndIf
        Return $result[0]
        
    EndIf
EndFunc ;==>SendMessage

Func GetWindowModuleFileName($hwnd, $FileName, $FileNameMax)

    $result = DllCall("user32.dll", "long", "GetWindowModuleFileName", "hwnd", $hwnd, "str", $FileName, "long", $FileNameMax)
    If @error Then
        MsgBox(0, "", "@ERROR: " & @error & @CRLF & "@EXTENDED: " & @extended)
    Else
        Return $result[2]
    EndIf
EndFunc ;==>GetWindowModuleFileName

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FUNCTIONS END;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END OF SCRIPT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

I have tested only on Windows XP

Edited by inferno123
Link to comment
Share on other sites

  • 5 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...