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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值