杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2201|回复: 0

NSIS 程序互斥函数

[复制链接]
发表于 2017-2-26 11:07:43 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. /************************
  2.       CheckMutex.nsi
  3. ************************/
  4. ;NSIS 安装程序与卸载程序互相检查互斥的例子
  5. ;编写:zhfi
  6. #定义自己的互斥名称:#
  7. #注意安:装程序不能与卸载程序相同!#
  8. !define MyMutex_Install     "MyMutex_Install"
  9. !define MyMutex_UnInstall   "MyMutex_UnInstall"

  10. ;--------------------------------
  11. ;Include Modern UI

  12. !include "MUI.nsh"
  13. !include "LogicLib.nsh"

  14. ;--------------------------------
  15. ;General

  16.   ;Name and file
  17.   Name "Mutex Test"
  18.   OutFile "Mutex.exe"

  19. ;--------------------------------
  20. #添加安装页面:#
  21.   !insertmacro MUI_PAGE_Welcome
  22.   !insertmacro MUI_PAGE_InstFiles
  23.   !insertmacro MUI_PAGE_Finish
  24. #添加卸载页面:#
  25.   !insertmacro MUI_UNPAGE_Welcome
  26.   !insertmacro MUI_UNPAGE_InstFiles
  27.   !insertmacro MUI_UNPAGE_Finish

  28. ;--------------------------------
  29. ;加入语言文件

  30.   !insertmacro MUI_LANGUAGE "SimpChinese"

  31. ;--------------------------------
  32. ;安装程序部分

  33. Function .onInit
  34. InitPluginsDir
  35. Call CreateMutex
  36. FunctionEnd

  37. Function CreateMutex
  38. #检查安装互斥:#
  39. ReCheck:
  40.   System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_Install}") i .R1 ?e'
  41. Pop $R0
  42.   System::Call 'kernel32::CloseHandle(i R1) i.s'
  43. #检查卸载互斥:#
  44.   System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_UnInstall}") i .R3 ?e'
  45. Pop $R2
  46.   System::Call 'kernel32::CloseHandle(i R3) i.s'
  47. #判断安装/卸载互斥的存在#
  48. ${If} $R0 != 0
  49.   MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "安装程序已经运行!" IdRetry ReCheck
  50. Quit
  51. ${ElseIf} $R2 != 0
  52.   MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "卸载程序已经运行!" IdRetry ReCheck
  53. Quit
  54. ${Else}
  55. #创建安装互斥:#
  56.   System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_Install}") i .R1 ?e'
  57. Pop $R0
  58.   StrCmp $R0 0 +2
  59. Quit
  60. ${EndIf}
  61. FunctionEnd

  62. Section Install
  63.   SetOutPath $EXEDIR
  64.   WriteUninstaller "$EXEDIR\Uninstall.exe"
  65. SectionEnd

  66. ;--------------------------------
  67. ;卸载程序部分

  68. Function un.onInit
  69. InitPluginsDir
  70. Call un.CreateMutex
  71. FunctionEnd

  72. Function Un.CreateMutex
  73. #检查安装互斥:#
  74. ReCheck:
  75.   System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_Install}") i .R1 ?e'
  76. Pop $R0
  77.   System::Call 'kernel32::CloseHandle(i R1) i.s'
  78. #检查卸载互斥:#
  79. CheckUnInstall:
  80.   System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_UnInstall}") i .R3 ?e'
  81. Pop $R2
  82.   System::Call 'kernel32::CloseHandle(i R3) i.s'
  83. #判断安装/卸载互斥的存在#
  84. ${If} $R0 != 0
  85.   MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "安装程序已经运行!" IdRetry ReCheck
  86. Quit
  87. ${ElseIf} $R2 != 0
  88.   MessageBox MB_RetryCancel|MB_ICONEXCLAMATION "卸载程序已经运行!" IdRetry ReCheck
  89. Quit
  90. ${Else}
  91. #创建卸载互斥:#
  92.   System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MyMutex_UnInstall}") i .R1 ?e'
  93. Pop $R0
  94.   StrCmp $R0 0 +2
  95. Quit
  96. ${EndIf}
  97. FunctionEnd

  98. Section Uninstall
  99.   SetOutPath $EXEDIR
  100.   Delete "$EXEDIR\Uninstall.exe"
  101. SectionEnd

  102. #脚本结束!#
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-19 15:11 , Processed in 0.110375 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表