编程论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

界面工厂编程视频教程广告联系qq1031180668广告位
查看: 86|回复: 0

[c/c++] C++操作Windows服务类

[复制链接]
  • ta_mind
    擦汗
    2016-3-2 13:17
  • classn_01: 5 classn_02

    [LV.2]偶尔看看I

    640

    主题

    651

    帖子

    675

    积分

    高级会员

    Rank: 4

    积分
    675
    发表于 2018-4-22 17:02:20 | 显示全部楼层 |阅读模式
    [C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode
    代码环境:
          操作系统:Win7 64bit
          I   D   E   : VS2012
    
    
    
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    stdafx.h
    // stdafx.h : 标准系统包含文件的包含文件,
    // 或是经常使用但不常更改的
    // 特定于项目的包含文件
    //
    #pragma once
    #include "targetver.h"
    #define WIN32_LEAN_AND_MEAN             //  从 Windows 头文件中排除极少使用的信息
    // TODO: 在此处引用程序需要的其他头文件
    
    
    [color=rgb(51, 102, 153) !important]复制代码
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------
    stdafx.cpp
    // stdafx.cpp : 只包括标准包含文件的源文件
    // ServiceClass.pch 将作为预编译头
    // stdafx.obj 将包含预编译类型信息
    #include "stdafx.h"
    // TODO: 在 STDAFX.H 中
    // 引用任何所需的附加头文件,而不是在此文件中引用
    
    
    [color=rgb(51, 102, 153) !important]复制代码
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------
    targetver.h
    #pragma once
    // 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。
    // 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将
    // WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。
    #include <SDKDDKVer.h>
    
    
    [color=rgb(51, 102, 153) !important]复制代码
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------
    serviceClass.h
    #ifndef SERVICE_H_
    #define SERVICE_H_
    #define _CRT_SECURE_NO_WARNINGS
    #include <string>
    #include <Windows.h>
    class ServiceClass
    {
    public:
            SC_HANDLE m_hSCManager,m_hService;
            char m_serviceName[256];
            ServiceClass(char *serviceName=NULL);
            ~ServiceClass(void);
            int GetServiceStat(char *serviceName=NULL);
            DWORD GetServiceType(char *serviceName=NULL);
            char *GetServiceDescrible(char *desc,char *serviceName=NULL);
            BOOL SetServiceDescrible(char *newDescrible,char *serviceName=NULL);
            char *GetDisbleName(char *disbleName,char *serviceName=NULL);
            char *GetServiceName(char *serviceName,char *disbleName);
            BOOL InstallService(char *serviceName,char *disbleName,char *exe,char *serviceDescrble=NULL,BOOL bAllowDesktopInteraction=FALSE,int serviceType=16,int startType=3,char *interdePendent=NULL,char *loginUser=NULL,char *loginPass=NULL);
            BOOL UnInstallService(char *serviceName=NULL);
            BOOL StartServices(char *serviceName=NULL);
            BOOL StopService(char *serviceName=NULL);
            BOOL SuspendService(char *serviceName=NULL);
            BOOL RecoveryService(char *serviceName=NULL);
            BOOL Existence(char *serviceName);
            BOOL SetStartType(int type,char *serviceName=NULL);
            DWORD GetStartType(char *serviceName=NULL);
            char *GetFileUrl(char *fileUrl,char *serviceName=NULL);
            BOOL SetFileUrl(char *fileUrl,char *serviceName=NULL);
            char *GetLoginType(char *loginType,char *serviceName=NULL);
            int GetServiceNumber(int serviceStatus=3);
            BOOL ReStartService(char *serviceName=NULL);
    };
    #endif
    
    [color=rgb(51, 102, 153) !important]复制代码
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------
    serviceClass.cpp
    #include "stdafx.h"
    #include "ServiceClass.h"
    ServiceClass::ServiceClass(char *serviceName)
    {
            if (serviceName)
            {
                    strcpy(ServiceClass::m_serviceName,serviceName);
                    ServiceClass::m_hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    if (!m_hSCManager)
                    {
                            printf("ServiceClass Init fail!\n");
                    } else {
                            ServiceClass::m_hService = OpenService(ServiceClass::m_hSCManager,ServiceClass::m_serviceName,SC_MANAGER_ALL_ACCESS );
                            if (!ServiceClass::m_hSCManager)
                            {
                                    printf("ServiceClass::hService Init fail!\n");
                            }
                    }
            } else {
                    ServiceClass::m_hSCManager = NULL;
                    ServiceClass::m_hService = NULL;
                    memset(ServiceClass::m_serviceName,'\0',sizeof(ServiceClass::m_serviceName));
            }
    }
    ServiceClass::~ServiceClass(void)
    {
            CloseServiceHandle(ServiceClass::m_hService);
            CloseServiceHandle(ServiceClass::m_hSCManager);
    }
    int ServiceClass::GetServiceStat(char *serviceName)
    {
            SERVICE_STATUS serStatus;
            int CurrentState;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return -1;
                    }
                    hService = OpenService(hSCManager,serviceName,GENERIC_READ);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return -2;
                    }
                    if (QueryServiceStatus(hService,&serStatus))
                    {
                            CurrentState = serStatus.dwCurrentState;
                            CloseServiceHandle(hService);
                            CloseServiceHandle(hSCManager);
                    }
            } else {
                    if (QueryServiceStatus(m_hService,&serStatus))
                    {
                            CurrentState = serStatus.dwCurrentState;
                    }
            }
            return CurrentState;
    }
    DWORD ServiceClass::GetServiceType(char *serviceName)
    {
            DWORD dwLen;
            BOOL bResult;
            DWORD dwServiceType = 0;
            LPQUERY_SERVICE_CONFIG lpServiceConfig;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return 0;
                    }
                    hService = OpenService(hSCManager,serviceName,GENERIC_READ);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return 0;
                    }
                    QueryServiceConfig(hService,NULL,NULL,&dwLen);
                    lpServiceConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwLen);
                    bResult = QueryServiceConfig(hService,lpServiceConfig,dwLen,&dwLen);
                    CloseHandle(hService);
                    CloseHandle(hSCManager);
            } else {
                    QueryServiceConfig(m_hService,NULL,NULL,&dwLen);
                    lpServiceConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwLen);
                    bResult = QueryServiceConfig(m_hService,lpServiceConfig,dwLen,&dwLen);
            }
            if (bResult)
            {
                    dwServiceType = lpServiceConfig->dwServiceType;
            }
            GlobalFree(lpServiceConfig);
            return dwServiceType;
    }
    char *ServiceClass::GetServiceDescrible(char *desc,char *serviceName)
    {
            DWORD dwLen = 0;
            BYTE *hMem = NULL;
            SERVICE_DESCRIPTION lpBuffer = {0};
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return NULL;
                    }
                    hService = OpenService(hSCManager,serviceName,GENERIC_READ);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return NULL;
                    }
                    QueryServiceConfig2(hService,1,NULL,NULL,&dwLen);
                    hMem = (BYTE *)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwLen);
                    if (QueryServiceConfig2(hService,1,hMem,dwLen,&dwLen))
                    {
                            RtlMoveMemory(&lpBuffer,hMem,4);        // memmove
                    }
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    QueryServiceConfig2(m_hService,1,NULL,NULL,&dwLen);
                    hMem = (BYTE *)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwLen);
                    if (QueryServiceConfig2(m_hService,1,hMem,dwLen,&dwLen))
                    {
                            RtlMoveMemory(&lpBuffer,hMem,4);        // memmove
                    }
            }
            strcpy(desc,lpBuffer.lpDescription);
            HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,hMem);
            return desc;
    }
    BOOL ServiceClass::SetServiceDescrible(char *newDescrible,char *serviceName)
    {
            BOOL bOk = FALSE;
            SERVICE_DESCRIPTION sd = {0};
            LPSTR szDesc = newDescrible;
            sd.lpDescription = szDesc;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_EXECUTE);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,GENERIC_EXECUTE);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = ChangeServiceConfig2A(hService,1,&sd);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    bOk = ChangeServiceConfig2(m_hService,1,&sd);
            }
            return bOk;
    }
    char *ServiceClass::GetDisbleName(char *disbleName,char *serviceName)
    {
            BOOL bResult;
            DWORD dwLen;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
                    if (!hSCManager)
                    {
                            return NULL;
                    }
                    GetServiceDisplayName(hSCManager,serviceName,NULL,&dwLen);
                    bResult = GetServiceDisplayName(hSCManager,serviceName,disbleName,&dwLen);
                    CloseServiceHandle(hSCManager);
            } else {
                    GetServiceDisplayName(m_hSCManager,serviceName,NULL,&dwLen);
                    bResult = GetServiceDisplayName(m_hSCManager,m_serviceName,disbleName,&dwLen);
            }
            return (bResult?disbleName:NULL);
    }
    char *ServiceClass::GetServiceName(char *serviceName,char *disbleName)
    {
            DWORD dwLen = 0;
            BOOL bOk = FALSE;
            GetServiceKeyName(ServiceClass::m_hSCManager,disbleName,NULL,&dwLen);
            bOk = GetServiceKeyName(ServiceClass::m_hSCManager,disbleName,serviceName,&dwLen);
            return (bOk?serviceName:NULL);
    }
    BOOL ServiceClass::InstallService(char *serviceName,char *disbleName,char *exe,char *serviceDescrble,BOOL bAllowDesktopInteraction,int serviceType,int startType,char *interdePendent,char *loginUser,char *loginPass)
    {
            if (bAllowDesktopInteraction)
            {
                    serviceType = serviceType|SERVICE_INTERACTIVE_PROCESS;
            }
            ServiceClass::m_hService = CreateService(ServiceClass::m_hSCManager,serviceName,disbleName,SERVICE_ALL_ACCESS,serviceType,startType,SERVICE_ERROR_NORMAL,exe,NULL,NULL,interdePendent,loginUser,loginPass);
            if (ServiceClass::m_hService)
            {
                    ServiceClass::SetServiceDescrible(serviceDescrble,serviceName);
            }
            return (ServiceClass::m_hService != 0);
    }
    BOOL ServiceClass::UnInstallService(char *serviceName)
    {
            BOOL bOk;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = DeleteService(hService);
                    CloseHandle(hService);
            } else {
                    bOk = DeleteService(ServiceClass::m_hService);
                    CloseHandle(ServiceClass::m_hService);
            }
            return bOk;
    }
    BOOL ServiceClass::StartServices(char *serviceName)
    {
            BOOL bOk;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SERVICE_START);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SERVICE_START);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = StartService(hService,NULL,NULL);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    bOk = StartService(ServiceClass::m_hService,NULL,NULL);
            }
            return bOk;
    }
    BOOL ServiceClass::StopService(char *serviceName)
    {
            BOOL bOk;
            SERVICE_STATUS status;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = ControlService(hService,1,&status);
            } else {
                    bOk = ControlService(ServiceClass::m_hService,1,&status);
            }
            return bOk;
    }
    BOOL ServiceClass::SuspendService(char *serviceName)
    {
            BOOL bOk;
            SERVICE_STATUS status;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = ControlService(hService,2,&status);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    bOk = ControlService(ServiceClass::m_hService,2,&status);
            }
            return bOk;
    }
    BOOL ServiceClass::RecoveryService(char *serviceName)
    {
            BOOL bOk;
            SERVICE_STATUS status;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = ControlService(hService,SERVICE_CONTROL_CONTINUE,&status);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    bOk = ControlService(ServiceClass::m_hService,SERVICE_CONTROL_CONTINUE,&status);
            }
            return bOk;
    }
    BOOL ServiceClass::Existence(char *serviceName)
    {
            SC_HANDLE hSCManager,hService;
            hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
            if (!hSCManager)
            {
                    hService = OpenService(hSCManager,serviceName,GENERIC_READ);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            }
            return (hService!=0);
    }
    BOOL ServiceClass::SetStartType(int type,char *serviceName)
    {
            BOOL bOk;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = ChangeServiceConfig(hService,4294967295,type,4294967295,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    bOk = ChangeServiceConfig(ServiceClass::m_hService,4294967295,type,4294967295,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
            }
            return bOk;
    }
    DWORD ServiceClass::GetStartType(char *serviceName)
    {
            LPQUERY_SERVICE_CONFIG lpsc = NULL;
            DWORD dwLen,type;
            BOOL bOk;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return -1;
                    }
                    hService = OpenService(hSCManager,serviceName,GENERIC_READ);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return -1;
                    }
                    QueryServiceConfig(hService,NULL,NULL,&dwLen);
                    lpsc = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwLen);
                    bOk = QueryServiceConfig(hService,lpsc,dwLen,&dwLen);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    QueryServiceConfig(ServiceClass::m_hService,NULL,NULL,&dwLen);
                    lpsc = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwLen);
                    bOk = QueryServiceConfig(ServiceClass::m_hService,lpsc,dwLen,&dwLen);
            }
            if (bOk)
            {
                    if (lpsc->dwStartType == 2 && lpsc->dwServiceType == 32)
                    {
                            type = 1;        // 自动
                    } else if (lpsc->dwStartType == 2 && lpsc->dwServiceType == 16)
                    {
                            type = 2;        // 自动(延迟启动)
                    } else if (lpsc->dwStartType == 3)
                    {
                            type =3;        // 手动
                    } else if (lpsc->dwStartType == 4)
                    {
                            type = 4;        // 禁用
                    }
            }
            GlobalFree(lpsc);
            return type;
    }
    char *ServiceClass::GetFileUrl(char *fileUrl,char *serviceName)
    {
            BOOL bOk;
            DWORD dwByteneeded;
            LPQUERY_SERVICE_CONFIG lpsc = {0};
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    QueryServiceConfig(hService,NULL,NULL,&dwByteneeded);
                    lpsc = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwByteneeded);
                    bOk = QueryServiceConfig(hService,lpsc,dwByteneeded,&dwByteneeded);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    QueryServiceConfig(ServiceClass::m_hService,NULL,NULL,&dwByteneeded);
                    lpsc = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwByteneeded);
                    bOk = QueryServiceConfig(ServiceClass::m_hService,lpsc,dwByteneeded,&dwByteneeded);
            }
            _strset(fileUrl,0);
            strcpy(fileUrl,lpsc->lpBinaryPathName);
            GlobalFree(lpsc);
            return fileUrl;
    }
    BOOL ServiceClass::SetFileUrl(char *fileUrl,char *serviceName)
    {
            BOOL bOk;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return FALSE;
                    }
                    hService = OpenService(hSCManager,serviceName,SC_MANAGER_ALL_ACCESS);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return FALSE;
                    }
                    bOk = ChangeServiceConfig(hService,4294967295,4294967295,4294967295,fileUrl,NULL,NULL,NULL,NULL,NULL,NULL);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    bOk = ChangeServiceConfig(ServiceClass::m_hService,4294967295,4294967295,4294967295,fileUrl,NULL,NULL,NULL,NULL,NULL,NULL);
            }
            return bOk;
    }
    char *ServiceClass::GetLoginType(char *loginType,char *serviceName)
    {
            char ss[100] = "";
            char *index = NULL;
            BOOL bOk = FALSE;
            DWORD dwByteneeded=0;
            LPQUERY_SERVICE_CONFIG lpsc = NULL;
            if (serviceName)
            {
                    SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,GENERIC_READ);
                    SC_HANDLE hService = 0;
                    if (!hSCManager)
                    {
                            return NULL;
                    }
                    hService = OpenService(hSCManager,serviceName,GENERIC_READ);
                    if (!hService)
                    {
                            CloseServiceHandle(hSCManager);
                            return NULL;
                    }
                    QueryServiceConfig(hService,NULL,NULL,&dwByteneeded);
                    lpsc = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwByteneeded);
                    bOk = QueryServiceConfig(hService,lpsc,dwByteneeded,&dwByteneeded);
                    CloseServiceHandle(hService);
                    CloseServiceHandle(hSCManager);
            } else {
                    QueryServiceConfig(ServiceClass::m_hService,NULL,NULL,&dwByteneeded);
                    lpsc = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(64,dwByteneeded);
                    bOk = QueryServiceConfig(ServiceClass::m_hService,lpsc,dwByteneeded,&dwByteneeded);
            }
            if (bOk)
            {
                    strcpy(ss,_strupr(lpsc->lpServiceStartName));
                    GlobalFree(lpsc);
                    _strset(loginType,0);
                    if (!strstr(ss,"localservice"))
                    {
                            strcpy(loginType,"本地服务");
                    } else if (!strstr(ss,"Networkservice"))
                    {
                            strcpy(loginType,"网络服务");
                    } else if (!strstr(ss,"localsystem"))
                    {
                            strcpy(loginType,"本地系统");
                    }
            }
            return loginType;
    }
    int ServiceClass::GetServiceNumber(int serviceStatus)
    {
            LPENUM_SERVICE_STATUS lpsc = NULL;
            DWORD dwBytesNeeded = 0,dwReturned = 0,dwHandle = 0;
            BOOL bOk = FALSE;
            EnumServicesStatus(ServiceClass::m_hSCManager,48,serviceStatus,NULL,NULL,&dwBytesNeeded,&dwReturned,&dwHandle);
            lpsc = (LPENUM_SERVICE_STATUS)GlobalAlloc(64,dwBytesNeeded);
            EnumServicesStatus(ServiceClass::m_hSCManager,48,serviceStatus,lpsc,dwBytesNeeded,&dwBytesNeeded,&dwReturned,&dwHandle);
            GlobalFree(lpsc);
            return dwReturned;
    }
    BOOL ServiceClass::ReStartService(char *serviceName)
    {
            if (serviceName)
            {
                    if (ServiceClass::StopService(serviceName))
                            return ServiceClass::StartServices(serviceName);
            } else {
                    if (ServiceClass::StopService(ServiceClass::m_serviceName))
                            return ServiceClass::StartServices(ServiceClass::m_serviceName);
            }
            return FALSE;
    }
    
    
    
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|Archiver|小黑屋|sitemap|编程论坛 - 一个单纯的编程学习交流论坛 ( 豫ICP备15032706号-2 )

    GMT+8, 2018-5-25 03:33 , Processed in 0.109375 second(s), 28 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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