au3注册服务启动后无法运行程序求解
本帖最后由 pyj521 于 2017-3-18 19:30 编辑最近维护一个网吧,系统升级到win10,在用万象收费的时候发现一个问题,进入系统后发现万象客户端启动太慢了,于是萌发一个想法,通过手动测试确实可以改变这个问题,我想通过au3编写一个服务程序,用服务启动客户端程序,但是测试了两天,找遍论坛所有有关教程都没能解决,只好在此发帖恳求元老大神们的帮助,感谢感激!我看了p大的注册自身为系统服务的脚本,没能实现我想要的功能!
我想要的功能是:系统启动时,以服务的方式运行万象收费客户端(C:\\Windows\\SysWOW64\\Clsmn.exe),程序运行后在后台监视进程Clsmn.exe,如果进程Clsmn.exe存在就什么也不做,如果进程不存在就再次运行(C:\\Windows\\SysWOW64\\Clsmn.exe),保持进程一直存在。
我知道通过别的办法是可以实现的,用系统服务的好处是权限高,万象启动快,安全性高一些,如今的熊孩子多,网络上傻瓜破解工具也多,希望元老们大神们给予帮助,感激万分,测试通过的朋友留下微信,小红包不会少!谢谢大家!拜托了!#NoTrayIcon
#include <LocalSecurityAuthority.au3>
Const $SERVICE_WIN32_OWN_PROCESS = 0x0010
Const $SERVICE_WIN32_SHARE_PROCESS = 0x0020
Const $SERVICE_INTERACTIVE_PROCESS = 0x0100
Const $SERVICE_STOPPED = 1
Const $SERVICE_RUNNING = 4
Const $SERVICE_CONTINUE_PENDING = 5
Const $SERVICE_PAUSED = 7
Const $SERVICE_ACCEPT_STOP = 1
Const $SERVICE_ACCEPT_PAUSE_CONTINUE = 2
Const $SERVICE_CONTROL_STOP = 1
Const $SERVICE_CONTROL_PAUSE = 2
Const $SERVICE_CONTROL_CONTINUE = 3
Const $SERVICE_CONTROL_INTERROGATE = 4
Const $tagSERVICE_FAILURE_ACTIONS = "dword ResetPeriod;ptr RebootMsg;ptr Command;dword Actions;ptr SCActions"
Const $tagSC_ACTION = "dword Type;dword Delay"
Const $tagSERVICE_STATUS = "dword ServiceType;dword CurrentState;dword ControlsAccepted;dword Win32ExitCode;dword ServiceSpecificExitCode;dword CheckPoint;dword WaitHint"
If Not @Compiled Then Exit(Msgbox(48, "Error", "Compile first."))
; SCM - 服务控制器 (Service Control Manager)。
; 此服务程序的名称。
Global $sServiceName = "Au3MemCleaner"
; 判断此服务程序是否由SCM启动。
Local $tProcessBasic = DllStructCreate("dword;ptr;ulong_ptr;ulong")
_NtQueryInformationProcess(-1, 0, DllStructGetPtr($tProcessBasic), 24)
If DllStructGetData($tProcessBasic, 4, 3) <> ProcessExists("Services.exe") Then
$hService = _LsaOpenService($sServiceName, 0xF01FF)
If ($hService = 0) Then $hService = _CreateService($sServiceName, $sServiceName, _
bitOr($SERVICE_WIN32_OWN_PROCESS, $SERVICE_INTERACTIVE_PROCESS), _
2, 0, @ScriptFullPath, "", 0)
$pAction = _HeapAlloc(44)
$tAction = DllStructCreate($tagSERVICE_FAILURE_ACTIONS, $pAction)
DllStructSetData($tAction, "Actions", 3)
DllStructSetData($tAction, "SCActions", $pAction + 20)
For $i = 1 To 3 ;修改服务启动失败后的三次动作
$tSCActions = DllStructCreate($tagSC_ACTION, ($pAction + 20) + ($i - 1) * 8)
DllStructSetData($tSCActions, "Type", 1)
Next
_ChangeServiceConfig2($hService, 2, $pAction) ;应用修改
_StartService($hService) ;启动服务
_LsaCloseServiceHandle($hService) ;关闭打开的服务句柄
exit
EndIf
; 定义几个全局变量,以便在多个函数中共享它们的值。
Global $hServiceMain, $hHandlerEx, $tServiceTable
Global $tServiceStatus, $pServiceStatus, $hServiceStatus
; 注册服务入口点函数,用于在SCM启动服务时,向SCM报告自己的状态。
$hServiceMain = DllCallbackRegister("_ServiceMain", "none", "dword;ptr")
; 子控制请求函数,用于接收SCM发来的请求,并报告自己的状态。
$hHandlerEx = DllCallbackRegister("_HandlerEx", "dword", "dword;dword;ptr;ptr")
$tServiceTable = DllStructCreate("ptr;ptr;ptr;ptr;char")
DllStructSetData($tServiceTable, 1, DllStructGetPtr($tServiceTable, 5)) ; 服务名称。
DllStructSetData($tServiceTable, 2, DllCallbackGetPtr($hServiceMain)) ; 入口点函数地址。
DllStructSetData($tServiceTable, 5, $sServiceName)
$tServiceStatus = DllStructCreate($tagSERVICE_STATUS)
$pServiceStatus = DllStructGetPtr($tServiceStatus)
DllStructSetData($tServiceStatus, "ServiceType", _ ; 服务类型
bitOr($SERVICE_WIN32_OWN_PROCESS, _ ; 独享进程。
$SERVICE_INTERACTIVE_PROCESS)) ; 允许与桌面交互。
DllStructSetData($tServiceStatus, "ControlsAccepted", _ ; 指定允许接收的后续控制请求。
bitOr($SERVICE_ACCEPT_STOP, _ ; 服务可以停止。
$SERVICE_ACCEPT_PAUSE_CONTINUE)) ; 服务可以暂停和继续。
; 启动服务的控制调度分派线程。
; 当SCM启动某个服务时,服务进程的主线程必须在30秒内调用此函数。
; SCM将分派表传递给StartServiceCtrlDispatcher,这将把调用进程的主线程转换为控制分派器,
; 该分派器启动一个新线程,该线程运行分派表中每个服务的 ServiceMain 入口点函数。
; 如果 StartServiceCtrlDispatcher 函数30秒没有被调用, 便会出现1053的错误(服务没有及时响应控制请求)。
_StartServiceCtrlDispatcher(DllStructGetPtr($tServiceTable))
Func _ServiceMain($iNumberofArgs, $pArguments)
Local $aProcess, $hProcess, $hToken, $aPriv = [[$SE_DEBUG_NAME, 2]]
; 注册服务的“控制处理器”,以用于接收停止、暂停等请求操作,应在ServiceMain函数中尽早调用。
$hServiceStatus = _RegisterServiceCtrlHandlerEx($sServiceName, DllCallbackGetPtr($hHandlerEx))
; 向SCM报告自己的状态。
DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_RUNNING)
_SetServiceStatus($hServiceStatus, $pServiceStatus)
$hToken = _OpenProcessToken(-1)
If Not _IsPrivilegeEnabled($hToken, $SE_DEBUG_NAME) Then
_AdjustTokenPrivileges($hToken, $aPriv)
EndIf
_LsaCloseHandle($hToken)
; 在服务的主函数中循环清理内存。
While 1
$aProcess = ProcessList()
For $i = 1 To $aProcess
$hProcess = _OpenProcess($aProcess[$i])
_EmptyWorkingSet($hProcess)
_LsaCloseHandle($hProcess)
Sleep(50)
Next
WEnd
EndFunc ;==>_ServiceMain
; 服务的“控制处理器”,用于处理SCM发出的各种请求。
Func _HandlerEx($iRequest, $iEventType, $pEventData, $pContext)
Switch $iRequest
Case $SERVICE_CONTROL_STOP ; 服务需停止。
; 如果30秒内没有向SCM报告自己的状态,将会出现1053的错误。
DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_STOPPED)
_SetServiceStatus($hServiceStatus, $pServiceStatus)
Return 0
Case $SERVICE_CONTROL_PAUSE ; 服务需暂停。
; 同上,必须在30秒调用,否则出错。
DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_PAUSED)
_SetServiceStatus($hServiceStatus, $pServiceStatus)
Return 0
Case $SERVICE_CONTROL_CONTINUE ; 服务需要继续。
; 同上。
DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_RUNNING)
_SetServiceStatus($hServiceStatus, $pServiceStatus)
Return 0
Case $SERVICE_CONTROL_INTERROGATE ; 服务需要向SCM报告自己的状态。
_SetServiceStatus($hServiceStatus, $pServiceStatus)
Return 0
EndSwitch
EndFunc ;==>_HandlerEx
Func _SetServiceStatus($hServiceStatus, $pServiceStatus)
Local $iResult
$iResult = DllCall("AdvApi32.dll", "int", "SetServiceStatus", _
"hWnd", $hServiceStatus, "ptr", $pServiceStatus)
Return SetError(_GetLastError(), 0, $iResult)
EndFunc ;==>_SetServiceStatus
Func _StartService($hService, $iNumberofArgs = 0, $pArguments = 0)
Local $iResult
$iResult = DllCall("AdvApi32.dll", "int", "StartService", "hWnd", $hService, _
"dword", $iNumberofArgs, "ptr", $pArguments)
Return SetError(_GetLastError(), 0, $iResult)
EndFunc ;==>_StartService
Func _RegisterServiceCtrlHandlerEx($sServiceName, $pHandlerEx, $pContext = 0)
Local $iResult
$iResult = DllCall("AdvApi32.dll", "hWnd", "RegisterServiceCtrlHandlerEx", _
"str", $sServiceName, "ptr", $pHandlerEx, "ptr", $pContext)
Return SetError(_GetLastError(), 0, $iResult)
EndFunc ;==>_RegisterServiceCtrlHandlerEx
Func _StartServiceCtrlDispatcher($pServiceTable)
Local $iResult
$iResult = DllCall("AdvApi32.dll", "int", "StartServiceCtrlDispatcher", _
"ptr", $pServiceTable)
Return SetError(_GetLastError(), 0, $iResult)
EndFunc ;==>_StartServiceCtrlDispatcher
Func _CreateService($sServiceName, $sDisplayName, $iServiceType, $iStartType, _
$iErrorControl, $sBinaryPath, $sLoadOrderGroup, $aDependencies, _
$sStartName = "", $sPassword = "", $iDesiredAccess = 0xF01FF)
Local $iResult, $tServiceName, $tDisplayName, $tBinaryPath, $hSC
Local $tLoadOrder, $tDependencies, $tStartName, $tPassword, $sBuffer
$tServiceName = DllStructCreate("wchar ServiceName[" & StringLen($sServiceName) + 1 & "]")
DllStructSetData($tServiceName, "ServiceName", $sServiceName)
If $sDisplayName <> "" Then
$tDisplayName = DllStructCreate("wchar DisplayName[" & StringLen($sDisplayName) + 1 & "]")
DllStructSetData($tDisplayName, "DisplayName", $sDisplayName)
EndIf
If $sBinaryPath <> "" Then
$tBinaryPath = DllStructCreate("wchar BinaryPath[" & StringLen($sBinaryPath) + 1 & "]")
DllStructSetData($tBinaryPath, "BinaryPath", $sBinaryPath)
EndIf
If $sLoadOrderGroup <> "" Then
$tLoadOrder = DllStructCreate("wchar LoadOrder[" & StringLen($sLoadOrderGroup) + 1 & "]")
DllStructSetData($tLoadOrder, "LoadOrder", $sLoadOrderGroup)
EndIf
If $sStartName <> "" Then
$tStartName = DllStructCreate("wchar StartName[" & StringLen($sStartName) + 1 & "]")
DllStructSetData($tStartName, "StartName", $sStartName)
EndIf
If $sPassword <> "" Then
$tPassword = DllStructCreate("wchar Password[" & StringLen($sPassword) + 1 & "]")
DllStructSetData($tPassword, "Password", $sPassword)
EndIf
If IsArray($aDependencies) And UBound($aDependencies, 0) = 1 Then
For $i = 0 To UBound($aDependencies) - 1
$sBuffer &= "wchar[" & StringLen($aDependencies[$i]) + 1 & "];")
Next
$tDependencies = DllStructCreate($sBuffer & ";wchar")
For $i =0 To UBound($aDependencies) - 1
DllStructSetData($tDependencies, ($i + 1), $aDependencies[$i])
Next
EndIf
$hSC = _LsaOpenSCManager("", 2)
$iResult = DllCall("AdvApi32.dll", "hWnd", "CreateServiceW", _
"hWnd", $hSC, _
"ptr", DllStructGetPtr($tServiceName), _
"ptr", DllStructGetPtr($tDisplayName), _
"dword", $iDesiredAccess, _
"dword", $iServiceType, _
"dword", $iStartType, _
"dword", $iErrorControl, _
"ptr", DllStructGetPtr($tBinaryPath), _
"ptr", DllStructGetPtr($tLoadOrder), _
"ptr", 0, _
"ptr", DllStructGetPtr($tDependencies), _
"ptr", DllStructGetPtr($tStartName), _
"ptr", DllStructGetPtr($tPassword))
Return SetError(_GetLastError(), _LsaCloseServiceHandle($hSC), $iResult)
EndFunc ;==>_CreateService
Func _EmptyWorkingSet($hProcess)
Local $iResult
$iResult = DllCall("psapi.dll", "int", "EmptyWorkingSet", "hWnd", $hProcess)
Return SetError(_GetLastError(), 0, $iResult)
EndFunc ;==>_EmptyWorkingSet
Func _NtQueryInformationProcess($hProcess, $iClass, $pBuffer, $iSizeofBuffer)
Local $iResult
$iResult = DllCall("Ntdll.dll", "dword", "NtQueryInformationProcess", "hWnd", $hProcess, _
"int", $iClass, "ptr", $pBuffer, "ulong", $iSizeofBuffer, "ulong*", 0)
Return SetError($iResult, $iResult, $iResult = 0)
EndFunc ;==>_NtQueryInformationProcess
Func _ChangeServiceConfig2($hService, $iInfoClass, $pBuffer)
Local $iResult
$iResult = DllCall("Advapi32.dll", "bool", "ChangeServiceConfig2", "handle", $hService, _
"dword", $iInfoClass, "ptr", $pBuffer)
Return SetError(_GetLastError(), 0, $iResult)
EndFunc ;==>_ChangeServiceConfig2 ServerMain 函数改成这样,看看行不行。
Func _ServiceMain($iNumberofArgs, $pArguments)
Local $aProcess, $hProcess, $hToken, $aPriv = [[$SE_DEBUG_NAME, 2]]
; 注册服务的“控制处理器”,以用于接收停止、暂停等请求操作,应在ServiceMain函数中尽早调用。
$hServiceStatus = _RegisterServiceCtrlHandlerEx($sServiceName, DllCallbackGetPtr($hHandlerEx))
; 向SCM报告自己的状态。
DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_RUNNING)
_SetServiceStatus($hServiceStatus, $pServiceStatus)
$hToken = _OpenProcessToken(-1)
If Not _IsPrivilegeEnabled($hToken, $SE_DEBUG_NAME) Then
_AdjustTokenPrivileges($hToken, $aPriv)
EndIf
_LsaCloseHandle($hToken)
While 1
If ProcessExists('Clsmn.exe') Then ExitLoop
ShellExecute('C:\Windows\SysWOW64\Clsmn.exe')
Sleep(3000)
WEnd
; 在服务的主函数中循环清理内存。
While 1
$aProcess = ProcessList()
For $i = 1 To $aProcess
$hProcess = _OpenProcess($aProcess[$i])
_EmptyWorkingSet($hProcess)
_LsaCloseHandle($hProcess)
Sleep(50)
Next
WEnd
EndFunc ;==>_ServiceMain
回复 1# pyj521
感谢朋友百忙之中的回复,测试中发现程序运行不起来,我在好好测试下!非常感谢! ServerMain 函数改成这样,看看行不行。
nmgwddj 发表于 2017-3-18 16:14 http://www.autoitx.com/images/common/back.gif
感谢朋友百忙之中的回复,测试中发现程序运行不起来,我在好好测试下!非常感谢! 以前试过用组策略,把软件设置成刚登录系统就启动. 以前试过用组策略,把软件设置成刚登录系统就启动.
Alam 发表于 2017-3-18 18:35 http://www.autoitx.com/images/common/back.gif
用任务计划试过,达不到想要的效果,感谢回复! 回复 6# pyj521
不是计划任务.也不是注册表的 run 项.
而是组策略的系统LONGIN登录脚本....比上面两者要更早执行.
还有个办法,先在注册表屏蔽掉 explorer, 开机登录系统后,先不打开它,
运行完需要的程序,再运行 explorer
我以前在XP系统上弄过. Win 10 下就未知效果了. 回复 4# pyj521
程序编译为 32 位。 回复 7# Alam
主策略不知效果,待会试试,屏蔽掉 explorer的方法影响开机速度,得不偿失,感谢解答! 回复pyj521
程序编译为 32 位。
nmgwddj 发表于 2017-3-18 23:55 http://www.autoitx.com/images/common/back.gif
我是编译为32位的,启动后不会运行设置的程序,测试了一下午,非常感谢你的帮助! 提供一个思路,先百度 srvany ,srvany.exe是Microsoft Windows Resource Kits工具集的一个实用小工具,用于将EXE程序作为Windows服务运行。srvany是其注册程序的服务外壳,我们可以通过它让我们的程序以SYSTEM账户启动,或者实现随机器启动而自启动,也可以隐藏不必要的窗口,比如说控制台窗口等等,如果不会用,在局域网中提供Access文件访问服务,有源码
然后百度Windows服务“允许服务与桌面交互”的使用和修改方法 回复 11# tubaba
感谢提供思路,先去学习学习看看能不能成功! 小白一枚,纯帮顶一下
页:
[1]