最新消息:

常用代码整理

nsis 永恒心锁 1814浏览 0评论
;创建桌面快捷方式
CreateShortCut "$DESKTOP${PRODUCT_INSTNAME}.lnk" "$INSTDIR${PRODUCT_EXENAME}"

;创建快速启动栏
SetOutPath "$INSTDIR"
CreateShortCut "$QUICKLAUNCH${PRODUCT_NAME}.lnk" "$INSTDIR${PRODUCT_EXENAME}"
${If} ${AtLeastWin7}
 ExecShell taskbarpin "$QUICKLAUNCH${PRODUCT_NAME}.lnk"
 Delete "$QUICKLAUNCH${PRODUCT_NAME}.lnk"
${EndIf}

;卸载快速启动栏
Delete "$QUICKLAUNCH${PRODUCT_NAME}.lnk"
${If} ${AtLeastWin7}
IfFileExists "$QUICKLAUNCHUser PinnedTaskBar${PRODUCT_NAME}.lnk" 0 +2
 Messagebox MB_OKCANCEL "是否删除快速启动栏" IDOK 0 IDCANCEL +2
 ExecShell taskbarunpin "$QUICKLAUNCHUser PinnedTaskBar${PRODUCT_NAME}.lnk"
${EndIf}

;创建菜单栏快捷方式
SetOutPath "$SMPROGRAMS"
CreateDirectory "$SMPROGRAMS${PRODUCT_INSTNAME}"
CreateShortCut "$SMPROGRAMS${PRODUCT_INSTNAME}${PRODUCT_INSTNAME}.lnk" "$INSTDIR${PRODUCT_EXENAME}" # 主程序快捷方式
#WriteIniStr "$INSTDIR${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}" # 官方网址快捷方式
#CreateShortCut "$SMPROGRAMS${PRODUCT_INSTNAME}Website.lnk" "$INSTDIR${PRODUCT_NAME}.url" # 将官方网址快捷方式放到菜单栏
CreateShortCut "$SMPROGRAMS${PRODUCT_INSTNAME}Uninstall.lnk" "$INSTDIRuninst.exe" # 卸载快捷方式

;卸载菜单栏快捷方式
; 卸载开始菜单目录
RMDir /R "$SMPROGRAMS${PRODUCT_INSTNAME}"
; Win7 下会在所有用户(all)下进行创建
SetShellVarContext all
RMDir /R "$SMPROGRAMS${PRODUCT_INSTNAME}"
SetShellVarContext current

;隐藏所有控件
${For} $R1 -1 2000
 ${If} $R1 == 0
 ${OrIf} $R1 == 1018
 ${Continue}
 ${Else}
 GetDlgItem $0 $HWNDPARENT $R1
 ShowWindow $0 ${SW_HIDE}
 ${EndIf}
${Next}

;销毁所有控件
${For} $R1 -1 2000
 ${If} $R1 == 0
 ${OrIf} $R1 == 1018
 ${Continue}
 ${Else}
 GetDlgItem $0 $HWNDPARENT $R1
 System::Call 'user32::DestroyWindow(i$0)'
 ${EndIf}
${Next}

;开机自动启动
section /o "开机自动启动"
writeregstr HKLM "softwaremicrosoftwindowscurrentversionrun" "${PRODUCT_NAME}" "$INSTDIR${EXE_NAME}"
sectionend

;获取操作系统盘符
ReadEnvStr $R0 SYSTEMDRIVE

;创建log文件夹
CreateDirectory "$R0log"

;递归删除log文件夹
RMDir /r "$R0log"

;弹出Windows关机框
FindWindow $0 "shell_traywnd"
SendMessage $0 273 506 0

;请求Windows帮助
FindWindow $0 "shell_traywnd"
SendMessage $0 273 503 0

;一句话刷新屏幕
System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'

;打开桌面属性
;...cpl,0,3'中的3可以改为
;-1 主题 0 壁纸 1 屏保 2 外观 3 设置
Exec 'Rundll32.exe shell32.dll,Control_RunDLL Desk.cpl,0,3' ;打开"设置"

;获得安装程序所在目录
System::Call 'kernel32::GetCurrentDirectory(i 1024,t .R1)'
MessageBox MB_OK '安装程序所在目录:$R1'

