找回密码
 加入
搜索
查看: 5350|回复: 7

[AU3基础] 关于P版的纯au3编写服务程序的实际应用修改注册表求助

  [复制链接]
发表于 2012-4-12 16:53:23 | 显示全部楼层 |阅读模式
本帖最后由 872777825 于 2012-4-14 14:18 编辑
#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_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[3]")
_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)
        _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[256]")
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[1][2] = [[$SE_DEBUG_NAME, 2]]

        ; 注册服务的“控制处理器”,以用于接收停止、暂停等请求操作,应在ServiceMain函数中尽早调用。
        $hServiceStatus = _RegisterServiceCtrlHandlerEx($sServiceName, DllCallbackGetPtr($hHandlerEx))

        ; 向SCM报告自己的状态。
        DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_RUNNING)
        _SetServiceStatus($hServiceStatus, $pServiceStatus)

        Msgbox(4, "Bingo~!!", "I am running as a service~~")

        RegWrite("HKEY_USERS\Software\Microsoft\Internet Explorer\Main", "Start Page", "REG_SZ", "http://www.baidu.com")        
     
      $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[0][0]
                        $hProcess = _OpenProcess($aProcess[$i][1])
                        _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[0])
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[0])
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[0])
EndFunc        ;==>_RegisterServiceCtrlHandlerEx

Func _StartServiceCtrlDispatcher($pServiceTable)
        Local $iResult
        $iResult = DllCall("AdvApi32.dll", "int", "StartServiceCtrlDispatcher", _
                        "ptr", $pServiceTable)
        Return SetError(_GetLastError(), 0, $iResult[0])
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[1]")
                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[0])
EndFunc        ;==>_CreateService

Func _EmptyWorkingSet($hProcess)
        Local $iResult
        $iResult = DllCall("psapi.dll", "int", "EmptyWorkingSet", "hWnd", $hProcess)
        Return SetError(_GetLastError(), 0, $iResult[0])
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[0], $iResult[5], $iResult[0] = 0)
EndFunc        ;==>_NtQueryInformationProcess
如上   在86行处添加修改注册表命令  未能成功  不知道何原因

希望能得到各位老师的指点
发表于 2012-4-12 19:19:49 | 显示全部楼层
服务程序默认是以SYSTEM权限运行的,应该确保注册表键的安全权限 允许SYSTEM账户的访问。PS,楼主兄竟然把写注册表的代码删除?纯粹是不想解决问题。
 楼主| 发表于 2012-4-12 20:11:27 | 显示全部楼层
回复 2# pusofalse


    呵呵  谢谢P大 亲自回复   看了下   权限默认是给system用户开放的
不如修改主页 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
    麻烦P大帮忙看看

   同时我发现很多函数在这段代码里面使用都不是很正常  不知道是不是我使用的问题

  如果 @desktopdir   这个桌面路径   但东西去被发送到了C盘根目录
  我是做网吧的   所以你这段代码对我来说  很时间  直接吧调用扔到启动服务   
  一般人找不到我在启动的东西  不错     麻烦P大再指点下   谢谢
 楼主| 发表于 2012-4-13 01:34:19 | 显示全部楼层
P大   有时间帮我解决下吧   等待ing
 楼主| 发表于 2012-4-14 13:57:48 | 显示全部楼层
本帖最后由 872777825 于 2012-4-14 14:10 编辑

回复 6# lele9013


     我网吧用的  修改主页也没事  因为我这主页被文化方面的插件强奸了

   试了下  还是不行    到底试什么原因
发表于 2012-4-14 22:58:24 | 显示全部楼层
HKEY_USERS\Software\... 这个键存在吗?反正我的系统中不存在。
应该改成HKEY_CURRENT_USER\Software\...。HKEY_CURRENT_USER,这个主键中定义的是“当前用户”的一些系统配置信息,注意是“当前用户”,以服务形式启动的应用程序,当前用户是SYSTEM,HKEY_CURRENT_USER中是SYSTEM用户的配置信息。以管理员身份运行的程序,HKEY_CURRENT_USER则对应Administrator(如果没改名)。总之,当前线程所属的用户是什么,HKEY_CURRENT_USER则对应着哪个用户。在操作这个注册表键之前,应该先模拟一下用户场景,使当前线程用户变为登录到默认桌面的用户,之后再恢复为SYSTEM用户。

