FCT plug-in: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Close or Terminate moved to FCT plug-in)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{PageAuthor|Takhir}}
{{PageAuthor|Takhir}}


"fct" stands for "File Close or Terminate".
"FCT" stands for "Find and Close or Terminate".


== Links ==
== Links ==
Line 8: Line 8:


== Description ==
== Description ==
Plug-in terminates application in 2 steps - first send WM_CLOSE to it's main window, then (if still alive) terminates application. Has sync/async (background) modes, async is good for slowly exiting applications (maximum delay for one application may be up to 2xTIMEOUT) and multiple applications (multithreading). Plug-in closes or terminates all process with top level  windows of such kind using EnumWindows(). Never closes installer $HWNDPARENT. Size is 2 kB in zip, so if you need WIN API calls for correct process termination only, this may replace System plug-in with less size and simple script code. Final package size decreases is 4 kB less in compressed file for installer and 8 kB if it replaces System plug-in both in installer and uninstaller.<br>
Plug-in terminates application in 2 steps - first send WM_CLOSE to its main window, then (if still alive) terminates application. Has sync/async (background) modes, async is good for slowly exiting applications and multiple applications (multithreading). Plug-in closes or terminates all process with top level  windows of such kind using EnumWindows(). Never closes installer $HWNDPARENT. Size is 2 kB in zip, so if you need WIN API calls for correct process termination only, this may replace System plug-in with less size and simple script code. Final package size is 4 kB less in compressed file for installer only and 8 kB if it replaces System plug-in both in installer and uninstaller.<br>
 


