分享
 
 
 

木马编程天天练 进入第3天 服务管理

王朝学院·作者佚名  2009-12-19
窄屏简体版  字體: |||超大  

服务函数

下面的函数用于被服务执行或者使用

函数 描述

Handler An application-defined callback function used with the RegisterServiceCtrlHandler function.

HandlerEx

RegisterServiceCtrlHandler 注册一个函数处理控制码请求。

RegisterServiceCtrlHandlerEx

ServiceMain 服务程序入口函数。

SetServiceBits Registers a service type with the service control manager and the Server service.

SetServiceStatus Updates the service control manager's status information for the calling service.

StartServiceCtrlDispatcher Connects the main thread of a service process to the service control manager.

下面的函数被用于管理和配置服务

函数 描述

ChangeServiceConfig 改变服务的开机运行状态。

ChangeServiceConfig2 改变服务的描述。

CloseServiceHandle 关闭服务句柄。

ControlService 在一个服务已经被开启的情况下,向这个服务发出控制码。

ControlServiceEx

CreateService 创建一个服务对象,并增加它到服务控制管理数据库。

DeleteService 在服务控制管理数据库中标示要删除的服务。

EnumDependentServices 获取服务管理数据库中所有服务的名称和当前状态。

EnumServicesStatusEx

GetServiceDisplayName 获取服务的描述。

GetServiceKeyName Retrieves the service name of the specified service.

NotifyBootConfigStatus Reports the boot status to the service control manager.

NotifyServiceStatusChange Enables an application to receive notification when the specified service is created or

deleted or when its status changes.

OpenSCManager 和指定机器的服务控制管理器建立连接并打开服务控制管理器数据库。

OpenService 打开一个存在的服务。

QueryServiceConfig

QueryServiceConfig2

QueryServiceObjectSecurity Retrieves a copy of the security descriptor associated with a service object.

QueryServiceStatusEx 查询服务程序现在的运行状态。

SetServiceObjectSecurity Sets the security descriptor of a service object.

StartService 开启一个服务。

废弃函数

下面的函数已经被废弃。

EnumServicesStatus

LockServiceDatabase

QueryServiceLockStatus

QueryServiceStatus

UnlockServiceDatabase

Build date: 12/3/2009

程序例子:

#include<windows.h>

#include<stdio.h>

bool Start_Service(wchar_t * ServiceName);

bool Stop_Service(wchar_t * ServiceName);

bool Create_Service(wchar_t * ServiceName);

bool Delete_Service(wchar_t * ServiceName);

void ReconfigureService(wchar_t * ServiceName, wchar_t * ServiceDes);

void ChangeServiceRun(wchar_t * ServiceName);

void EnumService(void);

int main()

{

wchar_t * ServiceDisp = L"快速缓存服务,为网络文件交换提供缓存,提高网络连接速度。";

//Start_Service(L"WmdmPmSN");

//Stop_Service(L"WmdmPmSN");

//Create_Service(L"ServiceTest");

//Delete_Service(L"ServiceTest");

//ReconfigureService(L"ServiceTest",ServiceDisp);

ChangeServiceRun(L"WmdmPmSN");

EnumService();

return 0;

}

bool Start_Service(wchar_t * ServiceName)

{

SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if(NULL != schSCManager)

{

// L"WmdmPmSN"

SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);

if( NULL != schService)

{

if(StartService(schService,0,NULL))

{

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

return 1;

}

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

wprintf(L"Start Service failed!\n");

return 0;

}

CloseServiceHandle(schSCManager);

wprintf(L"Open Service failed!\n");

return 0;

}

wprintf(L"OpenSCManager failed!\n");

CloseServiceHandle(schSCManager);

return 0;

}

bool Stop_Service(wchar_t * ServiceName)

{

SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if(NULL != schSCManager)

{

// L"WmdmPmSN"

SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);

if( NULL != schService)

{

SERVICE_STATUS ServiceStatus;

if(ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus))

{

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

return 1;

}

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

wprintf(L"Start Service failed!\n");

return 0;

}

