GetService.h
资源名称:Visualhk.rar [点击查看]
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:5k
源码类别:
网络截获/分析
开发平台:
Visual C++
- #include <windows.h>
- #include <winsvc.h>
- /*
- 作者:海啸 lyyer English Name: Jack
- blog:http://lyyer.blog.sohu.com
- website:http://www.cnGSG.com
- 海啸网络安全组织
- */
- typedef struct tagServiceInfo{
- //DWORD num;
- char ServiceName[32];
- char ServiceDetail[128];
- char ServiceState[16];
- char ServiceStartType[16];
- }SERVICEINFO,*LPSERVICEINFO;
- BOOL ListService(std::vector<SERVICEINFO *> *service_vector)
- {
- SERVICEINFO *service_tmp = new SERVICEINFO ;
- memset(service_tmp,0,sizeof(SERVICEINFO));
- SC_HANDLE ScManager;
- ENUM_SERVICE_STATUS EnService[512];
- DWORD cbBufSize=512*sizeof(ENUM_SERVICE_STATUS);
- // size of status buffer
- DWORD lpServicesReturned;
- DWORD pcbBytesNeeded; // buffer size needed
- DWORD lpResumeHandle=0; // next entry
- CString str,csTemp;
- service_vector->clear();
- ScManager=::OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE|GENERIC_READ);
- if(ScManager==NULL)
- {
- AfxMessageBox("Error Opening Service Mgr");
- return FALSE;
- }
- if(::EnumServicesStatus(ScManager,SERVICE_WIN32,SERVICE_STATE_ALL,EnService,cbBufSize,&pcbBytesNeeded,&lpServicesReturned,&lpResumeHandle )==0)
- {
- AfxMessageBox("Error Querrying Service Mgr");
- return FALSE;
- }
- for(int i=0;i<int(lpServicesReturned);i++)
- {
- SERVICEINFO *service_tmp = new SERVICEINFO ;
- memset(service_tmp,0,sizeof(SERVICEINFO));
- //csTemp.Format("%d",i+1);
- //m_list.InsertItem(i,csTemp);
- strcpy(service_tmp->ServiceName,EnService[i].lpServiceName);
- strcpy(service_tmp->ServiceDetail,EnService[i].lpDisplayName);
- //m_list.SetItemText(i,1,EnService[i].lpServiceName);
- //m_list.SetItemText(i,2,EnService[i].lpDisplayName);
- switch(EnService[i].ServiceStatus.dwCurrentState)
- {
- case SERVICE_PAUSED:
- //m_list.SetItemText(i,3,"paused");
- strcpy(service_tmp->ServiceState,"暂停");
- break; // The service is paused.
- case SERVICE_RUNNING:
- //m_list.SetItemText(i,3,"running");
- strcpy(service_tmp->ServiceState,"已启动");
- break;// The service is running.
- case SERVICE_STOPPED :
- //m_list.SetItemText(i,3,"stopped");
- strcpy(service_tmp->ServiceState,"停止");
- break;// The service is stopped
- case SERVICE_START_PENDING :
- //m_list.SetItemText(i,3,"starting");
- strcpy(service_tmp->ServiceState,"正在启动");
- break;// The service is starting
- case SERVICE_STOP_PENDING :
- //m_list.SetItemText(i,3,"stopping");
- strcpy(service_tmp->ServiceState,"正在停止");
- break;// The service is stopped
- default:
- strcpy(service_tmp->ServiceState,"未知");
- //m_list.SetItemText(i,3,"unknown"); strcpy(service_tmp->ServiceState,"unknown");
- break;// The service is running.
- }
- //block for start type
- {
- SC_HANDLE ScService;
- QUERY_SERVICE_CONFIG ServiceConfig;
- DWORD CbBufSize, pcbBytesNeeded;
- CbBufSize=sizeof(QUERY_SERVICE_CONFIG);
- //CString
- //char *SerState ;;
- if((ScService=::OpenService(ScManager,EnService[i].lpServiceName,SERVICE_ALL_ACCESS))==NULL)
- {
- ::CloseHandle(ScManager);
- "Problem in opening service";
- }
- ::QueryServiceConfig(ScService,&ServiceConfig,CbBufSize,&pcbBytesNeeded);
- switch(ServiceConfig.dwStartType)
- {
- case SERVICE_AUTO_START: strcpy(service_tmp->ServiceStartType,"自动"); break;
- case SERVICE_DEMAND_START:strcpy(service_tmp->ServiceStartType,"手动"); break;
- case SERVICE_DISABLED :strcpy(service_tmp->ServiceStartType,"禁用"); break;
- default: strcpy(service_tmp->ServiceStartType,"未知");
- }
- }/**/
- service_vector->push_back(service_tmp);
- }
- // for(int jj = 0;jj<service_vector.size();jj++)
- // {
- // AfxMessageBox(service_vector[jj]->ServiceName);
- // }
- return true;
- }
- BOOL KillService(char *chSvrid)
- {
- char *strServiceName = chSvrid;
- SC_HANDLE ScManager,ScService;//Declaring the Some USeful Variable
- if((ScManager=::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
- {
- //preparing Error Variable
- //AfxMessageBox("Error Opening System Manager");
- return false;
- }
- if((ScService=::OpenService(ScManager,strServiceName,SERVICE_ALL_ACCESS))==NULL)
- {
- //preparing Error Variable
- //AfxMessageBox("Error Opening Service");
- return false;
- }
- if(::DeleteService(ScService)==0)
- {
- //preparing Error Variable
- //AfxMessageBox("Error Deleting Service");
- return false;
- }
- else
- {
- //MessageBox("Service Marked For DeletionnDeleted After Computer Restart");
- return true;
- }
- }