;或者直接使用变量
MessageBox MB_OK '文件名$EXEFILE,所在文件夹$EXEDIR,完整路径$EXEPATH。'

;获得当前安装程序进程PID
System::Call 'kernel32::GetCurrentProcessId()i .R0'
MessageBox MB_OK '安装程序进程PID:$R0'

;获得磁盘卷标
StrCpy $R0 "C:"
System::Call 'kernel32::GetVolumeInformationA(t R0,t .R1,i 100,i .R3,i .R4,i .R5,t .R6,i 100)'
MessageBox MB_OK '盘符:$R0$n卷标:$R1$n文件格式:$R6'

;设置磁盘卷标
StrCpy $R0 "C:" ;盘符
StrCpy $R1 "系统" ;卷标
System::Call 'kernel32::SetVolumeLabelA(t R0,t R1)'

;设置文件属性
StrCpy $R0 "C:1.EXE" ;文件
StrCpy $R1 0x20 ;属性
System::Call 'kernel32::SetFileAttributesA(t R0,i 0x100)'
;属性表
;0x20 FILE_ATTRIBUTE_ARCHIVE,存档
;0x2 FILE_ATTRIBUTE_HIDDEN,隐藏
;0x80 FILE_ATTRIBUTE_NORMAL,正常
;0x2000 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,不索引
;0x1000 FILE_ATTRIBUTE_OFFLINE,离线
;0x1 FILE_ATTRIBUTE_READONLY,只读
;0x4 FILE_ATTRIBUTE_SYSTEM,系统
;0x100 FILE_ATTRIBUTE_TEMPORARY,;临时属性(一般无效)

;在WIN7任务栏中增加图标链接
SetOutPath $WINDIR
CreateShortcut "$DESKTOP记事本.lnk" "$WINDIRnotepad.exe"
ExecShell taskbarpin "$DESKTOP记事本.lnk"

;指定在 WIN7 下安装程序需要的执行级别。
RequestExecutionLevel admin

;安装结束后调用HTTP下载插件下载文件并运行
Function .onGUIEnd
NSISdl::download "http://www.pythonschool.com/服务器文件名.exe" "本地文件名.exe"
Pop $R0 ;获取返回值
 StrCmp $R0 "success" +3
 MessageBox MB_OK "下载失败,原因: $R0"
 Quit
exec ""本地文件名.exe""
FunctionEnd

;隐藏/显示文件扩展名

;隐藏/显示文件扩展名实时生效,写入 0 (0x00000000) 显示或 1 (0x00000001) 来隐藏
WriteRegDWORD HKCU "SoftwareMicrosoftWindowsCurrentVersionExplorerAdvanced" "HideFileExt" 0x00000001
SendMessage ${HWND_BROADCAST} ${WM_SETTINGCHANGE} 0 "STR:ShellState"
System::Call "shell32::SHChangeNotify(i0x08000000,i0,i0,i0)"

;获取当前屏幕分辨率
Function ".onInit"
System::Call 'user32::GetSystemMetrics(i 0) i .r0'
System::Call 'user32::GetSystemMetrics(i 1) i .r1'
MessageBox MB_OK|MB_ICONINFORMATION "当前屏幕分辨率为: $r$n$0 X $1"
Quit
FunctionEnd

;设置主页
WriteRegStr HKCU "SoftwareMicrosoftInternet ExplorerMain" "Start Page" "http://www.yhxs3344.net"

;调用 Windows API 来获取运行时间
System::Alloc 16
System::Call kernel32::GetLocalTime(isR0)
System::Call *$R0(&i2.R1,&i2.R2,&i2.R3,&i2.R4,&i2.R5,&i2.R6,&i2.R7,&i2.R8)
System::Free $R0
MessageBox MB_OK "$R1年$R2月$R4日,星期$R3,$R5:$R6:$R7.$R8"

;使用MiscButtonText修改按钮文本
;分别对应[上一步]、[下一步]、[取消]、[关闭],如果留空参数,则使用默认值
MiscButtonText "" "" "" "完成(&G)"

;安装过程页面
!insertmacro MUI_PAGE_INSTFILES

;清除目录权限及设置目录权限
AccessControl::DisableFileInheritance "$INSTDIR" ;禁止继承
AccessControl::ClearOnFile "$INSTDIR" "(S-1-1-0)" "FullAccess" ;设置Everyone权限