CloseServiceHandle(schSCManager);

wprintf(L"Open Service failed!\n");

return 0;

}

wprintf(L"OpenSCManager failed!\n");

CloseServiceHandle(schSCManager);

return 0;

}

bool Create_Service(wchar_t * ServiceName)

{

SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if(NULL != schSCManager)

{

wchar_t * DisplayName = L"Service Program Test\n";

wchar_t * FilePathName = L"d:\\cyuyan\\servicetest.exe";

SC_HANDLE schService = CreateService(

schSCManager,

ServiceName,

DisplayName,

SC_MANAGER_ALL_ACCESS,

SERVICE_WIN32_OWN_PROCESS,

SERVICE_AUTO_START,

SERVICE_ERROR_IGNORE,

FilePathName,

NULL,

NULL,

NULL,

NULL,

NULL);

if(schService != NULL)

{

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

return 1;

}

else

{

CloseServiceHandle(schSCManager);

return 0;

}

}

else

return 0;

}

bool Delete_Service(wchar_t * ServiceName)

{

SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if(NULL != schSCManager)

{

// L"WmdmPmSN"

SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);

if( NULL != schService)

{

SERVICE_STATUS ServiceStatus;

QueryServiceStatus(schService,&ServiceStatus);

if(ServiceStatus.dwCurrentState != SERVICE_STOPPED)

{

ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus);

}

DeleteService(schService);

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

return 1;

}

else

wprintf(L"Open Service failed!\n");

return 0;

}

else

{

wprintf(L"OpenSCManager failed!\n");

CloseServiceHandle(schSCManager);

return 0;

}

}

void ReconfigureService(wchar_t * ServiceName, wchar_t * ServiceDisp)

{

SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

if (schSCManager != NULL)

{

// Need to acquire database lock before reconfiguring.

SC_LOCK sclLock = LockServiceDatabase(schSCManager);

if (sclLock != NULL)

{

// Open a handle to the service.

SC_HANDLE schService = OpenService(

schSCManager, // SCManager database

ServiceName, // name of service

SERVICE_CHANGE_CONFIG); // need CHANGE access

if (schService != NULL)

{

SERVICE_DESCRIPTION sdBuf;

sdBuf.lpDescription = ServiceDisp;

if (ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &sdBuf))

{

MessageBox(NULL,L"Change SUCCESS",L" ",MB_OK);

}

CloseServiceHandle(schService);

}

UnlockServiceDatabase(sclLock);

}

CloseServiceHandle(schSCManager);

}

}

void ChangeServiceRun(wchar_t * ServiceName)

{

SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if(NULL != schSCManager)

{

// L"WmdmPmSN"

SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);

if( NULL != schService)

{

if(ChangeServiceConfig(

schService,

SERVICE_NO_CHANGE,

SERVICE_AUTO_START,

SERVICE_NO_CHANGE,

NULL,

NULL,

NULL,

NULL,

NULL,

NULL,

NULL))

{

wprintf(L"Change Service done!\n");

return;

}

CloseServiceHandle(schService);

CloseServiceHandle(schSCManager);

}

wprintf(L"Open Service failed!\n");

}

wprintf(L"OpenSCManager failed!\n");

CloseServiceHandle(schSCManager);

}

void EnumService(void)

{

LPENUM_SERVICE_STATUS st;

st=(LPENUM_SERVICE_STATUS)LocalAlloc(LPTR,64*1024);

DWORD ret=0;

DWORD size=0;

SC_HANDLE sc=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

EnumServicesStatus(sc,SERVICE_WIN32,SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)st,1024*64,&size,&ret,NULL);

for(int i=0;i<ret;i++){

wprintf(L"%-20s%-50s",st[i].lpServiceName,st[i].lpDisplayName);

switch(st[i].ServiceStatus.dwCurrentState){

case(SERVICE_RUNNING):

wprintf(L"running\n");

break;

case(SERVICE_STOPPED):

wprintf(L"stopped\n");

break;

}

}

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/armor51/archive/2009/12/17/5027209.aspx

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有