Option Explicit
Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Private Sub Command1_Click()
Dim cmdline
cmdline = "sc config  Server start= disable" 'auto:自动 demand:手动 disable:禁用
WinExec cmdline, 0 '0 表示隐藏执行
End Sub上面是我写的代码,为什么运行 点了后没有反应。大家都帮我看看。

解决方案 »

  1.   

    可能有依赖关系,试试不同的 depend 选项。
    提示:先在命令行中测试通过,再用 VB 调用。
      

  2.   

       感觉还是用API好点  服务名要用短名 
    Public Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
    Public Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
    Public Type SERVICE_STATUS
            dwServiceType   As Long
            dwCurrentState   As Long
            dwControlsAccepted   As Long
            dwWin32ExitCode   As Long
            dwServiceSpecificExitCode   As Long
            dwCheckPoint   As Long
            dwWaitHint   As Long
    End Type
    Public Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
    Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
    Public Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As LongPublic Const SERVICE_ALL_ACCESS = 983551'*************************************************************************
    '**函 数 名:ServiceControl
    '**输    入:ServiceName(String) - 服务名
    '**        :ServiceStatic(Byte) - 服务控制 [1(停止),2(暂停),3(继续)..参考Service Controls声明]
    '**输    出:(Boolean) - True 成功 False 失败
    '**功能描述:控制服务状态,该服务必须处于启动状态
    '*************************************************************************
    Public Function ServiceControl(ServiceName As String, ServiceStatic As Byte) As Boolean
        Dim Service As SERVICE_STATUS
        Dim hSCManager As Long, hService As Long    hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
        hService = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS)
        ServiceControl = ControlService(hService, ServiceStatic, Service)
        CloseServiceHandle (hService)
        CloseServiceHandle (hSCManager)
    End Function
    '*************************************************************************
    '**函 数 名:ServiceStart
    '**输    入:ServiceName(String) - 服务名
    '**输    出:(Boolean) - True 成功 False 失败
    '**功能描述:启动服务
    '*************************************************************************
    Public Function ServiceStart(ServiceName As String) As Boolean
        Dim hSCManager As Long
        Dim hService As Long
        hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
        hService = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS)
        ServiceStart = StartService(hService, 0, 0)
        CloseServiceHandle (hService)
        CloseServiceHandle (hSCManager)
    End Function
      

  3.   

    SC_MANAGER_ALL_ACCESS=983103 
    少了一个常数。