服务管理CMD版(VC源代码)

服务管理 CMD版
功能如下:
查看服务
添加服务
删除服务
停止服务
启动服务
程序很简单运行效果如图所示
为了方便,我把源代码一块打包了,需要的话可以自己修改,有问题可以一块讨论……呵呵
完整代码如下:
// /
// 服务管理器-命令行版 //
// 日期:2007/06/08 //
// 作者:冷风 //
// 文件:Server.cpp //
// 信箱:xo0888@Tom.com //
// QQ:121121606 //
// 说明:若有问题请访问Blog.youkuaiyun.com/chinafe偶可以提供免费技术支持呵呵 //
// /
#include < stdio.h >
#include
< windows.h >
#include
< Winsvc.h >

BOOLDisplayServices();
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 ;
}


BOOLDisplayServices()
{

LPENUM_SERVICE_STATUSlpServices
= NULL;
DWORDnSize
= 0 ;
DWORDnServicesReturned;
DWORDnResumeHandle
= 0 ;
DWORDdwServiceType
= SERVICE_WIN32;

SC_HANDLEschSCManager
= NULL;
BOOLFlag
= FALSE;
DWORDi
= 0 ;
UINTj
= 0 ;

schSCManager
= OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);


if (schSCManager == NULL) // FailToOpenSCM
{
printf(
" FailToOpenSCM " );
return FALSE;
}

lpServices
= (LPENUM_SERVICE_STATUS)LocalAlloc(LPTR, 64 * 1024 ); // AllocateRam

if (lpServices == NULL) // FailToAllocateRam
{
printf(
" FailToAllocateRam " );
goto CleanUP;
}

// EnumAllServiceBasedOnServiceType
if (EnumServicesStatus(schSCManager,
dwServiceType,
SERVICE_STATE_ALL,
(LPENUM_SERVICE_STATUS)lpServices,
64 * 1024 ,
& nSize,
& nServicesReturned,
& nResumeHandle) == NULL)




{
printf(
" FailToEnumService " );
goto CleanUP;
}

// DisplayTheServices
printf( " %-34s%s " , " ServiceName " , " DisplayName " );
for (i = 0 ;i < nServicesReturned;i ++ )
{

printf(
" %d:%-32s%s " , ++ j,lpServices[i].lpServiceName,lpServices[i].lpDisplayName);

}
Flag
= TRUE;

// CloseServiceHandle,FreeAllocatedRamAndReturnToTheCaller
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_HANDLEmanager
= NULL;
SC_HANDLEservice
= NULL;

if ((manager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf(
" OpenSCManagerError " );
}
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_HANDLEscm;
SC_HANDLEservice;
SERVICE_STATUSstatus;

printf(
" 请输入要删除的服务名 " );
scanf(
" %s " , & name);

if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf(
" OpenSCManagerError " );
}
service
= OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf(
" OpenServiceerror! " );
return ;
}
BOOLisSuccess
= QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf(
" QueryServiceStatuserror! " );
return ;
}

if (status.dwCurrentState != SERVICE_STOPPED)
{

isSuccess
= ControlService(service,SERVICE_CONTROL_STOP, & status);
if ( ! isSuccess)
printf(
" StopServiceerror! " );
Sleep(
500 );

}

isSuccess
= DeleteService(service);
if ( ! isSuccess)
printf(
" 删除服务失败! " );
else
printf(
" 删除服务成功! " );

CloseServiceHandle(service);
CloseServiceHandle(scm);
}


void StopServices()
{
char name[ 100 ];
SC_HANDLEscm;
SC_HANDLEservice;
SERVICE_STATUSstatus;

printf(
" 请输入要停止的服务名 " );
scanf(
" %s " , & name);

if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf(
" OpenSCManagerError " );
}
service
= OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf(
" OpenServiceerror! " );
return ;
}
BOOLisSuccess
= QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf(
" QueryServiceStatuserror! " );
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_HANDLEscm;
SC_HANDLEservice;
SERVICE_STATUSstatus;

printf(
" 请输入要启动的服务名 " );
scanf(
" %s " , & name);

if ((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf(
" OpenSCManagerError " );
}
service
= OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE);
if ( ! service)
{
printf(
" OpenServiceerror! " );
return ;
}
BOOLisSuccess
= QueryServiceStatus(service, & status);
if ( ! isSuccess)
{
printf(
" QueryServiceStatuserror! " );
return ;
}
if (status.dwCurrentState == SERVICE_STOPPED)
{
isSuccess
= StartService(service, 0 ,NULL);
if ( ! isSuccess)
printf(
" 服务启动失败! " );
else
printf(
" 服务启动成功! " );

}
else
{
printf(
" 此服务正在运行! " );
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值