資源簡介
用C++編寫的,利用調用操作系統提供的API獲取操作系統的服務詳細信息。

代碼片段和文件信息
//?GetSysService.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“StdAfx.h“
#include?
#include?
#include
void?main()
{
printf(“輸入1查看系統服務狀況!\n“);
int?menu?=?0;
while?(cin?>>?menu)
{
if(menu?==?1)
{
SC_HANDLE?SCMan?=?OpenSCManager(NULL?NULL?SC_MANAGER_ALL_ACCESS);?//打開系統服務控制器
if(SCMan?==?NULL)
{
printf(“OpenSCManager?Eorror\n“);
return;
}
LPENUM_SERVICE_STATUS?service_status;?//保存系統服務的結構
DWORD?cbBytesNeeded?=?NULL;?
DWORD?ServicesReturned?=?NULL;
DWORD?ResumeHandle?=?NULL;
service_status?=?(LPENUM_SERVICE_STATUS)LocalAlloc(LPTR?1024?*?64);//分配內存,注意大小
//獲取系統服務的簡單信息
bool?ESS?=?EnumServicesStatus(SCMan?//系統服務句柄
SERVICE_WIN32?//服務的類型
SERVICE_STATE_ALL??//服務的狀態
(LPENUM_SERVICE_STATUS)service_status??//輸出參數,系統服務的結構
1024?*?64??//?結構的大小
&cbBytesNeeded?//輸出參數,接收返回所需的服務
&ServicesReturned?//輸出參數,接收返回服務的數量
&ResumeHandle);?//輸入輸出參數,第一次調用必須為0,返回為0代表成功
if(ESS?==?NULL)?
{
printf(“EnumServicesStatus?Eorror\n“);
return;
}
printf(“當前服務數量為:%d\n“ServicesReturned);
for(int?i?=?0;?i? {
printf(“服務名:?%s?“?service_status[i].lpDisplayName);
switch(service_status[i].ServiceStatus.dwCurrentState)//?服務狀態
{
case?SERVICE_CONTINUE_PENDING:
printf(“狀態:?掛起“);
break;
case?SERVICE_PAUSE_PENDING:
printf(“狀態:?正在暫停?“);
break;
case?SERVICE_PAUSED:
printf(“狀態:?已暫停?“);
break;
case?SERVICE_RUNNING:
printf(“狀態:?運行?“);
break;
case?SERVICE_START_PENDING:
printf(“狀態:?啟動?“);
break;
case?SERVICE_STOPPED:
printf(“狀態:?停止?“);
break;
default:
printf(“狀態:?未知?“);
break;
}
LPQUERY_SERVICE_CONFIG?lpServiceConfig?=?NULL;?//服務詳細信息結構
SC_HANDLE?service_curren?=?NULL;?//當前的服務句柄
service_curren?=?OpenService(SCMan?service_status[i].lpServiceName?SERVICE_QUERY_CONFIG);?//打開當前服務
lpServiceConfig?=?(LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR?8?*?1024);??????//分配內存,?最大為8kb?
if(NULL?==?QueryServiceConfig(service_curren?lpServiceConfig?8?*?1024?&ResumeHandle))
{
printf(“QueryServiceConfig?Error\n“);
return;
}
printf(“路徑:?%s?“?lpServiceConfig->lpBinaryPathName);//服務的路徑
printf(“\n“);
CloseServiceHandle(service_curren);//關閉當前服務的句柄
}
CloseServiceHandle(SCMan);//關閉服務管理器的句柄
}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????213049??2012-04-01?14:36??GetSysService\Debug\GetSysService.exe
?????文件?????260312??2012-04-01?14:36??GetSysService\Debug\GetSysService.ilk
?????文件??????66728??2012-04-01?14:36??GetSysService\Debug\GetSysService.obj
?????文件?????187144??2012-04-01?14:11??GetSysService\Debug\GetSysService.pch
?????文件?????541696??2012-04-01?14:36??GetSysService\Debug\GetSysService.pdb
?????文件???????1764??2012-04-01?14:11??GetSysService\Debug\StdAfx.obj
?????文件?????181248??2012-04-01?14:36??GetSysService\Debug\vc60.idb
?????文件?????307200??2012-04-01?14:36??GetSysService\Debug\vc60.pdb
?????文件???????2745??2012-04-01?14:35??GetSysService\GetSysService.cpp
?????文件???????4620??2012-04-01?14:11??GetSysService\GetSysService.dsp
?????文件????????532??2012-04-01?14:11??GetSysService\GetSysService.dsw
?????文件??????41984??2012-04-01?15:02??GetSysService\GetSysService.ncb
?????文件??????48640??2012-04-01?15:02??GetSysService\GetSysService.opt
?????文件???????1560??2012-04-01?14:36??GetSysService\GetSysService.plg
?????文件???????1250??2012-04-01?14:11??GetSysService\ReadMe.txt
?????文件????????300??2012-04-01?14:11??GetSysService\StdAfx.cpp
?????文件????????667??2012-04-01?14:11??GetSysService\StdAfx.h
?????目錄??????????0??2012-04-01?14:36??GetSysService\Debug
?????目錄??????????0??2012-04-01?15:02??GetSysService
-----------?---------??----------?-----??----
??????????????1861439????????????????????19
評論
共有 條評論