需要用到SimpleSC::InstallService这个功能。
Section 安装服务 SimpleSC::ExistsService "服务名" Pop $0 ${If} $0 == 0 Push $0 SimpleSC::ServiceIsRunning "服务名" Pop $0 Pop $1 ${If} $0 == 0 Push $0 ${If} $1 == 1 SimpleSC::StopService "服务名" 1 30 Pop $0 ${If} $0 == 0 DetailPrint "删除 服务名..." SimpleSC::RemoveService "服务名" ${ElseIf} $0 != 0 Push $0 SimpleSC::GetErrorMessage Pop $0 MessageBox MB_OK|MB_ICONSTOP "服务关闭失败($0)" ${EndIf} ${ElseIf} $1 == 0 DetailPrint "Removing 服务名..." SimpleSC::RemoveService "服务名" ${EndIf} ${ElseIf} $0 != 0 Push $0 SimpleSC::GetErrorMessage Pop $0 MessageBox MB_OK|MB_ICONSTOP "检测服务是否正在运行失败($0)" ${EndIf} ${EndIf} DetailPrint "Installing 服务名Client service..." SimpleSC::InstallService "服务名" "服务名" "16" "2" "$INSTDIR\服务.exe" "Dhcp" "" "" SimpleSC::SetServiceDescription "服务名" "服务说明。" Pop $0 ${If} $0 != 0 Push $0 SimpleSC::GetErrorMessage Pop $0 MessageBox MB_OK|MB_ICONSTOP "服务注册失败($0),请手动注册" ${EndIf} SectionEnd Section 卸载服务 SimpleSC::ExistsService "服务名" Pop $0 ${If} $0 == 0 Push $0 SimpleSC::ServiceIsRunning "服务名" Pop $0 Pop $1 ${If} $0 == 0 Push $0 ${If} $1 == 1 SimpleSC::StopService "服务名" 1 30 ${If} $0 == 0 DetailPrint "Removing 服务名..." SimpleSC::RemoveService "服务名" ${ElseIf} $0 != 0 Push $0 SimpleSC::GetErrorMessage Pop $0 MessageBox MB_OK|MB_ICONSTOP "服务关闭失败($0)" ${EndIf} ${ElseIf} $1 == 0 DetailPrint "Removing 服务名..." SimpleSC::RemoveService "服务名" ${EndIf} ${ElseIf} $0 != 0 Push $0 SimpleSC::GetErrorMessage Pop $0 MessageBox MB_OK|MB_ICONSTOP "检测服务是否正在运行失败($0)" ${EndIf} ${EndIf} SectionEnd