NSIS获取本机IP地址写入配置文件

安装一个应用程序的时候需要获取当前的网卡IP地址,即服务器IP地址,同时写入到配置文件。NSIS提供了获取IP地址的插件,IP-plugin,详情请看:http://nsis.sourceforge.net/IP_plug-in

插件安装很简单,只需要下载dll文件,拷贝到NSIS安装目录的plugin目录下即可。如果还有其它文件如nsi文件,另见插件安装说明。

Nsis代码 复制代码
  1. outfile IpTest.exe   
  2. name IpTest   
  3.     
  4. ; get_ip example   
  5. ; by Hendri Adriaens   
  6. ; HendriAdriaens@hotmail.com   
  7. ;   
  8. ; Usage:   
  9. ; ip::get_ip   
  10. ; Output: 'ip1;ip2;ip3;ip4;'  
  11. ; (semi-colon delimited IP's list, on stack)   
  12. ;   
  13. ; Uses NSIS script to retrieve   
  14. ; separate IP-addresses and to   
  15. ; test whether or not it is an   
  16. ; internet IP-address. Based on   
  17. ; information by Joost Verburg:   
  18. ;   These ranges are for local networks:    
  19. ;   10.x.x.x / 255.0.0.0    
  20. ;   172.16.x.x / 255.255.0.0 to 172.31.x.x / 255.255.0.0    
  21. ;   192.168.x.x / 255.255.255.0    
  22. ;   169.254.x.x / 255.255.0.0  
  23. ; and:   
  24. ;   169.254.x.x is an Automatic Private IP Address,   
  25. ;   which you get when there is no DHCP server available,   
  26. ;   for example, Windows gives these addresses when you check   
  27. ;   "Obtain an IP address automatically" and you have no DHCP server.   
  28. ;   
  29. ; Further information has also been found at:   
  30. ; http://home.t-online.de/home/TschiTschi/ip_adressierung.htm   
  31. ;   
  32. ; Script supplies two funcions:   
  33. ; GetNextIp : get any IP (network and internet)   
  34. ; CheckIP   : determine IP type (see function header for available types)   
  35. ; Script uses the VersionCheck   
  36. ; function to test IP's.   
  37.     
  38.     
  39. Section   
  40.   ; Code to use actual ExtensionDLL   
  41.   ; Current script provides an example   
  42.     
  43.   ip::get_ip   
  44.   Pop $0  
  45.     
  46.   ; test entry   
  47.   ;StrCpy $0 '192.168.0.100;127.0.0.1;152.168.0.101;169.254.0.1;'  
  48.     
  49.   Loop:   
  50.   Push $0  
  51.   Call GetNextIp   
  52.   Call CheckIp   
  53.   Pop $2 ; Type of current IP-address   
  54.   Pop $1 ; Current IP-address   
  55.   Pop $0 ; Remaining addresses   
  56.   StrCmp $2 '1' '' NoLoopBackIp   
  57.     MessageBox MB_OK "LoopBack IP-address: $1"  
  58.     Goto Continue   
  59.   NoLoopBackIp:   
  60.   StrCmp $2 '2' '' NoAPA   
  61.     MessageBox MB_OK "Automatic Private IP-address: $1"  
  62.     Goto Continue   
  63.   NoAPA:   
  64.   StrCmp $2 '3' '' NoLanIp   
  65.     MessageBox MB_OK "Network IP-address: $1"  
  66.     Goto Continue   
  67.   NoLanIp:   
  68.   MessageBox MB_OK "Internet IP-address: $1"  
  69.   Continue:   
  70.   StrCmp $0 '' ExitLoop Loop   
  71.   ExitLoop:   
  72. SectionEnd   
  73.     
  74. ; Function GetNextIp   
  75. ; input: head of stack   
  76. ; format: 'ip1;ip2;ip3;ip4;'  
  77. ; output: 'ip1' head of stack   
  78. ;         'ip2;ip3;ip4;' second entry of stack   
  79.     
  80. Function GetNextIp   
  81.   Exch $0  
  82.   Push $1  
  83.   Push $2  
  84.   Strcpy $2 0             ; Counter   
  85.   Loop:   
  86.     IntOp $2 $2 + 1  
  87.     StrCpy $1 $0 1 $2  
  88.     StrCmp $1 '' ExitLoop   
  89.     StrCmp $1 ';' '' Loop   
  90.     StrCpy $1 $0 $2       ; IP-address   
  91.     IntOp $2 $2 + 1  
  92.     StrCpy $0 $0 '' $2    ; Remaining string   
  93.   ExitLoop:   
  94.   Pop $2  
  95.   Push $0  
  96.   Exch 2  
  97.   Pop $0  
  98.   Exch $1  
  99. FunctionEnd   
  100.     
  101. ; Function CheckIP   
  102. ; input: IP-address on stack   
  103. ; output: additional entry on stack   
  104. ;         1 - LoopBack IP (localhost, indicates no connection to a LAN or to the internet).   
  105. ;         2 - Automatic Private IP Address (no DHCP server).   
  106. ;         3 - Network IP.   
  107. ;         4 - Internet IP.   
  108. ; Eg:   
  109. ; Push '192.168.0.100'  
  110. ; Call CheckIP   
  111. ; Pop $0 ; Contains '3'  
  112. ; Pop $1 ; Contains '192.168.0.100'  
  113.     
  114. Function CheckIP   
  115.   Exch $0  
  116.   Push $1  
  117.     
  118.   ; Check 127.x.x.x   
  119.   Push '127.0.0.0'  
  120.   Push $0  
  121.   Call VersionCheck   
  122.   Pop $1  
  123.   StrCmp $1 2 '' Range1     ; IP cannot be in range of LoopBack addresses   
  124.   Push '127.255.255.255'  
  125.   Push $0  
  126.   Call VersionCheck   
  127.   Pop $1  
  128.   StrCmp $1 1 LoopBack      ; We found a LoopBack IP   
  129.     
  130.   ; Check 10.x.x.x   
  131.   Range1:   
  132.   Push '10.0.0.0'  
  133.   Push $0  
  134.   Call VersionCheck   
  135.   Pop $1  
  136.   StrCmp $1 2 '' Range2     ; IP cannot be in range 1  
  137.   Push '10.255.255.255'  
  138.   Push $0  
  139.   Call VersionCheck   
  140.   Pop $1  
  141.   StrCmp $1 1 LanIp         ; We found a LanIp   
  142.     
  143.   ; Check 172.16.x.x to 172.31.x.x   
  144.   Range2:   
  145.   Push '172.16.0.0'  
  146.   Push $0  
  147.   Call VersionCheck   
  148.   Pop $1  
  149.   StrCmp $1 2 '' Range3     ; IP cannot be in range 2  
  150.   Push '172.31.255.255'  
  151.   Push $0  
  152.   Call VersionCheck   
  153.   Pop $1  
  154.   StrCmp $1 1 LanIp         ; We found a LanIp   
  155.     
  156.   ; Check 192.168.x.x   
  157.   Range3:   
  158.   Push '192.168.0.0'  
  159.   Push $0  
  160.   Call VersionCheck   
  161.   Pop $1  
  162.   StrCmp $1 2 '' Range4     ; IP cannot be in range 3  
  163.   Push '192.168.255.255'  
  164.   Push $0  
  165.   Call VersionCheck   
  166.   Pop $1  
  167.   StrCmp $1 1 LanIp         ; We found a LanIp   
  168.     
  169.   ; Check 169.254.x.x   
  170.   Range4:   
  171.   Push '169.254.0.0'  
  172.   Push $0  
  173.   Call VersionCheck   
  174.   Pop $1  
  175.   StrCmp $1 2 '' InternetIp ; It should be an internet IP   
  176.   Push '169.254.255.255'  
  177.   Push $0  
  178.   Call VersionCheck   
  179.   Pop $1  
  180.   StrCmp $1 1 APA           ; We found an Automatic Private IP Address   
  181.     
  182.   Goto InternetIp           ; Remaining addresses are internet IPs   
  183.     
  184.   LoopBack:   
  185.   StrCpy $1 1  
  186.   Goto Exit   
  187.     
  188.   APA:   
  189.   StrCpy $1 2  
  190.   Goto Exit   
  191.     
  192.   LanIp:   
  193.   StrCpy $1 3  
  194.   Goto Exit   
  195.     
  196.   InternetIp:   
  197.   StrCpy $1 4  
  198.     
  199.   Exit:   
  200.   Exch $1  
  201.   Exch 1  
  202.   Exch $0  
  203.   Exch 1  
  204. FunctionEnd   
  205.     
  206. ; Function VersionCheck   
  207. ; input: 'v1''v2' on stack   
  208. ; output 1 - if number 1 is newer   
  209. ;        2 - if number 2 is newer   
  210. ;        0 - if it is the same verion   
  211. ; Eg:   
  212. ; Push '3.5.1.4'  
  213. ; Push '3.5'  
  214. ; Call VersionCheck   
  215. ; Pop $0 ; now contains 1  
  216.     
  217. Function VersionCheck   
  218.   Exch $0 ;second versionnumber   
  219.   Exch   
  220.   Exch $1 ;first versionnumber   
  221.   Push $R0 ;counter for $0  
  222.   Push $R1 ;counter for $1  
  223.   Push $3 ;temp char   
  224.   Push $4 ;temp string for $0  
  225.   Push $5 ;temp string for $1  
  226.   StrCpy $R0 "-1"  
  227.   StrCpy $R1 "-1"  
  228.   Start:   
  229.   StrCpy $4 ""  
  230.   DotLoop0:   
  231.   IntOp $R0 $R0 + 1  
  232.   StrCpy $3 $0 1 $R0   
  233.   StrCmp $3 "" DotFound0   
  234.   StrCmp $3 "." DotFound0   
  235.   StrCpy $4 $4$3  
  236.   Goto DotLoop0   
  237.   DotFound0:   
  238.   StrCpy $5 ""  
  239.   DotLoop1:   
  240.   IntOp $R1 $R1 + 1  
  241.   StrCpy $3 $1 1 $R1   
  242.   StrCmp $3 "" DotFound1   
  243.   StrCmp $3 "." DotFound1   
  244.   StrCpy $5 $5$3  
  245.   Goto DotLoop1   
  246.   DotFound1:   
  247.   Strcmp $4 "" 0 Not4   
  248.     StrCmp $5 "" Equal   
  249.     Goto Ver2Less   
  250.   Not4:   
  251.   StrCmp $5 "" Ver2More   
  252.   IntCmp $4 $5 Start Ver2Less Ver2More   
  253.   Equal:   
  254.   StrCpy $0 "0"  
  255.   Goto Finish   
  256.   Ver2Less:   
  257.   StrCpy $0 "1"  
  258.   Goto Finish   
  259.   Ver2More:   
  260.   StrCpy $0 "2"  
  261.   Finish:   
  262.   Pop $5  
  263.   Pop $4  
  264.   Pop $3  
  265.   Pop $R1   
  266.   Pop $R0   
  267.   Pop $1  
  268.   Exch $0  
  269. FunctionEnd  

 

运行上面这个例子你就可以看到结果了。

IP地址获取之后,写入配置文件就简单了。如果是ini文件,可以直接用WriteINIStr方法

指令 <noscript src="/admin/blogs/339618/chmlink.js" type="text/javascript"></noscript>

WriteINIStr $TEMP/something.ini section1 something 123
WriteINIStr $TEMP/something.ini section1 somethingelse 1234
WriteINIStr $TEMP/something.ini section2 nsis true
NSIS(Nullsoft Scriptable Install System)是一个开源的脚本安装系统,可以用于创建Windows平台上的安装程序。要检测本机的.NET Framework版本并重启,可以使用以下步骤: 1. 创建一个NSIS脚本,可以使用任何文本编辑器来编辑它。在脚本中添加以下代码段: ``` ReadRegStr $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version" ${If} $0 == "" MessageBox MB_OK "未检测到.NET Framework" Quit ${EndIf} ${If} $0 < "4.5" MessageBox MB_OK "需要安装.NET Framework 4.5或更高版本" Quit ${EndIf} MessageBox MB_OK "已检测到.NET Framework版本: $0" ; 在这里添加重启代码,例如: MessageBox MB_OKCANCEL "完成安装,是否重启计算机?" ${If} $?.eq.IDOK Reboot ${EndIf} ``` 2. 上述代码通过读取注册表中的.NET Framework版本信息来检测本机上是否安装了.NET Framework。如果未检测到.NET Framework,则显示一个消息框并退出安装程序。如果检测到的版本低于4.5,则显示另一个消息框提醒用户需要安装较高的版本。 3. 修改重启代码以符合实际需求。上述代码中使用了一个消息框来询问用户是否重启计算机。根据需要,可以自定义重启逻辑,例如直接执行重启命令或显示一个带有确认按钮的自定义消息框。 4. 将NSIS脚本保存为一个以`.nsi`为扩展名的文件。然后可以使用NSIS编译器将脚本编译为可执行的安装程序。 注意:上述代码仅适用于检测.NET Framework的版本是否达到最低要求,并在需要时重启计算机。对于其他.NET Framework相关的操作或更复杂的检测逻辑,可能需要编写更多的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值