== Syntax ==
== Syntax ==
=== "fct" DLL function ===
=== "fct" DLL function ===
<highlight-nsis>
<highlight-nsis>
fct::fct [/NOUNLOAD] WINDOW_CLASS WINDOW_TITLE [/ASYNC] [/PART] [/TIMEOUT=xxx] [/UDATA=xxx] [/END]
fct::fct [/NOUNLOAD] [/WC CLASS | /WCP CLASS_PART]  [/WT WINDOW_TITLE | /WTP TITLE_PART] \
[/ASYNC] [/MSGONLY] [/SCCLOSE] [/TIMEOUT xxx] [/UDATA xxx] [/QUESTION TERM_QUEST] [/END]
</highlight-nsis>
</highlight-nsis>
: Searchs for all windows with predefined 'class' and 'title' (or it's part). Use "" if class or title are not defined. In async mode plug-in returns running thread handle, in the sync one - still alive applications count (i.e. how many application not exited after WM_CLOSE and were not terminated using TerminateProcess() ). I.e. 0 is OK. Use 'wait' with handle as parameter to get final result for this thread.
: Searchs for all windows with predefined 'class' and 'title' (or it's part if /WCP or /WTP keys used). In async mode plug-in returns running terminator thread handle, in the sync one - still alive applications count (i.e. how many application not exited after WM_CLOSE and were not terminated using TerminateProcess() ). Returnes "-1" if /QUESTION was defined, application not exited after WM_CLOSE and user selected Cancel on the MessageBox. I.e. 0 is OK. Use 'wait' with handle as parameter to get final result for async mode.
; WT
: WINDOW_TITLE is a full window title
; WTP
: TITLE_PART is only a part of window title (caption)
; WC
: WINDOW_CLASS is a full window class name
; WCP
: CLASS_PART is only a part of window class name
; ASYNC
; ASYNC
: Not waits for process exit. Use 'wait' call if you want to get result
: Not waits for process exit. Use 'wait' call if you want to get result
; MSGONLY
: Not attempts to terminate process
; SCCLOSE
: uses WM_SYSCOMMAND SC_CLOSE instead of WM_CLOSE (for Window Explorer mainly)
; TIMEOUT
; TIMEOUT
: timeout for SendMessage() and WaitForSingleObject(). Default is 1000 (ms).
: timeout for SendMessage() and WaitForSingleObject(). Default is 1000 (ms).
; PART
: WINDOW_TITLE is only a part of window title (caption)
; UDATA
; UDATA
: if few applications have "32770" class based windows and all titles include only a fragment of WINDOW_TITLE, situation becomes confusing. This case developer can preset specific GWL_USERDATA value to his app's window, plug-in will check, for example, /udata=0x12fe3400.
: if few applications have "32770" class based windows and all titles include only a fragment of TITLEPART, situation becomes confusing. This case developer can preset specific GWL_USERDATA value to his app's window, plug-in will check, for example, /udata=0x12fe3400.
; QUESTION
: TERM_QUEST - text to display on the MessageBox if application not exited on WM_CLOSE. Skipped in the Silent mode.
; END
: Allows to limit plug-in stack reading (optional, required if you stores other vars in the stack).


=== "wait" DLL function ===
=== "wait" DLL function ===
<highlight-nsis>fct::wait handle</highlight-nsis>
<highlight-nsis>fct::wait handle</highlight-nsis>
: Waits for process exit.
: Waits for terminator thread exit in async mode.
; handle
; handle
: Control thread handle returned by 'fct' call in the /ASYNC mode.
: Control thread handle returned by 'fct' call in the /ASYNC mode.
Line 35: Line 49:
Sync execution:
Sync execution:
<highlight-nsis>
<highlight-nsis>
     fct::fct '${WND_CLASS}' '${WND_TITLE}' /timeout=2000 /part '/question=${Msg}'
     fct::fct /WC '${WND_CLASS}' /WTP '${MY_TITLE}' /TIMEOUT 2000 /QUESTION 'Terminate?'
     Pop $0
     Pop $0
    StrCmp $0 "-1" 0 +2
    Abort "Terminated by user"
     MessageBox MB_OK "Still Alive Count=$0"
     MessageBox MB_OK "Still Alive Count=$0"
</highlight-nsis>
</highlight-nsis>
Line 42: Line 58:
Async execution:
Async execution:
<highlight-nsis>
<highlight-nsis>
     fct::fct /nounload '${WND_CLASS}' '${WND_TITLE}' /async /timeout=2000 /part '/question=${Msg}'
     fct::fct /NOUNLOAD /WC '${WND_CLASS}' /ASYNC
; You can place some code here. We skipped Pop and Push for hThread - it just sits in the stack.  
    ;You can place some code here. We skipped Pop and Push for hThread -  
    ;it just sits in the stack. 'Question' not defined - silent execution.
     fct::wait
     fct::wait
     Pop $0
     Pop $0

Latest revision as of 15:12, 30 November 2010

Author: Takhir (talk, contrib)


"FCT" stands for "Find and Close or Terminate".

Links

Download:
Fct.zip (8 KB)

Description

Plug-in terminates application in 2 steps - first send WM_CLOSE to its main window, then (if still alive) terminates application. Has sync/async (background) modes, async is good for slowly exiting applications and multiple applications (multithreading). Plug-in closes or terminates all process with top level windows of such kind using EnumWindows(). Never closes installer $HWNDPARENT. Size is 2 kB in zip, so if you need WIN API calls for correct process termination only, this may replace System plug-in with less size and simple script code. Final package size is 4 kB less in compressed file for installer only and 8 kB if it replaces System plug-in both in installer and uninstaller.

Syntax

"fct" DLL function

fct::fct [/NOUNLOAD] [/WC CLASS | /WCP CLASS_PART]  [/WT WINDOW_TITLE | /WTP TITLE_PART] \
 [/ASYNC] [/MSGONLY] [/SCCLOSE] [/TIMEOUT xxx] [/UDATA xxx] [/QUESTION TERM_QUEST] [/END]
Searchs for all windows with predefined 'class' and 'title' (or it's part if /WCP or /WTP keys used). In async mode plug-in returns running terminator thread handle, in the sync one - still alive applications count (i.e. how many application not exited after WM_CLOSE and were not terminated using TerminateProcess() ). Returnes "-1" if /QUESTION was defined, application not exited after WM_CLOSE and user selected Cancel on the MessageBox. I.e. 0 is OK. Use 'wait' with handle as parameter to get final result for async mode.
WT
WINDOW_TITLE is a full window title
WTP
TITLE_PART is only a part of window title (caption)
WC
WINDOW_CLASS is a full window class name
WCP
CLASS_PART is only a part of window class name
ASYNC
Not waits for process exit. Use 'wait' call if you want to get result
MSGONLY
Not attempts to terminate process
SCCLOSE
uses WM_SYSCOMMAND SC_CLOSE instead of WM_CLOSE (for Window Explorer mainly)
TIMEOUT
timeout for SendMessage() and WaitForSingleObject(). Default is 1000 (ms).
UDATA
if few applications have "32770" class based windows and all titles include only a fragment of TITLEPART, situation becomes confusing. This case developer can preset specific GWL_USERDATA value to his app's window, plug-in will check, for example, /udata=0x12fe3400.
QUESTION
TERM_QUEST - text to display on the MessageBox if application not exited on WM_CLOSE. Skipped in the Silent mode.
END
Allows to limit plug-in stack reading (optional, required if you stores other vars in the stack).

"wait" DLL function

fct::wait handle
Waits for terminator thread exit in async mode.
handle
Control thread handle returned by 'fct' call in the /ASYNC mode.

Example

Sync execution:

    fct::fct /WC '${WND_CLASS}' /WTP '${MY_TITLE}' /TIMEOUT 2000 /QUESTION 'Terminate?'
    Pop $0
    StrCmp $0 "-1" 0 +2
    Abort "Terminated by user" 
    MessageBox MB_OK "Still Alive Count=$0"

Async execution:

    fct::fct /NOUNLOAD /WC '${WND_CLASS}' /ASYNC
    ;You can place some code here. We skipped Pop and Push for hThread - 
    ;it just sits in the stack. 'Question' not defined - silent execution.
    fct::wait
    Pop $0
    MessageBox MB_OK "Still Alive Count=$0"