当前位置:Nanlon > 经验教程 > 正文

NSIS 制作软件单文件版示例

一个 NSIS 制作单文件的示例:

基本上包含了单文件打包基本的一些操作(释放、运行、删除文件,写注册表、删注册表,注册与反注册DLL,关联文件等)

# Include
!include x64.nsh

# APP
!define APPNAME "BCompare"
!define APP_VER "4.3.3.24545"
!define APP_PUB "Scooter Software"
!define DIR_TMP "BCompare-64bit43324545"

# VER
VIProductVersion "${APP_VER}"
VIAddVersionKey /LANG=2052 "ProductName"     "Beyond Compare 4"
VIAddVersionKey /LANG=2052 "ProductVersion"  "${APP_VER}"
VIAddVersionKey /LANG=2052 "LegalCopyright"  "Copyright © 2020 ${APP_PUB}, Inc."
VIAddVersionKey /LANG=2052 "FileDescription" "Beyond Compare 64bit"
VIAddVersionKey /LANG=2052 "FileVersion"     "${APP_VER}"

# SET
Unicode true
SetOverwrite try
SilentInstall silent
RequestExecutionLevel admin

# Icon
Icon ".\resource\${APPNAME}.ico"

# Setup
Name "${APPNAME}-64bit"
OutFile "${APPNAME}-64bit-${APP_VER}.exe"

# --- Runtime ---

