【问题标题】:NSIS install path validationNSIS 安装路径验证
【发布时间】:2020-08-12 02:50:47
【问题描述】:

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

  • 您不能选择带空格的路径(Program Files 除外)
  • 点击“安装”时会提示需要更改安装目录的错误

现在我有这个:

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

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

当路径中有任何空间时,它拒绝安装。我需要对此进行修改,因此 Program Files 将是该规则的唯一例外。此外,打印错误消息会有所帮助

【问题讨论】:

    标签: nsis electron-builder


    【解决方案1】:

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

    NSIS 没有直接在页面上显示错误/警告消息的特定方式,但您可以更改 UI 中的现有文本和/或显示气球。

    !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 $9 "#32770" "" $HWNDPARENT
    !ifdef DIRPAGE_CHANGETEXT
        GetDlgItem $3 $9 1006 ; IDC_INTROTEXT
        LockWindow on
    !endif
        StrCpy $1 0
        loop:
            StrCpy $2 $InstDir 1 $1
            StrCmp $2 '' valid ; End of string
            StrCmp $2 ' ' found_space
            IntOp $1 $1 + 1
            Goto loop
    valid:
    !ifdef DIRPAGE_CHANGETEXT
        SetCtlColors $3 SYSCLR:18 SYSCLR:15
        SendMessage $3 ${WM_SETTEXT} "" "STR:$(^DirText)"
        LockWindow off
    !endif
    !ifdef DIRPAGE_BALLOON
        GetDlgItem $3 $9 1019
        SendMessage $3 ${EM_HIDEBALLOONTIP} "" "" ; Not required?
    !endif
        Return
    found_space:
        StrLen $1 "$ProgramFiles\"
        StrCpy $2 "$InstDir\" $1
        StrCmp $2 "$ProgramFiles\" valid
    !ifdef DIRPAGE_CHANGETEXT
        SetCtlColors $3 ff0000 transparent
        SendMessage $3 ${WM_SETTEXT} "" "STR:Paths with spaces are not allowed, except for $ProgramFiles for some reason!"
        LockWindow off
    !endif
    !ifdef DIRPAGE_BALLOON
        GetDlgItem $3 $9 1019
        System::Call '*(&l${NSIS_PTR_SIZE},w "Bad path!", w "Spaced not allowed in path!",p 3)p.r2'
        SendMessage $3 ${EM_SHOWBALLOONTIP} "" $2 ; This will only work on XP and later (and you must use "XPStyle on")
        System::Free $2
    !endif
        Abort
    FunctionEnd
    
    XPStyle on
    Page Directory
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多