服务管理 CMD版
功能如下:
查看服务
添加服务
删除服务
停止服务
启动服务
程序很简单运行效果如图所示
为了方便,我把源代码一块打包了,需要的话可以自己修改,有问题可以一块讨论……呵呵
完整代码如下:
//
/
// 服务管理器-命令行版 //
// 日期:2007/06/08 //
// 作者:冷风 //
// 文件:Server.cpp //
// 信箱:xo0888@Tom.com //
// QQ: 121121606 //
// 说明:若有问题请访问 Blog.csdn.net/chinafe 偶可以提供免费技术支持 呵呵 //
// /
#include < stdio.h >
#include < windows.h >
#include < Winsvc.h >
BOOL DisplayServices();
void AddServices();
void DelServices();
void StopServices();
void StartServices();
main()
{
int i = 10 ;
while (i != 0 )
{
printf( " **************************************************** " );
printf( " 服务管理器-命令行版 " );
printf( " 作者:冷风 2007.6.8 " );
printf( " **************************************************** " );
printf( " 查看服务请输入1 " );
printf( " 添加服务请输入2 " );
printf( " 删除服务请输入3 " );
printf( " 停止服务请输入4 " );
printf( " 启动服务请输入5 " );
printf( " 退出程序请输入0 " );
scanf ( " %d " , & i);
if ( 1 == i)
{
DisplayServices();
}
if ( 2 == i)
{
AddServices();
}
if ( 3 == i)
{
DelServices();
}
if ( 4 == i)
{
StopServices();
}
if ( 5 == i)
{
StartServices();
}
}
return 0 ;
}
BOOL DisplayServices()
{
LPENUM_SERVICE_STATUS lpServices = NULL;
DWORD nSize = 0 ;
DWORD nServicesReturned;
DWORD nResumeHandle = 0 ;
DWORD dwServiceType = SERVICE_WIN32;
SC_HANDLE schSCManager = NULL;
BOOL Flag = FALSE;
DWORD i = 0 ;
UINT j = 0 ;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) // Fail To Open SCM
{
printf( " Fail To Open SCM " );
return FALSE;
}
lpServices = (LPENUM_SERVICE_STATUS) LocalAlloc(LPTR, 64 * 1024 ); // Allocate Ram
if (lpServices == NULL) // Fail To Allocate Ram
{
printf( " Fail To Allocate Ram " );
goto CleanUP;
}
// Enum All Service Based On Service Type
if (EnumServicesStatus(schSCManager,
dwServiceType,
SERVICE_STATE_ALL,
(LPENUM_SERVICE_STATUS)lpServices,
64 * 1024 ,
& nSize,
& nServicesReturned,
& nResumeHandle) == NULL)
{
printf( " Fail To Enum Service " );
goto CleanUP;
}
// Display The Services
printf( " %-34s%s " , " ServiceName " , " DisplayName " );
for (i = 0 ; i < nServicesReturned; i ++ )
{
printf( " %d:%-32s%s " , ++ j,lpServices[i].lpServiceName, lpServices[i].lpDisplayName);
}
Flag = TRUE;
// Close Service Handle,Free Allocated Ram And Return To The Caller
CleanUP:
CloseServiceHandle(schSCManager);
if (lpServices != NULL)
{
LocalFree(lpServices);
}
getchar ();
return Flag;
}
void AddServices()
{
char name[ 100 ];
char info[ 200 ];
char path[ 300 ];
printf( " 请输入服务名 " );
scanf ( " %s " , & name);
printf( " 请输入服务描述 " );
scanf ( " %s " , & info);
printf( " 请输入程序路径 " );
scanf ( " %s " , & path);
SC_HANDLE manager = NULL;
SC_HANDLE service = NULL;
if ((manager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = CreateService(
manager,name,info,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, 0 , 0 , 0 , 0 , 0 );
if (service)
printf( " 服务创建成功 " );
else
printf( " 服务创建失败 " );
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
void DelServices()
{
char name[ 100 ];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf( " 请输入要删除的服务名 " );
scanf ( " %s " , & name);
if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf( " OpenService error! " );
return ;
}
BOOL isSuccess = QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf( " QueryServiceStatus error! " );
return ;
}
if ( status.dwCurrentState != SERVICE_STOPPED )
{
isSuccess = ControlService(service,SERVICE_CONTROL_STOP, & status);
if ( ! isSuccess )
printf( " Stop Service error! " );
Sleep( 500 );
}
isSuccess = DeleteService(service);
if ( ! isSuccess)
printf( " 删除服务失败! " );
else
printf( " 删除服务成功! " );
CloseServiceHandle(service );
CloseServiceHandle(scm);
}
void StopServices()
{
char name[ 100 ];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf( " 请输入要停止的服务名 " );
scanf ( " %s " , & name);
if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf( " OpenService error! " );
return ;
}
BOOL isSuccess = QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf( " QueryServiceStatus error! " );
return ;
}
if ( status.dwCurrentState != SERVICE_STOPPED )
{
isSuccess = ControlService(service,SERVICE_CONTROL_STOP, & status);
if ( ! isSuccess )
printf( " 服务停止失败! " );
else
printf( " 服务停止成功! " );
Sleep( 500 );
} else
{
printf( " 此服务没有运行! " );
}
}
void StartServices()
{
char name[ 100 ];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf( " 请输入要启动的服务名 " );
scanf ( " %s " , & name);
if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf( " OpenService error! " );
return ;
}
BOOL isSuccess = QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf( " QueryServiceStatus error! " );
return ;
}
if ( status.dwCurrentState == SERVICE_STOPPED )
{
isSuccess = StartService(service, 0 ,NULL);
if ( ! isSuccess )
printf( " 服务启动失败! " );
else
printf( " 服务启动成功! " );
} else
{
printf( " 此服务正在运行! " );
}
}
// 服务管理器-命令行版 //
// 日期:2007/06/08 //
// 作者:冷风 //
// 文件:Server.cpp //
// 信箱:xo0888@Tom.com //
// QQ: 121121606 //
// 说明:若有问题请访问 Blog.csdn.net/chinafe 偶可以提供免费技术支持 呵呵 //
// /
#include < stdio.h >
#include < windows.h >
#include < Winsvc.h >
BOOL DisplayServices();
void AddServices();
void DelServices();
void StopServices();
void StartServices();
main()
{
int i = 10 ;
while (i != 0 )
{
printf( " **************************************************** " );
printf( " 服务管理器-命令行版 " );
printf( " 作者:冷风 2007.6.8 " );
printf( " **************************************************** " );
printf( " 查看服务请输入1 " );
printf( " 添加服务请输入2 " );
printf( " 删除服务请输入3 " );
printf( " 停止服务请输入4 " );
printf( " 启动服务请输入5 " );
printf( " 退出程序请输入0 " );
scanf ( " %d " , & i);
if ( 1 == i)
{
DisplayServices();
}
if ( 2 == i)
{
AddServices();
}
if ( 3 == i)
{
DelServices();
}
if ( 4 == i)
{
StopServices();
}
if ( 5 == i)
{
StartServices();
}
}
return 0 ;
}
BOOL DisplayServices()
{
LPENUM_SERVICE_STATUS lpServices = NULL;
DWORD nSize = 0 ;
DWORD nServicesReturned;
DWORD nResumeHandle = 0 ;
DWORD dwServiceType = SERVICE_WIN32;
SC_HANDLE schSCManager = NULL;
BOOL Flag = FALSE;
DWORD i = 0 ;
UINT j = 0 ;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) // Fail To Open SCM
{
printf( " Fail To Open SCM " );
return FALSE;
}
lpServices = (LPENUM_SERVICE_STATUS) LocalAlloc(LPTR, 64 * 1024 ); // Allocate Ram
if (lpServices == NULL) // Fail To Allocate Ram
{
printf( " Fail To Allocate Ram " );
goto CleanUP;
}
// Enum All Service Based On Service Type
if (EnumServicesStatus(schSCManager,
dwServiceType,
SERVICE_STATE_ALL,
(LPENUM_SERVICE_STATUS)lpServices,
64 * 1024 ,
& nSize,
& nServicesReturned,
& nResumeHandle) == NULL)
{
printf( " Fail To Enum Service " );
goto CleanUP;
}
// Display The Services
printf( " %-34s%s " , " ServiceName " , " DisplayName " );
for (i = 0 ; i < nServicesReturned; i ++ )
{
printf( " %d:%-32s%s " , ++ j,lpServices[i].lpServiceName, lpServices[i].lpDisplayName);
}
Flag = TRUE;
// Close Service Handle,Free Allocated Ram And Return To The Caller
CleanUP:
CloseServiceHandle(schSCManager);
if (lpServices != NULL)
{
LocalFree(lpServices);
}
getchar ();
return Flag;
}
void AddServices()
{
char name[ 100 ];
char info[ 200 ];
char path[ 300 ];
printf( " 请输入服务名 " );
scanf ( " %s " , & name);
printf( " 请输入服务描述 " );
scanf ( " %s " , & info);
printf( " 请输入程序路径 " );
scanf ( " %s " , & path);
SC_HANDLE manager = NULL;
SC_HANDLE service = NULL;
if ((manager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = CreateService(
manager,name,info,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, 0 , 0 , 0 , 0 , 0 );
if (service)
printf( " 服务创建成功 " );
else
printf( " 服务创建失败 " );
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
void DelServices()
{
char name[ 100 ];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf( " 请输入要删除的服务名 " );
scanf ( " %s " , & name);
if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf( " OpenService error! " );
return ;
}
BOOL isSuccess = QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf( " QueryServiceStatus error! " );
return ;
}
if ( status.dwCurrentState != SERVICE_STOPPED )
{
isSuccess = ControlService(service,SERVICE_CONTROL_STOP, & status);
if ( ! isSuccess )
printf( " Stop Service error! " );
Sleep( 500 );
}
isSuccess = DeleteService(service);
if ( ! isSuccess)
printf( " 删除服务失败! " );
else
printf( " 删除服务成功! " );
CloseServiceHandle(service );
CloseServiceHandle(scm);
}
void StopServices()
{
char name[ 100 ];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf( " 请输入要停止的服务名 " );
scanf ( " %s " , & name);
if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf( " OpenService error! " );
return ;
}
BOOL isSuccess = QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf( " QueryServiceStatus error! " );
return ;
}
if ( status.dwCurrentState != SERVICE_STOPPED )
{
isSuccess = ControlService(service,SERVICE_CONTROL_STOP, & status);
if ( ! isSuccess )
printf( " 服务停止失败! " );
else
printf( " 服务停止成功! " );
Sleep( 500 );
} else
{
printf( " 此服务没有运行! " );
}
}
void StartServices()
{
char name[ 100 ];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf( " 请输入要启动的服务名 " );
scanf ( " %s " , & name);
if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf( " OpenSCManager Error " );
}
service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf( " OpenService error! " );
return ;
}
BOOL isSuccess = QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf( " QueryServiceStatus error! " );
return ;
}
if ( status.dwCurrentState == SERVICE_STOPPED )
{
isSuccess = StartService(service, 0 ,NULL);
if ( ! isSuccess )
printf( " 服务启动失败! " );
else
printf( " 服务启动成功! " );
} else
{
printf( " 此服务正在运行! " );
}
}