#include <lsasecur.au3>

_SeCloseHandle(_SeImpersonateSystemContext("explorer.exe"))
RegWrite("HKEY_CURRENT_USER\Software\...")
_SeRevertToSelf()


lsasecur.au3与LocalSecurityAuthority.au3有部分冲突,用#include <lsasecur.au3>代替#include <LocalSecurityAuthority.au3>。另外把89~93行的代码删除,并用_SeCloseHandle代替_LsaCloseHandle。
发表于 2012-4-14 23:22:43 | 显示全部楼层
高手学习 也正需要次源码
 楼主| 发表于 2012-4-16 16:21:23 | 显示全部楼层
回复 8# pusofalse


    谢谢老大的 耐心回复  前两天出去了   没及时来看

刚刚安装您的说法 去改了  不过编译时又出现问题  麻烦您再帮忙看看  错误如下
N:\test.au3(79,77) : WARNING: $SE_DEBUG_NAME: 使用前并未进行声明.
        Local $aProcess, $hProcess, $hToken, $aPriv[1][2] = [[$SE_DEBUG_NAME,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(191,88) : 错误: 表达式错误
                        $sBuffer &= "wchar[" & StringLen($aDependencies[$i]) + 1 & "];")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(79,77) : ERROR: $SE_DEBUG_NAME: 未声明的全局变量.
        Local $aProcess, $hProcess, $hToken, $aPriv[1][2] = [[$SE_DEBUG_NAME,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(38,59) : ERROR: _LsaOpenService(): undefined function.
        $hService = _LsaOpenService($sServiceName, 0xF01FF)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(43,41) : ERROR: _LsaCloseServiceHandle(): undefined function.
        _LsaCloseServiceHandle($hService)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(98,66) : ERROR: _OpenProcess(): undefined function.
                        $hProcess = _OpenProcess($aProcess[$i][1])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(134,39) : ERROR: _GetLastError(): undefined function.
        Return SetError(_GetLastError()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3(198,39) : ERROR: _LsaOpenSCManager(): undefined function.
        $hSC = _LsaOpenSCManager("", 2)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
N:\test.au3 - 7 error(s), 1 warning(s)
下面是我根据您说的去修改过的   您看看是不是我修改错了
#NoTrayIcon
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <lsasecur.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_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[3]")
_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)
        _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[256]")
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[1][2] = [[$SE_DEBUG_NAME, 2]]

        ; 注册服务的“控制处理器”,以用于接收停止、暂停等请求操作,应在ServiceMain函数中尽早调用。
        $hServiceStatus = _RegisterServiceCtrlHandlerEx($sServiceName, DllCallbackGetPtr($hHandlerEx))

        ; 向SCM报告自己的状态。
        DllStructSetData($tServiceStatus, "CurrentState", $SERVICE_RUNNING)
        _SetServiceStatus($hServiceStatus, $pServiceStatus)

        Msgbox(4, "Bingo~!!", "I am running as a service~~")
                 
                _SeCloseHandle(_SeImpersonateSystemContext("explorer.exe"))
                RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main", "Start Page", "REG_SZ", "http://www.baidu.com")
                _SeRevertToSelf()
                
        ; 在服务的主函数中循环清理内存。
        While 1
                $aProcess = ProcessList()
                For $i = 1 To $aProcess[0][0]
                        $hProcess = _OpenProcess($aProcess[$i][1])
                        _EmptyWorkingSet($hProcess)
                        _SeCloseHandle($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[0])
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[0])
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[0])
EndFunc        ;==>_RegisterServiceCtrlHandlerEx

Func _StartServiceCtrlDispatcher($pServiceTable)
        Local $iResult
        $iResult = DllCall("AdvApi32.dll", "int", "StartServiceCtrlDispatcher", _
                        "ptr", $pServiceTable)
        Return SetError(_GetLastError(), 0, $iResult[0])
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[1]")
                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[0])
EndFunc        ;==>_CreateService

Func _EmptyWorkingSet($hProcess)
        Local $iResult
        $iResult = DllCall("psapi.dll", "int", "EmptyWorkingSet", "hWnd", $hProcess)
        Return SetError(_GetLastError(), 0, $iResult[0])
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[0], $iResult[5], $iResult[0] = 0)
EndFunc        ;==>_NtQueryInformationProcess
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-20 16:41 , Processed in 0.088901 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表