Detect if a window is open and close it.

From NSIS Wiki
Jump to navigationJump to search
Author: Poromenos (talk, contrib)


Description

Using this function you can detect if a program is running and halt until the user closes it.

The Function

;------------------------------------------------------------------------------
; un.FindWindowClose
;
; Closes a window if open. Also prompts user if closing fails.
; Use by passing the window class and title on the stack.
; You must pass both even if one is empty (i.e. "").
;
; Usage: 
;   Push ThunderRT6FormDC
;   Push "Visual Basic Form Name"
;   Call un.FindWindowClose
;
 
Function un.FindWindowClose
    Exch $0
    Exch
    Exch $1
    Push $2
    Push $3
    find:
        FindWindow $2 $1 $0
        IntCmp $2 0 nowindow
        SendMessage $2 16 "" ""
        Sleep 500
        FindWindow $2 $1 $0
        IntCmp $2 0 nowindow
            MessageBox MB_OK|MB_ICONSTOP "An instance of the program is running. Please close it and press OK to continue."
            Goto find
    nowindow:
    Pop $3
    Pop $2
    Pop $1
    Pop $0
FunctionEnd