Function .onInit
# --- InstInit ---

    InitPluginsDir
    
    # Don't run on 32-bit system
    ${If} ${RunningX64}
    ${Else}
    MessageBox MB_OK|MB_ICONEXCLAMATION "您的操作系统为32位版本,无法运行64位版本的软件!"
    Abort
    ${EndIf}
    
    # Create Mutex
    System::Call `kernel32::CreateMutex(i0,i0,t"${APPNAME}_installer")i.r1?e`
    Pop $R0
    StrCmp $R0 0 +3
    MessageBox MB_OK|MB_ICONEXCLAMATION "程序已在运行,不能重复打开!"
    Abort
    
    # Out Files
    SetOutPath "$TEMP\${DIR_TMP}"
    File /r ".\${APPNAME}\64bit\*.*"
    
    # Reg Dll
    ${DisableX64FSRedirection}
    ExecWait `regsvr32 /s "$TEMP\${DIR_TMP}\BCShellEx64.dll"`
    ${EnableX64FSRedirection}
    
    ExecWait `regsvr32 /s "$TEMP\${DIR_TMP}\BCShellEx.dll"`
    
    # Write Reg
    SetRegView 64    
    WriteRegStr   HKLM "SOFTWARE\Classes\*\shellex\ContextMenuHandlers\CirrusShellEx" "" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    WriteRegStr   HKLM "SOFTWARE\Classes\.bcss" "" "BeyondCompare.Snapshot"
    WriteRegStr   HKLM "SOFTWARE\Classes\.bcpkg" "" "BeyondCompare.SettingsPackage"
    
    WriteRegStr   HKLM "SOFTWARE\Classes\BeyondCompare.SettingsPackage" "" "Beyond Compare Settings Package"
    WriteRegStr   HKLM "SOFTWARE\Classes\BeyondCompare.SettingsPackage\DefaultIcon" "" "$TEMP\${DIR_TMP}\${APPNAME}.exe,0"
    WriteRegStr   HKLM "SOFTWARE\Classes\BeyondCompare.SettingsPackage\shell\open\command" "" `"$TEMP\${DIR_TMP}\${APPNAME}.exe" "%1"`
    
    WriteRegStr   HKLM "SOFTWARE\Classes\BeyondCompare.Snapshot" "" "Beyond Compare Snapshot"
    WriteRegStr   HKLM "SOFTWARE\Classes\BeyondCompare.Snapshot\DefaultIcon" "" "$TEMP\${DIR_TMP}\${APPNAME}.exe,0"
    WriteRegStr   HKLM "SOFTWARE\Classes\BeyondCompare.Snapshot\shell\open\command" "" `"$TEMP\${DIR_TMP}\${APPNAME}.exe" "%1"`
    
    WriteRegStr   HKLM "SOFTWARE\Classes\Directory\shellex\ContextMenuHandlers\CirrusShellEx" "" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    WriteRegStr   HKLM "SOFTWARE\Classes\Folder\shellex\ContextMenuHandlers\CirrusShellEx" "" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    WriteRegStr   HKLM "SOFTWARE\Classes\lnkfile\shellex\ContextMenuHandlers\CirrusShellEx" "" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    
    WriteRegStr   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}" "" "CirrusShellEx"
    WriteRegStr   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32" "" "$TEMP\${DIR_TMP}\BCShellEx64.dll"
    WriteRegStr   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32" "ThreadingModel" "Apartment"
    
    WriteRegStr   HKLM "SYSTEM\CurrentControlSet\Services\EventLog\Application\Beyond Compare 4" "EventMessageFile" "$TEMP\${DIR_TMP}\${APPNAME}.exe"
    WriteRegDWORD HKLM "SYSTEM\CurrentControlSet\Services\EventLog\Application\Beyond Compare 4" "TypesSupported" "0x00000007"
    
    WriteRegStr   HKCU "SOFTWARE\Scooter Software\Beyond Compare" "ExePath" "$TEMP\${DIR_TMP}\${APPNAME}.exe"
    WriteRegDWORD HKCU "SOFTWARE\Scooter Software\Beyond Compare" "SupportsMerge" "0x00000001"
    
    WriteRegStr   HKCU "SOFTWARE\Scooter Software\Beyond Compare 4" "ExePath" "$TEMP\${DIR_TMP}\${APPNAME}.exe"
    WriteRegDWORD HKCU "SOFTWARE\Scooter Software\Beyond Compare 4" "SupportsMerge" "0x00000001"
    WriteRegStr   HKCU "SOFTWARE\Scooter Software\Beyond Compare 4\BcShellEx" "ArchiveMasks" "*.7z;*.7z.001;*.arj;*.bcpkg;*.bcs;*.bcss;*.bz;*.bz2;*.cab;*.chm;*.cpio;*.cramfs;*.deb;*.dmg;*.ear;*.ext2;*.ext3;*.ext4;*.fat;*.flv;*.gz;*.hfs;*.hxi;*.hxq;*.hxr;*.hxs;*.hxw;*.img;*.iso;*.jar;*.lha;*.lzh;*.lzma;*.lzma86;*.mbr;*.nsis;*.ntfs;*.pmd;*.rar;*.rpm;*.safariextz;*.scap;*.squashfs;*.swf;*.swm;*.tar;*.tar.bz2;*.tar.gz;*.tar.lzma;*.tar.xz;*.taz;*.tbz;*.tbz2;*.te;*.tgz;*.tlz;*.txz;*.war;*.wim;*.xar;*.xz;*.z;*.zip;*.zipx;"
    WriteRegStr   HKCU "SOFTWARE\Scooter Software\Beyond Compare 4\BcShellEx" "RegistryViewer" "注册表比较(R)"
    WriteRegStr   HKCU "SOFTWARE\Scooter Software\Beyond Compare 4\BcShellEx" "Viewers" "文本比较(T);表格比较(B);16 进制比较(H);MP3 比较;图片比较(P);注册表比较(R);版本比较(V)"
    SetRegView lastused
    
    SetRegView 32
    WriteRegStr   HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}" "Beyond Compare 4 Shell Extension"
    WriteRegStr   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}" "" "CirrusShellEx"
    WriteRegStr   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32" "" "$TEMP\${DIR_TMP}\BCShellEx.dll"
    WriteRegStr   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32" "ThreadingModel" "Apartment"    
    SetRegView lastused
    
    # Refresh icon
    System::Call 'shell32::SHChangeNotify(i 0x08000000, i 0, i 0, i 0)'
    
# --- RunExe ---

    ExecWait "$TEMP\${DIR_TMP}\${APPNAME}.exe"

