对于有过编程基础的人来说,防止程序重复同时运行是一件必做而又严谨的事情,NSIS打包安装程序也不例外,下面小编将给大家分享NSIS 程序互斥函数 检测防止程序重复同时运行。
/************************
CheckMutex.nsi
************************/
;NSIS 安装程序与卸载程序互相检查互斥的例子
;编写:ITmemo.cn
#定义自己的互斥名称:#
#注意安:装程序不能与卸载程序相同!#
!define MyMutex_Install "MyMutex_Install"
!define MyMutex_UnInstall "MyMutex_UnInstall"
;--------------------------------
;Include Modern UI
!include "MUI.nsh"
!include "LogicLib.nsh"
;--------------------------------
;General
;Name and file
Name "Mutex Test"
OutFile "Mutex.exe"
;--------------------------------
#添加安装页面:#
!insertmacro MUI_PAGE_Welcome
!insertmacro MUI_PAGE_InstFiles
!insertmacro MUI_PAGE_Finish
#添加卸载页面:#
!insertmacro MUI_UNPAGE_Welcome
!insertmacro MUI_UNPAGE_InstFiles
!insertmacro MUI_UNPAGE_Finish
;--------------------------------
;加入语言文件
!insertmacro MUI_LANGUAGE "SimpChinese"
;--------------------------------
;安装程序部分
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
Section Install
SetOutPath $EXEDIR
WriteUninstaller "$EXEDIR\Uninstall.exe"
SectionEnd
;--------------------------------
;卸载程序部分
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
Section Uninstall
SetOutPath $EXEDIR
Delete "$EXEDIR\Uninstall.exe"
SectionEnd
#脚本结束!#
正在阅读:
高三教室励志横幅标语震撼人心大全08-03
沪教版二年级上册英语教案三篇07-31
小学生拜年300字作文【六篇】01-24
2016年小学美术教学工作计划书05-04
2023年北京丰台小学排名前十学校参考12-10
2017年湖北中建长江建设投资有限公司招聘工作人员【36名】03-29
2022年10月北京东城自学考试笔试课程准考证打印时间及入口(10月17日-10月23日)07-19
初中生需要微笑作文600字【5篇】09-10
小学六年级优秀作文:垃圾激起邻里情12-04
2017年北京延庆中考成绩公布时间12-25