GetService.h
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:5k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include  <winsvc.h>
  3. /*
  4.  作者:海啸 lyyer English Name: Jack 
  5.   blog:http://lyyer.blog.sohu.com
  6.   website:http://www.cnGSG.com
  7.   海啸网络安全组织
  8. */
  9. typedef struct tagServiceInfo{
  10. //DWORD num;
  11. char  ServiceName[32];
  12. char  ServiceDetail[128];
  13. char  ServiceState[16];
  14.     char  ServiceStartType[16];
  15. }SERVICEINFO,*LPSERVICEINFO;
  16. BOOL ListService(std::vector<SERVICEINFO *> *service_vector)
  17. {
  18. SERVICEINFO *service_tmp = new SERVICEINFO ;
  19. memset(service_tmp,0,sizeof(SERVICEINFO));
  20.     SC_HANDLE ScManager;
  21. ENUM_SERVICE_STATUS EnService[512];
  22. DWORD   cbBufSize=512*sizeof(ENUM_SERVICE_STATUS);
  23. // size of status buffer
  24. DWORD lpServicesReturned;
  25. DWORD   pcbBytesNeeded;           // buffer size needed
  26. DWORD   lpResumeHandle=0;           // next entry
  27. CString str,csTemp;
  28. service_vector->clear();
  29. ScManager=::OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE|GENERIC_READ);
  30. if(ScManager==NULL)
  31. {
  32. AfxMessageBox("Error Opening Service Mgr");
  33. return FALSE;
  34. }
  35. if(::EnumServicesStatus(ScManager,SERVICE_WIN32,SERVICE_STATE_ALL,EnService,cbBufSize,&pcbBytesNeeded,&lpServicesReturned,&lpResumeHandle )==0)
  36. {
  37. AfxMessageBox("Error Querrying Service Mgr");
  38. return FALSE;
  39. }
  40. for(int i=0;i<int(lpServicesReturned);i++)
  41. {
  42. SERVICEINFO *service_tmp = new SERVICEINFO ;
  43.         memset(service_tmp,0,sizeof(SERVICEINFO));
  44. //csTemp.Format("%d",i+1);
  45. //m_list.InsertItem(i,csTemp);
  46. strcpy(service_tmp->ServiceName,EnService[i].lpServiceName);
  47. strcpy(service_tmp->ServiceDetail,EnService[i].lpDisplayName);
  48. //m_list.SetItemText(i,1,EnService[i].lpServiceName);
  49. //m_list.SetItemText(i,2,EnService[i].lpDisplayName);
  50. switch(EnService[i].ServiceStatus.dwCurrentState)
  51. {
  52. case SERVICE_PAUSED:
  53. //m_list.SetItemText(i,3,"paused"); 
  54. strcpy(service_tmp->ServiceState,"暂停");
  55. break; // The service is paused. 
  56. case SERVICE_RUNNING:
  57. //m_list.SetItemText(i,3,"running");
  58. strcpy(service_tmp->ServiceState,"已启动");          
  59. break;// The service is running. 
  60. case SERVICE_STOPPED :
  61. //m_list.SetItemText(i,3,"stopped");          
  62. strcpy(service_tmp->ServiceState,"停止");  
  63. break;// The service is stopped
  64.   case SERVICE_START_PENDING :
  65.   //m_list.SetItemText(i,3,"starting");
  66.   strcpy(service_tmp->ServiceState,"正在启动");  
  67.   break;// The service is starting
  68.   
  69.   case SERVICE_STOP_PENDING :
  70.   //m_list.SetItemText(i,3,"stopping");
  71.   strcpy(service_tmp->ServiceState,"正在停止");                                                                      
  72.   break;// The service is stopped
  73.   
  74.   default:
  75.   strcpy(service_tmp->ServiceState,"未知"); 
  76.   //m_list.SetItemText(i,3,"unknown");       strcpy(service_tmp->ServiceState,"unknown");    
  77.   break;// The service is running.
  78. }
  79. //block for start type
  80. {
  81. SC_HANDLE ScService;
  82. QUERY_SERVICE_CONFIG ServiceConfig;
  83. DWORD CbBufSize, pcbBytesNeeded;
  84. CbBufSize=sizeof(QUERY_SERVICE_CONFIG);
  85. //CString 
  86. //char *SerState ;;
  87. if((ScService=::OpenService(ScManager,EnService[i].lpServiceName,SERVICE_ALL_ACCESS))==NULL)
  88. {
  89. ::CloseHandle(ScManager);
  90. "Problem in opening service";
  91. }
  92. ::QueryServiceConfig(ScService,&ServiceConfig,CbBufSize,&pcbBytesNeeded);
  93. switch(ServiceConfig.dwStartType)
  94. {
  95. case SERVICE_AUTO_START: strcpy(service_tmp->ServiceStartType,"自动"); break;                           
  96. case SERVICE_DEMAND_START:strcpy(service_tmp->ServiceStartType,"手动"); break;
  97. case SERVICE_DISABLED :strcpy(service_tmp->ServiceStartType,"禁用"); break;
  98. default:  strcpy(service_tmp->ServiceStartType,"未知");
  99.      
  100. }
  101. }/**/
  102. service_vector->push_back(service_tmp);
  103. }
  104. // for(int jj = 0;jj<service_vector.size();jj++)
  105. // {
  106. // AfxMessageBox(service_vector[jj]->ServiceName);
  107. // }
  108. return true;
  109. }
  110. BOOL KillService(char *chSvrid)
  111. {
  112.     char *strServiceName = chSvrid;
  113. SC_HANDLE ScManager,ScService;//Declaring the Some USeful Variable
  114. if((ScManager=::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
  115. {
  116. //preparing Error Variable
  117. //AfxMessageBox("Error Opening System Manager");
  118. return false;
  119.  
  120. }
  121. if((ScService=::OpenService(ScManager,strServiceName,SERVICE_ALL_ACCESS))==NULL)
  122. {
  123. //preparing Error Variable
  124. //AfxMessageBox("Error Opening Service");
  125. return false;
  126. }
  127. if(::DeleteService(ScService)==0)
  128. {
  129. //preparing Error Variable
  130. //AfxMessageBox("Error Deleting Service");
  131. return false;
  132. }
  133. else
  134. {
  135. //MessageBox("Service Marked For DeletionnDeleted After Computer Restart");
  136. return true;
  137. }    
  138. }