例子:在网上下载配置文件并运行相应程序

优质
小牛编辑
134浏览
2023-12-01

注:编译脚本需要在本地搭建服务器环境,你也可以放到自己的网站上,然后更改下载服务器的地址(脚本第30行)即可。

引用 RandomRead.nsi 的内容:


OutFile "RandomPage.EXE"
Name "RandomPage"
!include "MUI2.nsh"
SetFont "tahoma" 8
Var NoNetwork
Var ID1
Var ID2
Var ID3
Var ID4
Var Dialog
Var Checkbox1
Var Checkbox2
Var Checkbox3
Var Checkbox4

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_COMPONENTS
Page custom RandomPageShow RandomPageLeave
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE "SimpChinese"

Section
SectionEnd

Function .onInit

;下载服务器的INI,否则记住,不显示此页。
InitPluginsDir
inetc::get /SILENT "http://127.0.0.1/RandomPage.ini" "$PLUGINSDIR\RandomPage.ini"
Pop $0
StrCmp $0 "OK" +2   ;下载ini文件不成功视为无网络
StrCpy $NoNetwork 1 ;无网络标志

;获取时间
System::Call '*(&i2,&i2,&i2,&i2,&i2,&i2,&i2,&i2) i .r0'
System::Call 'kernel32::GetLocalTime(i)i(r0)'
;时间除7求余数得ID1
IntOp $ID1 $0 % 7
;ID1加一再除7求余数
IntOp $ID2 $ID1 + 1
IntOp $ID2 $ID2 % 7
;ID2加一再除7求余数
IntOp $ID3 $ID2 + 1
IntOp $ID3 $ID3 % 7
;ID3加一再除7求余数
IntOp $ID4 $ID3 + 1
IntOp $ID4 $ID4 % 7
;得出0~6的连续不同数.
FunctionEnd

Function RandomPageShow
StrCmp $NoNetwork 1 NoNeedPage  ;如果没有网络,不需要显示.
nsDialogs::Create /NOUNLOAD 1018
Pop $Dialog

${NSD_CreateLabel} 0 0u 100% 25u "感谢安装超级兔子,欢迎安装下列为你推荐的附加组件。"

;做四个checkbox,标题和是否默认选择由INI决定
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Title" $ID1
${NSD_CreateCheckbox} 0 30u 100% 10u "$R1"
Pop $Checkbox1
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "IsCheckDefault" $ID1
${NSD_SetState} $Checkbox1 $R1

ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Title" $ID2
${NSD_CreateCheckbox} 0 45u 100% 10u "$R1"
Pop $Checkbox2
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "IsCheckDefault" $ID2
${NSD_SetState} $Checkbox2 $R1

ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Title" $ID3
${NSD_CreateCheckbox} 0 60u 100% 10u "$R1"
Pop $Checkbox3
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "IsCheckDefault" $ID3
${NSD_SetState} $Checkbox3 $R1


ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Title" $ID4
${NSD_CreateCheckbox} 0 75u 100% 10u "$R1"
Pop $Checkbox4
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "IsCheckDefault" $ID4
${NSD_SetState} $Checkbox4 $R1

nsDialogs::Show
NoNeedPage:
FunctionEnd

Function RandomPageLeave
StrCmp $NoNetwork 1 NoNeedLeavePage  ;如果没有网络,不需要显示.
Push $R0
Push $R1
Push $R2
${NSD_GetState} $Checkbox1 $R0
	StrCmp $R0 1 +1 NOCheck1    ;检查Checkbox有没选择
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "URL" $ID1  ;读URL
	StrCmp $R1 "" +2        ;没有跳过打开网址
	ExecShell open "$R1" SW_SHOWNORMAL
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Execute" $ID1  ;读需要下载执行的文件网址
	StrCmp $R1 "" NOCheck1        ;没有跳过下载打开程序
	inetc::get /SILENT "$R1" "$PLUGINSDIR\1.exe"    ;下载程序为"$PLUGINSDIR\1.exe"
	Pop $R0
	StrCmp $R0 "OK" +1 NOCheck1     ;下载成功开始运行,运行是否ExecWait由INI决定.
	ReadINIStr $R2 "$PLUGINSDIR\RandomPage.ini" "ExecWait" $ID1
	StrCmp $R2 1 +1 +3
	ExecWait "$PLUGINSDIR\1.exe"
	Goto +2
	Exec "$PLUGINSDIR\1.exe"
