NSIS 安装路径验证

NSIS install path validation

我想验证用户输入的安装路径 select。我不知道如何检查它,所以它看起来像这样:

现在我有这个:

  Function StrStr
  Exch  ; st=haystack,old, =needle
  Exch    ; st=old,haystack
  Exch  ; st=old,old, =haystack
  Push 
  Push 
  Push 
  StrLen  
  StrCpy  0
  ; =needle
  ; =haystack
  ; =len(needle)
  ; =cnt
  ; =tmp
  loop:
    StrCpy    
    StrCmp   done
    StrCmp  "" done
    IntOp   + 1
    Goto loop
  done:
  StrCpy   "" 
  Pop 
  Pop 
  Pop 
  Pop 
  Exch 
FunctionEnd

Function .onVerifyInstDir
Push "$INSTDIR"
Push " "
Call StrStr
Pop [=10=]
StrCpy [=10=] [=10=] 1
StrCmp [=10=] " " 0 +2
  Abort
FunctionEnd

路径中有 space 时拒绝安装。我需要对此进行修改,以便 Program Files 将成为该规则的唯一例外。另外,打印错误消息会有帮助

这个限制对我来说毫无意义。一些遗留应用程序无法处理路径中的空格,但当然也包括 Program Files 文件夹(尽管 progra~1 hack 可以用作变通方法 if 短名称生成处于活动状态)。

NSIS 没有直接在页面上显示 error/warning 消息的特定方法,但您可以更改 UI and/or 显示气球中的现有文本。

!include WinMessages.nsh
!define /IfNDef EM_SHOWBALLOONTIP 0x1503
!define /IfNDef EM_HIDEBALLOONTIP 0x1504

!define DIRPAGE_CHANGETEXT ; Remove this line to disable the text change
!define DIRPAGE_BALLOON    ; Remove this line to disable the balloon
Function .onVerifyInstDir
    FindWindow  "#32770" "" $HWNDPARENT
!ifdef DIRPAGE_CHANGETEXT
    GetDlgItem   1006 ; IDC_INTROTEXT
    LockWindow on
!endif
    StrCpy  0
    loop:
        StrCpy  $InstDir 1 
        StrCmp  '' valid ; End of string
        StrCmp  ' ' found_space
        IntOp   + 1
        Goto loop
valid:
!ifdef DIRPAGE_CHANGETEXT
    SetCtlColors  SYSCLR:18 SYSCLR:15
    SendMessage  ${WM_SETTEXT} "" "STR:$(^DirText)"
    LockWindow off
!endif
!ifdef DIRPAGE_BALLOON
    GetDlgItem   1019
    SendMessage  ${EM_HIDEBALLOONTIP} "" "" ; Not required?
!endif
    Return
found_space:
    StrLen  "$ProgramFiles\"
    StrCpy  "$InstDir\" 
    StrCmp  "$ProgramFiles\" valid
!ifdef DIRPAGE_CHANGETEXT
    SetCtlColors  ff0000 transparent
    SendMessage  ${WM_SETTEXT} "" "STR:Paths with spaces are not allowed, except for $ProgramFiles for some reason!"
    LockWindow off
!endif
!ifdef DIRPAGE_BALLOON
    GetDlgItem   1019
    System::Call '*(&l${NSIS_PTR_SIZE},w "Bad path!", w "Spaced not allowed in path!",p 3)p.r2'
    SendMessage  ${EM_SHOWBALLOONTIP} ""  ; This will only work on XP and later (and you must use "XPStyle on")
    System::Free 
!endif
    Abort
FunctionEnd

XPStyle on
Page Directory

.onVerifyInstDir 中调用 Abort 时,下一步按钮被禁用。如果你想在用户点击下一步时显示一个MessageBox那么你不能在.onVerifyInstDir中调用Abort,你将不得不使用页面离开函数回调(你必须在其中验证路径并可能调用 MessageBox+Abort)。