NSIS 添加Windows服务实战

最近做了一个项目需要将Mysql数据库打包到程序中,安装完成后自动启动Mysql服务。我一直用NSIS进行软件打包,之前也打包Windows 服务之类

最近做了一个项目需要将Mysql数据库打包到程序中,安装完成后自动启动Mysql服务。
我一直用NSIS进行软件打包,之前也打包Windows 服务之类的程序。这次趁这次机会把代码记录下来。
 

Section -MySql  
  SimpleSC::ExistsService "Mysql"
  Pop $0
  ${If} $0 != 0
    Push $0
    nsExec::Exec '$INSTDIR\bin\mysqld.exe install Mysql --defaults-file="$INSTDIR\my.ini"'
    Pop $0
    ${If} $0 == 0
      nsExec::Exec 'NET START Mysql'
      nsExec::Exec 'SC CONFIG Mysql START= AUTO DISPLAYNAME= "MYSQL"'
      nsExec::Exec 'SC DESCRIPTION Mysql "数据库,如果停用将无法正常使用。"'
	${ElseIf} $0 != 0
	  MessageBox MB_OK|MB_ICONSTOP "安装MySql服务失败($0)"
    ${EndIf}	
  ${EndIf}
SectionEnd

这样写的话我们必须要事先知道他要安装的目录,要不然Mysql的配置文件my.ini的一些配置出错,导致无法进行 install 操作。如:basedir=D:\XXX;datadir=D:\XXX等配置。都需要知道所要安装的目录。

所以我们需要在安装过程中对配置文件进行修改。如下:

Section -SetINI
  Push [INSTDIR]
  Push $INSTDIR
  Push all
  Push all
  Push $INSTDIR\my.ini
  Call AdvReplaceInFile
SectionEnd

AdvReplaceInFile :

Function AdvReplaceInFile
Exch $0 ;file to replace in
Exch
Exch $1 ;number to replace after
Exch
Exch 2
Exch $2 ;replace and onwards
Exch 2
Exch 3
Exch $3 ;replace with
Exch 3
Exch 4
Exch $4 ;to replace
Exch 4
Push $5 ;minus count
Push $6 ;universal
Push $7 ;end string
Push $8 ;left string
Push $9 ;right string
Push $R0 ;file1
Push $R1 ;file2
Push $R2 ;read
Push $R3 ;universal
Push $R4 ;count (onwards)
Push $R5 ;count (after)
Push $R6 ;temp file name
 
  GetTempFileName $R6
  FileOpen $R1 $0 r ;file to search in
  FileOpen $R0 $R6 w ;temp file
   StrLen $R3 $4
   StrCpy $R4 -1
   StrCpy $R5 -1
 
loop_read:
 ClearErrors
 FileRead $R1 $R2 ;read line
 IfErrors exit
 
   StrCpy $5 0
   StrCpy $7 $R2
 
loop_filter:
   IntOp $5 $5 - 1
   StrCpy $6 $7 $R3 $5 ;search
   StrCmp $6 "" file_write1
   StrCmp $6 $4 0 loop_filter
 
StrCpy $8 $7 $5 ;left part
IntOp $6 $5 + $R3
IntCmp $6 0 is0 not0
is0:
StrCpy $9 ""
Goto done
not0:
StrCpy $9 $7 "" $6 ;right part
done:
StrCpy $7 $8$3$9 ;re-join
 
IntOp $R4 $R4 + 1
StrCmp $2 all loop_filter
StrCmp $R4 $2 0 file_write2
IntOp $R4 $R4 - 1
 
IntOp $R5 $R5 + 1
StrCmp $1 all loop_filter
StrCmp $R5 $1 0 file_write1
IntOp $R5 $R5 - 1
Goto file_write2
 
file_write1:
 FileWrite $R0 $7 ;write modified line
Goto loop_read
 
file_write2:
 FileWrite $R0 $R2 ;write unmodified line
Goto loop_read
 
exit:
  FileClose $R0
  FileClose $R1
 
   SetDetailsPrint none
  Delete $0
  Rename $R6 $0
  Delete $R6
   SetDetailsPrint lastused
 
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $9
Pop $8
Pop $7
Pop $6
Pop $5
;These values are stored in the stack in the reverse order they were pushed
Pop $0
Pop $1
Pop $2
Pop $3
Pop $4
FunctionEnd
(152)
打赏 支付红包 支付红包 微信打赏 微信打赏
上一篇 2023-05-26 10:14:46
下一篇 2023-07-19 17:19:26

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信