;获得CPU和BIOS信息
Name "获得CPU和BIOS信息"
OutFile "flighty.exe"
Section
SectionEnd
Function .onInit
 ReadRegStr $0 HKLM "HARDWAREDESCRIPTIONSystemCentralProcessor" "VendorIdentifier"
 ReadRegStr $1 HKLM "HARDWAREDESCRIPTIONSystemCentralProcessor" "ProcessorNameString"
 ReadRegStr $2 HKLM "HARDWAREDESCRIPTIONSystemCentralProcessor" "Identifier"
 ReadRegStr $3 HKLM "HARDWAREDESCRIPTIONSystemCentralProcessor" "~MHz"
 ReadRegStr $4 HKLM "HARDWAREDESCRIPTIONSystemBIOS" "BaseBoardManufacturer"
 ReadRegStr $5 HKLM "HARDWAREDESCRIPTIONSystemBIOS" "BIOSVersion"
 ReadRegStr $6 HKLM "HARDWAREDESCRIPTIONSystemBIOS" "BIOSReleaseDate"
 MessageBox MB_OK|MB_ICONEXCLAMATION "CPU制造商$0 $r$nCPU型号$1 $r$nCPU标识$2 $r$nCPU主频$3 $r$n$r$n主板品牌$4 $r$n主板BIOS版本$5 $r$n主板BIOS日期$6"
Abort
FunctionEnd

;判断操作系统位宽
Name "判断操作系统位宽"
OutFile "判断操作系统位宽.exe"
!include "x64.nsh"
Section
SectionEnd
Function .onInit
${If} ${RunningX64}
 MessageBox MB_OK "64"
${Else}
 MessageBox MB_OK "32"
${EndIf}
FunctionEnd

;在 hosts 文件中追加地址屏蔽脚本
ClearErrors
SetFileAttributes "$SYSDIRdriversetchosts" NORMAL ;去只读属性
FileOpen $9 $SYSDIRdriversetchosts a ;追加数据
;FileSeek $9 0 END ;指针定位到相对于文件结尾
FileWrite $9 "127.0.0.1 adcount.sandai.net $r$n"
FileWrite $9 "127.0.0.1 biz4.sandai.net $r$n"
;下面自己加
SetFileAttributes "$SYSDIRdriversetchosts" READONLY ;加只读属性
FileClose $9 ;关闭打开的文件

;对比两个文件的时间
;对比两个文件的时间,如两个时间相等,就返回0;如R0小于R1,返回-1;如R1小于R0,返回1
StrCpy $R0 "C:boot.ini"
StrCpy $R1 "C:AUTOEXEC.BAT"
System::Call 'kernel32::CompareFileTime(t R0,t R1) i.R2'
MessageBox MB_OK '文件$R0与$R1时间比较结果$R2'

;注册apache服务器
;注册apache服务器, -n MyWebServer表示apache2注册服务名为MyWebServer,默认服务名为Apache2
nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/apache2/bin/httpd.exe" -k install -n MyWebServer'
;注册MySQL服务器, MyDBServer表示MySQL注册服务名为MyDBServer,默认服务名为MySQL
nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/mysql5/bin/mysqld-nt.exe" -install MyDBServer'
;启动apache服务和MySQL服务
nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/apache2/bin/httpd.exe" -k start -n MyWebServer'
;或者
nsExec::ExecToLog 'cmd.exe /c net start MyWebServer'
nsExec::ExecToLog 'cmd.exe /c net start MyDBServer'

;从注册表中读取操作系统语言
ReadRegStr $0 HKLM "SYSTEMCurrentControlSetControlNlsLanguage" "Default"
${If} $0 = 0804
MessageBox MB_OK "您的操作系统语言为简体中文。"
${ElseIf} $0 == 0404
MessageBox MB_OK "您的操作系统语言为繁体中文。"
${ElseIf} $0 == 0409
MessageBox MB_OK "您的操作系统语言为英文。"
${EndIf}
永恒心锁,版权所有丨如未注明,均为转载丨本网站采用BY-NC-SA协议进行授权

转载请注明:永恒心锁-分享互联网 » 常用代码整理

您必须 登录 才能发表评论!