Eli*_*jah 11
如果我们假设cmd.exe对于所有Windows安装都是相同的绝对路径(可能,但不是万无一失),你可以找出.lnk文件来启动这样的cmd
cmd.exe /c start /d. your command here
Run Code Online (Sandbox Code Playgroud)
/ d将目录设置为.lnk文件的目录
启动命令可能还有其他有用的选项(例如/ b)
虽然快捷方式可以包含目标的相对路径(.lnk文件有一个名为SLDF_HAS_RELPATH的标志),但NSIS不支持创建除"普通"快捷方式之外的任何内容,因此您需要直接编写二进制数据(.lnk格式)非常稳定,已被MS记录
!macro FileWriteHexBytes h b
push ${h}
push ${b}
call FileWriteHexBytes
!macroend
Function FileWriteHexBytes
exch $9
exch
exch $0
push $1
push $2
loop:
StrCpy $2 $9 2
StrLen $1 $2
IntCmp $1 2 0 end
FileWriteByte $0 "0x$2"
StrCpy $9 $9 "" 2
goto loop
end:
pop $2
pop $1
pop $0
pop $9
FunctionEnd
Function CreateRelativeLnk
exch $9
exch
exch $0
push $1
FileOpen $0 "$0" w
StrCmp $0 "" clean
!insertmacro FileWriteHexBytes $0 "4C0000000114020000000000C000000000000046"
!insertmacro FileWriteHexBytes $0 48010400 ;flags
!insertmacro FileWriteHexBytes $0 00000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000
StrLen $1 $9 ;must be < 255!
FileWriteByte $0 $1
FileWriteByte $0 0
FileWrite $0 "$9" ;relative target path
!if 0
;The icon is problematic, does not seem like it works with relative paths (but you can use system icons...)
StrCpy $9 "explorer.exe"
StrLen $1 $9
FileWriteByte $0 $1
FileWriteByte $0 0
FileWrite $0 "$9"
!else
!insertmacro FileWriteHexBytes $0 05003e2e657865 ;fake default .exe icon
!endif
clean:
FileClose $0
pop $1
pop $0
pop $9
FunctionEnd
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
push "$temp\testlink.lnk"
push "testdir\testapp.exe" ;full path to this is $temp\testdir\testapp.exe
call CreateRelativeLnk
Run Code Online (Sandbox Code Playgroud)
虽然生成的.lnk似乎有效,但我不确定是否会在生产代码中使用它
一个更好的解决方案是创建一个像Oleg建议的小NSIS应用程序(NSIS应用程序可以在.exe的末尾包含嵌入式数据,它可以在运行时等自己读取..)
| 归档时间: |
|
| 查看次数: |
17253 次 |
| 最近记录: |