NOCheck1:

${NSD_GetState} $Checkbox2 $R0
	StrCmp $R0 1 +1 NOCheck2
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "URL" $ID2
	StrCmp $R1 "" +2
	ExecShell open "$R1" SW_SHOWNORMAL
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Execute" $ID2
	StrCmp $R1 "" NOCheck2
	inetc::get /SILENT "$R1" "$PLUGINSDIR\2.exe"
	Pop $R0
	StrCmp $R0 "OK" +1 NOCheck2
	ReadINIStr $R2 "$PLUGINSDIR\RandomPage.ini" "ExecWait" $ID2
	StrCmp $R2 1 +1 +3
	ExecWait "$PLUGINSDIR\2.exe"
	Goto +2
	Exec "$PLUGINSDIR\2.exe"
NOCheck2:

${NSD_GetState} $Checkbox3 $R0
	StrCmp $R0 1 +1 NOCheck3
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "URL" $ID3
	StrCmp $R1 "" +2
	ExecShell open "$R1" SW_SHOWNORMAL
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Execute" $ID3
	StrCmp $R1 "" NOCheck3
	inetc::get /SILENT "$R1" "$PLUGINSDIR\3.exe"
	Pop $R0
	StrCmp $R0 "OK" +1 NOCheck3
	ReadINIStr $R2 "$PLUGINSDIR\RandomPage.ini" "ExecWait" $ID3
	StrCmp $R2 1 +1 +3
	ExecWait "$PLUGINSDIR\3.exe"
	Goto +2
	Exec "$PLUGINSDIR\3.exe"
NOCheck3:


${NSD_GetState} $Checkbox4 $R0
	StrCmp $R0 1 +1 NOCheck4
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "URL" $ID4
	StrCmp $R1 "" +2
	ExecShell open "$R1" SW_SHOWNORMAL
ReadINIStr $R1 "$PLUGINSDIR\RandomPage.ini" "Execute" $ID4
	StrCmp $R1 "" NOCheck4
	inetc::get /SILENT "$R1" "$PLUGINSDIR\4.exe"
	Pop $R0
	StrCmp $R0 "OK" +1 NOCheck4
	ReadINIStr $R2 "$PLUGINSDIR\RandomPage.ini" "ExecWait" $ID4
	StrCmp $R2 1 +1 +3
	ExecWait "$PLUGINSDIR\4.exe"
	Goto +2
	Exec "$PLUGINSDIR\4.exe"
NOCheck4:
Pop $R2
Pop $R1
Pop $R0
NoNeedLeavePage:
FunctionEnd

引用 RandomPage.ini 的内容:

[Title]
0=0.打开雅虎
1=1.打开谷歌
2=2.打开完美者
3=3.安装Windows8
4=4.打开blog,顺便装个xxoo
5=5.打开百毒
6=6.打开病

[IsCheckDefault]
0=0
1=1
2=1
3=0
4=1
5=0
6=1

[URL]
0=http://www.yahoo.com
1=http://www.g.cn
2=http://www.wmzhe.com
3=
4=http://blog.163.com
5=http://www.baidu.com
6=http://www.bing.com

[Execute]
0=http://127.0.0.1/ccc.exe
1=
2=http://127.0.0.1/aaa.exe
3=http://127.0.0.1/bbb.exe
4=http://127.0.0.1/aaa.exe
5=
6=http://127.0.0.1/aaa.exe

[ExecWait]
0=0
1=0
2=0
3=0
4=1
4=1
5=1
6=0