分享
 
 
 

枚举NT services

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

枚举NT services

闻怡洋·vchelp

下面的文章提供了访问NT中所有Service的功能,每次列举Services时,函数会返回一个列表。 列表的内容依赖于你所使用的参数。 (我认为这是一种很巧妙的编程方法,它极大的减轻了数据和函数的冗余,利用一个STATIC函数来产生本身对象的列表或者是来产生对象)

Class declaration:声明

class TTrixServiceInfo {

public:

CString ServiceName;

CString DisplayName;

CString BinaryPath;

DWORD ServiceType;

DWORD StartType;

DWORD ErrorControl;

DWORD CurrentState;

public:

TTrixServiceInfo();

TTrixServiceInfo& operator=(const TTrixServiceInfo& source);

CString GetServiceType(void);

CString GetStartType(void);

CString GetErrorControl(void);

CString GetCurrentState(void);

static TTrixServiceInfo *EnumServices(DWORD serviceType,

DWORD serviceState,DWORD *count);

};

Description:类的每一个实例都包含了SERVICE的各种信息,如果想得到SERVICE的列表,请调用TTrixServiceInfo::EnumServices(...)。

参数ServiceType的取值可能是:SERVICE_WIN32 and SERVICE_DRIVER.

参数ServiceState的取值可能是:SERVICE_ACTIVE and SERVICE_INACTIVE.

EnumServices(...)将返回TTrixServiceInfo对象的列表,(如果出错返回NULL)。列表中对象的个数可以通过参数返回时得到。

下面是具体的代码:

TTrixServiceInfo *lpservice = NULL;

DWORD count;

lpservice = TTrixServiceInfo::EnumServices(SERVICE_WIN32,SERVICE_ACTIVE|SERVICE_INACTIVE,&count/*得到个数*/);

if (lpservice) {//如果正确

for (DWORD index = 0; index < count; index ++) {

printf("%d. %s, %s\n", index, lpservice[index].DisplayName,

lpservice[index].GetCurrentState());

}

delete [] lpservice;

}

Source code:

TTrixServiceInfo::TTrixServiceInfo()

{

ServiceName.Empty();

DisplayName.Empty();

BinaryPath.Empty();

ServiceType = 0;

StartType = 0;

ErrorControl = 0;

CurrentState = 0;

}

TTrixServiceInfo& TTrixServiceInfo::operator=(const TTrixServiceInfo& source)

{

ServiceName = source.ServiceName;

DisplayName = source.DisplayName;

BinaryPath = source.BinaryPath;

ServiceType = source.ServiceType;

StartType = source.StartType;

ErrorControl = source.ErrorControl;

CurrentState = source.CurrentState;

return *this;

}

CString TTrixServiceInfo::GetServiceType(void)

{

// Winnt.h

CString str = "UNKNOWN";

if (ServiceType & SERVICE_WIN32) {

if (ServiceType &

SERVICE_WIN32_OWN_PROCESS)

str = "WIN32_OWN_PROCESS";

else if (ServiceType &

SERVICE_WIN32_SHARE_PROCESS)

str = "WIN32_SHARE_PROCESS";

if (ServiceType &

SERVICE_INTERACTIVE_PROCESS)

str += "(INTERACTIVE_PROCESS)";

}

switch (ServiceType) {

case SERVICE_KERNEL_DRIVER:

str = "KERNEL_DRIVER"; break;

case SERVICE_FILE_SYSTEM_DRIVER:

str = "FILE_SYSTEM_DRIVER";

break;

};

return str;

}

CString TTrixServiceInfo::GetStartType(void)

{

// Winnt.h

TCHAR *types[] = {

"BOOT_START", // 0

"SYSTEM_START", // 1

"AUTO_START", // 2

"DEMAND_START", // 3

"DISABLED" // 4

};

return CString(types[StartType]);

}

CString TTrixServiceInfo::GetErrorControl(void)

{

// Winnt.h

TCHAR *types[] = {

"ERROR_IGNORE", // 0

"ERROR_NORMAL", // 1

"ERROR_SEVERE", // 2

"ERROR_CRITICAL" // 3

};

return CString(types[ErrorControl]);

}

CString TTrixServiceInfo::GetCurrentState(void)

{

// Winsvc.h

TCHAR *types[] = {

"UNKNOWN",

"STOPPED", // 1

"START_PENDING", // 2

"STOP_PENDING", // 3

"RUNNING", // 4

"CONTINUE_PENDING", // 5

"PAUSE_PENDING", // 6

"PAUSED" // 7

};

return CString(types[CurrentState]);

}

// ServiceType = bit OR of SERVICE_WIN32, SERVICE_DRIVER

// ServiceState = bit OR of SERVICE_ACTIVE, SERVICE_INACTIVE

TTrixServiceInfo *TTrixServiceInfo::EnumServices(DWORD serviceType,DWORD

serviceState,DWORD *count)

{

// Maybe check if serviceType and serviceState have at least one constant specified

*count = 0;

TTrixServiceInfo *info = NULL;

SC_HANDLE scman = ::OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE);

if (scman) {

ENUM_SERVICE_STATUS service, *lpservice;

BOOL rc;

DWORD bytesNeeded,servicesReturned,resumeHandle = 0;

rc = ::EnumServicesStatus(scman,serviceType,serviceState,&service,sizeof(service),

&bytesNeeded,&servicesReturned,&resumeHandle);

if ((rc == FALSE) && (::GetLastError() == ERROR_MORE_DATA)) {

DWORD bytes = bytesNeeded + sizeof(ENUM_SERVICE_STATUS);

lpservice = new ENUM_SERVICE_STATUS [bytes];

::EnumServicesStatus(scman,serviceType,serviceState,lpservice,bytes,

&bytesNeeded,&servicesReturned,&resumeHandle);

*count = servicesReturned; // Not a chance that 0 services is returned

info = new TTrixServiceInfo [servicesReturned];

TCHAR Buffer[1024];

// Should be enough for service info

QUERY_SERVICE_CONFIG *lpqch = (QUERY_SERVICE_CONFIG*)Buffer;

for (DWORD ndx = 0; ndx < servicesReturned; ndx++) {

info[ndx].ServiceName = lpservice[ndx].lpServiceName;

info[ndx].DisplayName = lpservice[ndx].lpDisplayName;

info[ndx].ServiceType = lpservice[ndx].ServiceStatus.dwServiceType;

info[ndx].CurrentState = lpservice[ndx].ServiceStatus.dwCurrentState;

SC_HANDLE sh = ::OpenService(scman,lpservice[ndx].lpServiceName,SERVICE_QUERY_CONFIG);

if (::QueryServiceConfig(sh,lpqch,sizeof(Buffer),&bytesNeeded)) {

info[ndx].BinaryPath = lpqch->lpBinaryPathName;

info[ndx].StartType = lpqch->dwStartType;

info[ndx].ErrorControl = lpqch->dwErrorControl;

}

::CloseServiceHandle(sh);

}

delete [] lpservice;

}

::CloseServiceHandle(scman);

}

return info;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有