sky808 发表于 2009-8-6 04:36:30

请问怎样注册系统服务呢?

请问高手们怎样注册一个系统服务呢?
能否详细说说createservice()里的参数用法呢?
多谢!

lynfr8 发表于 2009-8-6 09:25:10

本帖最后由 lynfr8 于 2009-8-6 09:27 编辑

http://www.autoitx.com/viewthr ... fromuid=1003#pid544

http://www.autoitx.com/search.php?searchid=28&orderby=lastpost&ascdesc=desc&searchsubmit=yes&page=3、
Func _CreateService($name, $displayname, $pathname, $startmode = "Automatic", $desktopinteract = True, $startname ="", $servicetype = 16, $errorcontrol = 0)
; Connect to WMI.
    $objServices = ObjGet("winmgmts:root\cimv2")
   
; Obtain the definition of the Win32_Service class.
    $objService = $objServices.Get ("Win32_Service")
   
; Obtain an InParameters object specific to the Win32_Service.Create method.
    $objInParam = $objService.Methods_ ("Create") .inParameters.SpawnInstance_ ()
   
;Add the input parameters.
    $objInParam.Properties_.item ("Name") = $name;< - Service Name
    $objInParam.Properties_.item ("DisplayName") = $displayname;< - Display Name, what you see in the Services control panel
    $objInParam.Properties_.item ("PathName") = $pathname;< - Path and Command Line of the executable
    $objInParam.Properties_.item ("ServiceType") = $servicetype
    $objInParam.Properties_.item ("ErrorControl") = $errorcontrol
    $objInParam.Properties_.item ("StartMode") = $startmode
    $objInParam.Properties_.item ("DesktopInteract") = $desktopinteract
    If not $startname = "" Then
      $objInParam.Properties_.item("StartName") = $startname;< - If null, will run as Local System
    EndIf
    If not $password = "" And not $startname ="" Then
      $objInParam.Properties_.item("StartPassword") = $password;< - Only populate if the SatrtName param is populated
    EndIf
;More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class"

; Execute the method and obtain the return status.
; The OutParameters object in objOutParams is created by the provider.
    $objOutParams = $objService.ExecMethod_ ("Create", $objInParam)
    ConsoleWrite($objOutParams)
EndFunc
Func _DeleteService($name)
    Dim $objWMIService, $objItem, $objService
    Dim $colListOfServices, $strService
; NB strService is case sensitive.
    $strService = $name;< - Service Name
; Connect to WMI.
    $objWMIService = ObjGet("winmgmts:root\cimv2")
    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")
    For $objService in $colListOfServices
      $objService.Delete()
    Next
EndFunc
Func _StartService($name)
    Dim $objWMIService, $objItem, $objService
    Dim $colListOfServices, $strService
; NB strService is case sensitive.
    $strService = $name;< - Service Name
; Connect to WMI.
    $objWMIService = ObjGet("winmgmts:root\cimv2")
    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")
    For $objService in $colListOfServices
      $objService.StartService()
    Next
EndFunc
Func _StopService($name)
    Dim $objWMIService, $objItem, $objService
    Dim $colListOfServices, $strService
; NB strService is case sensitive.
    $strService = $name;< - Service Name
; Connect to WMI.
    $objWMIService = ObjGet("winmgmts:root\cimv2")
    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")
    For $objService in $colListOfServices
      $objService.StopService()
    Next
EndFunc

menfan1 发表于 2009-8-6 12:34:35

温习一下,呵呵

sky808 发表于 2009-8-6 14:23:30

这个utf函数我有,就是不会用。
不知道里面的参数如何填写,没有翻译。
希望高手给各例子。多谢!

afan 发表于 2009-8-6 14:42:50

_CreateService("计算机名","服务名","显示名","要建立服务的文件路径")
一般其它的都不用管了...
比如:
_CreateService("", "nnn", "", "xxx.exe") ;在本机为 xxx.exe 建立名为 nnn 的服务

sky808 发表于 2009-8-6 14:59:52

不行啊,楼上的朋友按照你方法编写提示变量没有声明.

Line 24(File "D:\createservice\create.au3"):

If not $password = "" And not $startname ="" Then
If not ^ ERROR

Error: 变量没有在开始声明.

afan 发表于 2009-8-6 15:15:58

你的变量没声明啊,与这无关哦

sky808 发表于 2009-8-6 15:25:04

这个怎么声明变量啊?
我都没用变量传递,都是他自己函数内部的。
这不要声明的吧?


你看看是否有问题:
_CreateService("","keysv","keyboardservice","c:\keyboardservice.exe")

Func _CreateService($name, $displayname, $pathname, $startmode = "Automatic", $desktopinteract = True, $startname ="", $servicetype = 16, $errorcontrol = 0)
; Connect to WMI.
   $objServices = ObjGet("winmgmts:root\cimv2")
   
; Obtain the definition of the Win32_Service class.
   $objService = $objServices.Get ("Win32_Service")
   
; Obtain an InParameters object specific to the Win32_Service.Create method.
   $objInParam = $objService.Methods_ ("Create") .inParameters.SpawnInstance_ ()
   
;Add the input parameters.
   $objInParam.Properties_.item ("Name") = $name;< - Service Name
   $objInParam.Properties_.item ("DisplayName") = $displayname;< - Display Name, what you see in the Services control panel
   $objInParam.Properties_.item ("PathName") = $pathname;< - Path and Command Line of the executable
   $objInParam.Properties_.item ("ServiceType") = $servicetype
   $objInParam.Properties_.item ("ErrorControl") = $errorcontrol
   $objInParam.Properties_.item ("StartMode") = $startmode
   $objInParam.Properties_.item ("DesktopInteract") = $desktopinteract
   If not $startname = "" Then
         $objInParam.Properties_.item("StartName") = $startname;< - If null, will run as Local System
   EndIf
   If not $password = "" And not $startname ="" Then
         $objInParam.Properties_.item("StartPassword") = $password;< - Only populate if the SatrtName param is populated
   EndIf

;More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class"


; Execute the method and obtain the return status.
; The OutParameters object in objOutParams is created by the provider.
   $objOutParams = $objService.ExecMethod_ ("Create", $objInParam)
   ConsoleWrite($objOutParams)
EndFunc

afan 发表于 2009-8-6 15:57:30

你这个函数内部也没见声明,可能这只是一个UDF的一部分,又或者缺少某个头文件吧

netegg 发表于 2009-8-6 16:52:27

_CreateService这应该是某个udf里的吧,脚本开始的时候,没有声明调用那个au3吧(#include)

sky808 发表于 2009-8-6 16:54:27

那谁知道那个官方services.au3的创建服务方法呢?

函数的圆形是这样的:
Func _Service_Create( $sServiceName, _
                $sDisplayName, _
                $iServiceType, _
                $iStartType, _
                $iErrorControl, _
                $sBinaryPath, _
                $sLoadOrderGroup = Default, _
                $fTagId = Default, _
                $vDependencies = Default, _
                $sServiceUser = Default, _
                $sPassword = Default, _
                $sComputerName = "" )

buvi不知道每一项参数填写什么,知道的朋友麻烦说下.多谢了!
页: [1]
查看完整版本: 请问怎样注册系统服务呢?