Installation 在工作目录中使用环境变量创建快捷方式

Installation 在工作目录中使用环境变量创建快捷方式,installation,nsis,shortcut,Installation,Nsis,Shortcut,目前我正在创建一个快捷方式,如下所示: SetShellVarContext all SetOutPath "$INSTDIR" CreateShortCut "$SMPROGRAMS\MyApp.lnk" "$INSTDIR\MyApp.exe" 我想将此快捷方式的工作目录从C:\Program Files\MyApp更改为%UserProfile% 棘手的是,我不想扩展%UserProfile%,我想将其作为一个环境变量,因此程序在当前用户的profile目录中启动 我可以通过NSIS实现

目前我正在创建一个快捷方式,如下所示:

SetShellVarContext all
SetOutPath "$INSTDIR"
CreateShortCut "$SMPROGRAMS\MyApp.lnk" "$INSTDIR\MyApp.exe"
我想将此快捷方式的工作目录从
C:\Program Files\MyApp
更改为
%UserProfile%

棘手的是,我不想扩展
%UserProfile%
,我想将其作为一个环境变量,因此程序在当前用户的profile目录中启动

我可以通过NSIS实现这一点吗?如果不是,最简单的解决方法是什么


参考:

NSIS使用SetOutPath(
$OutDir
)设置的路径调用快捷方式

可以将
$OutDir
设置为无效路径,然后调用CreateShortcut:

Push $OutDir ; Save
StrCpy $OutDir "%UserProfile%"
CreateShortcut "$temp\test1.lnk" "$sysdir\Calc.exe"
Pop $OutDir ; Restore
它确实有效,但可能有点违反规则。您也可以在不依赖未记录的NSIS怪癖的情况下完成此操作:

!define CLSCTX_INPROC_SERVER 1
!define STGM_READWRITE 2
!define IID_IPersistFile {0000010b-0000-0000-C000-000000000046}
!define CLSID_ShellLink {00021401-0000-0000-c000-000000000046}
!define IID_IShellLinkA {000214ee-0000-0000-c000-000000000046}
!define IID_IShellLinkW {000214f9-0000-0000-c000-000000000046}
!ifdef NSIS_UNICODE
!define IID_IShellLink ${IID_IShellLinkW}
!else
!define IID_IShellLink ${IID_IShellLinkA}
!endif

!include LogicLib.nsh
Function Lnk_SetWorkingDirectory
Exch $9 ; New working directory 
Exch
Exch $8 ; Path
Push $0 ; HRESULT
Push $1 ; IShellLink
Push $2 ; IPersistFile
System::Call 'OLE32::CoCreateInstance(g "${CLSID_ShellLink}",i 0,i ${CLSCTX_INPROC_SERVER},g "${IID_IShellLink}",*i.r1)i.r0'
${If} $0 = 0
    System::Call `$1->0(g "${IID_IPersistFile}",*i.r2)i.r0`
    ${If} $0 = 0
        System::Call `$2->5(wr8,i${STGM_READWRITE})i.r0` ; Load
        ${If} $0 = 0
            System::Call `$1->9(tr9)i.r0` ; SetWorkingDirectory
            ${If} $0 = 0
                System::Call `$2->6(i0,i0)i.r0` ; Save
            ${EndIf}
        ${EndIf}
        System::Call `$2->2()` ; Release
    ${EndIf}
    System::Call `$1->2()` ; Release
${EndIf}
StrCpy $9 $0
Pop $1
Pop $0
Pop $8
Exch $9
FunctionEnd

Section
CreateShortcut "$temp\test2.lnk" "$sysdir\Calc.exe"
Push "$temp\test2.lnk"
Push "%UserProfile%"
Call Lnk_SetWorkingDirectory 
Pop $0
DetailPrint HRESULT=$0 ; 0 = success
SectionEnd

需要注意的是,IShellLink::SetWorkingDirectory并没有说明支持未展开的环境变量,但它们似乎确实有效。

谢谢!除了你的解决方案,我所能看到的就是调用VB脚本。您的第二个解决方案有点过于繁琐,但您的第一个解决方案应该不会那么糟糕(一个普通字符串被传递给
$OutDir
IShellLink::SetWorkingDirectory
,它们只是照原样处理)。