# --- Uninst ---    
    
    # Un Reg DLL
    ${DisableX64FSRedirection}
    ExecWait `regsvr32 /s /u "$TEMP\${DIR_TMP}\BCShellEx64.dll"`
    ${EnableX64FSRedirection}
    
    ExecWait `regsvr32 /s /u "$TEMP\${DIR_TMP}\BCShellEx.dll"`
    
    # Refresh icon
    System::Call 'shell32::SHChangeNotify(i 0x08000000, i 0, i 0, i 0)'
    
    # Delete Files
    SetShellVarContext current
    IfFileExists "$APPDATA\${APP_PUB}" 0 +2
    RMDir /r "$APPDATA\${APP_PUB}"
    
    SetOutPath "$TEMP"
    RMDir /r /REBOOTOK "$TEMP\${DIR_TMP}"
    
    # Delete Reg
    SetRegView 32    
    DeleteRegValue HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"    
    DeleteRegKey   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}"    
    SetRegView lastused
    
    SetRegView 64    
    DeleteRegValue HKLM "SOFTWARE\Classes\*\shellex\ContextMenuHandlers\CirrusShellEx" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    DeleteRegKey   HKLM "SOFTWARE\Classes\.bcss"
    DeleteRegKey   HKLM "SOFTWARE\Classes\.bcpkg"
        
    DeleteRegKey   HKLM "SOFTWARE\Classes\BeyondCompare.SettingsPackage"
    DeleteRegKey   HKLM "SOFTWARE\Classes\BeyondCompare.Snapshot"
    
    DeleteRegValue HKLM "SOFTWARE\Classes\Directory\shellex\ContextMenuHandlers\CirrusShellEx" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    DeleteRegValue HKLM "SOFTWARE\Classes\Folder\shellex\ContextMenuHandlers\CirrusShellEx" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    DeleteRegValue HKLM "SOFTWARE\Classes\lnkfile\shellex\ContextMenuHandlers\CirrusShellEx" "{57FA2D12-D22D-490A-805A-5CB48E84F12A}"
    
    DeleteRegKey   HKLM "SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}"    
    DeleteRegKey   HKLM "SYSTEM\CurrentControlSet\Services\EventLog\Application\Beyond Compare 4"    
    DeleteRegKey   HKCU "SOFTWARE\Scooter Software"    
    SetRegView lastused
    
    Abort
FunctionEnd

Section -

SectionEnd

感觉很棒!可以赞赏支持我哟~

赞(4) 打赏
作者保留所有权,侵删请联系,转载请注明: » Nanlon » NSIS 制作软件单文件版示例

评论7

  1. #2

    非常感谢您的指点,打包顺利通过并能运行了。开心的一天。

    NsisFan1周前 (05-23)回复
  2. #1

    编译可通过,运行无反应,据说是少了将原exe打包进去的部分,请您多多指点,我只会照葫芦画瓢,仅爱好而以,想弄懂这个问题。

    DictSun2周前 (05-20)回复
    • 那就是你的安装文件的问题,你需要自行准备完整的安装文件,且路径须与脚本中的路径一致!

      Nanlon2周前 (05-20)回复
      • 按您的指点做了。用nsis中文论坛的〔源码〕打包后的文件为13,906KB:用您主页的〔源码〕打包后的为18,321KB。Windows10 64位家庭版环境下运行没反应 无界面。任务管理器里也看不到。

        DictSun2周前 (05-20)回复
    • 我仅发了安装脚本,是不包含安装软件文件的,仅是个示例,打包的软件需要你自行准备

      Nanlon2周前 (05-20)回复
      • 我已将BCompare-zh-4.3.3.24545软件安装了且运行正常。打包软件为nsis3.05。还需要自行准备其他的吗?

        DictSun2周前 (05-20)回复
        • 你肯定没有把安装文件打包进去,正常的话打包的这个安装包大概二十几MB,你还是好好看看NSIS用户手册,了解一下脚本中的各个指令了再说吧!

          Nanlon2周前 (05-21)回复