|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
学的第一门编程语言是易语言,可能大部分人没有听说过,国内的中文编程,我非常喜欢,但是遇到比较棘手(或者说深入or冷门)的问题时,去网上搜索一些资料吧,发现几乎都是C/C++的源代码,根本看不懂,所以又去学习C/C++,至今有将近两年了,在学校快要毕业了,却一天天的闲的发慌,就找个事做做,仿造开源的精易模块和MSDN的资料参考写了一套服务操作类,现在分享出来,与君共勉。
接下来准备学习一下Python了,不想再继续深入C/C++了,感觉在没毕业前,多学几门语言,会点基础,就业了在考虑深入哪一门吧,准备在接下来的时间在鱼C学习Python了。
下面贴代码
ServiceClass.h
//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
ServiceClass.cpp
// 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;
}
VS的预编译文件也贴出来吧 stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息
// TODO: 在此处引用程序需要的其他头文件
VS的预编译文件也贴出来吧 stdafx.cpp
// stdafx.cpp : 只包括标准包含文件的源文件
// ServiceClass.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用
有什么地方看不懂,回复这个帖子,看到就做出解答! |
|