/************************
CheckMutex.nsi
************************/
;NSIS 安裝程序與卸載程序互相檢查互斥的例子
;編寫:zhfi
#定義自己的互斥名稱:#
#注意安:裝程序不能與卸載程序相同!#
!define MyMutex_Install "MyMutex_Install"
!define MyMutex_UnInstall "MyMutex_UnInstall"
!include "LogicLib.nsh"
;安裝程序部分
Function .onInit
InitPluginsDir
Call CreateMutex
FunctionEnd
Function CreateMutex
#檢查安裝互斥:#
ReCheck:
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_Install}") i .R1 ?e'
Pop $R0
System::Call 'kernel32::CloseHandle(i R1) i.s'
#檢查卸載互斥:#
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_UnInstall}") i .R3 ?e'
Pop $R2
System::Call 'kernel32::CloseHandle(i R3) i.s'
#判斷安裝/卸載互斥的存在#
${If} $R0 != 0
MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "安裝程序已經運行!" IdRetry ReCheck
Quit
${ElseIf} $R2 != 0
MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "卸載程序已經運行!" IdRetry ReCheck
Quit
${Else}
#創建安裝互斥:#
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_Install}") i .R1 ?e'
Pop $R0
StrCmp $R0 0 +2
Quit
${EndIf}
FunctionEnd
;卸載程序部分
Function un.onInit
InitPluginsDir
Call un.CreateMutex
FunctionEnd
Function Un.CreateMutex
#檢查安裝互斥:#
ReCheck:
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_Install}") i .R1 ?e'
Pop $R0
System::Call 'kernel32::CloseHandle(i R1) i.s'
#檢查卸載互斥:#
CheckUnInstall:
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_UnInstall}") i .R3 ?e'
Pop $R2
System::Call 'kernel32::CloseHandle(i R3) i.s'
#判斷安裝/卸載互斥的存在#
${If} $R0 != 0
MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "安裝程序已經運行!" IdRetry ReCheck
Quit
${ElseIf} $R2 != 0
MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "卸載程序已經運行!" IdRetry ReCheck
Quit
${Else}
#創建卸載互斥:#
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_UnInstall}") i .R1 ?e'
Pop $R0
StrCmp $R0 0 +2
Quit
${EndIf}
FunctionEnd