NSIS—程序互斥函數(只能運行一個安裝程序,安裝時不能卸載)

/************************
      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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章