zoukankan      html  css  js  c++  java
  • api控制系统服务

    SC_HANDLE  sc=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); //服务控制器

    //获得服务状态
    DWORD get_srvst(SC_HANDLE sc)
    {

     SC_HANDLE  schService;
     
    if (NULL == sc) 
        printf(
    "OpenSCManager failed (%d)\n", GetLastError());

     schService 
    = OpenService(
            sc,          
    // SCM database 
            L"sharedaccess",          // service name
            SERVICE_ALL_ACCESS); 
     
        
    if (schService == NULL) 
        

      printf(
    "OpenSCManager failed (%d)\n", GetLastError());
            
    return 0
        }

     SC_STATUS_TYPE st;
     LPSERVICE_STATUS_PROCESS buf;
     buf
    =(LPSERVICE_STATUS_PROCESS) LocalAlloc(LPTR, 1024);
     DWORD dwneed;
     
    if(QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE)buf,1024,&dwneed))
     
    {
      
    return buf->dwCurrentState;
     }


     
    return 0;

     
    }


    int Open_Service(SC_HANDLE sc)
    {


        SC_HANDLE  schService;
        
    if (NULL == sc) 
        printf(
    "OpenSCManager failed (%d)\n", GetLastError());

        schService 
    = OpenService(
            sc,          
    // SCM database 
            L"sharedaccess",          // service name
            SERVICE_ALL_ACCESS); 
     
        
    if (schService == NULL) 
        

            printf(
    "OpenSCManager failed (%d)\n", GetLastError());
            
    return 0
        }




        
    if (!StartService(
                schService,  
    // handle to service 
                0,           // number of arguments 
                NULL) )      // no arguments 
        {
            
    return 0
        }

        
    else 
        
    {
            printf(
    "Service start pending.\n"); 
        }

        
        CloseServiceHandle(schService);
        
    return 0;
    }

    int Close_Service(SC_HANDLE sc)
    {
        
        SC_HANDLE  schService;
        
    if (NULL == sc) 
        printf(
    "OpenSCManager failed (%d)\n", GetLastError());

        schService 
    = OpenService(
            sc,          
    // SCM database 
            L"sharedaccess",          // service name
            SERVICE_ALL_ACCESS); 
     

        
    if (schService == NULL) 
        

            
    return 0
        }

        SERVICE_STATUS ssStatus; 

        ControlService(schService,SERVICE_CONTROL_STOP,
    &ssStatus);
        CloseServiceHandle(schService);
    }
    dwCurrentState
    Current state of the service. This member can be one of the following values.
    Value Meaning
    SERVICE_CONTINUE_PENDING
    0x00000005
    The service continue is pending.
    SERVICE_PAUSE_PENDING
    0x00000006
    The service pause is pending.
    SERVICE_PAUSED
    0x00000007
    The service is paused.
    SERVICE_RUNNING
    0x00000004
    The service is running.
    SERVICE_START_PENDING
    0x00000002
    The service is starting.
    SERVICE_STOP_PENDING
    0x00000003
    The service is stopping.
    SERVICE_STOPPED
    0x00000001
    The service is not running.
  • 相关阅读:
    nginxWebUI
    c#通过串口及CAN模块实现上位及下位机通讯
    使用IDEA创建SpringBoot项目出现intellij idea No active profile set, falling back to default profiles: default
    linux服务器创建python环境
    在Linux服务器上安装anaconda
    牛客-小w的魔术扑克【并查集】
    bzoj#4161-Shlw loves matrixI【常系数线性齐次递推】
    CF903G-Yet Another Maxflow Problem【线段树,最大流】
    P4700-[CEOI2011]Traffic【tarjan,dp】
    CF1039D-You Are Given a Tree【根号分治,贪心】
  • 原文地址:https://www.cnblogs.com/ahuo/p/684249.html
Copyright © 2011-2022 走看看