Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following code to detect whether the service is running on the machine or not, it needs to infinitely, so i have used infinite loop here, but the problem is the exe is taking more memory and it keeps on increasing for every second, How to solve this.
ENUM_SERVICE_STATUS EnService[512];
		SC_HANDLE ScManager,ScService; //Handle of service manager
		DWORD cbBufSize=512*sizeof(ENUM_SERVICE_STATUS); 
		DWORD lpServicesReturned; 
		DWORD pcbBytesNeeded; 
		DWORD lpResumeHandle=0; 
		 
		//For querying service info
		DWORD dwBytesNeeded;
		//LPQUERY_SERVICE_CONFIG lpqscBuf; 
		 
		//Open the service manager with all as desired access
		ScManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 
		 
		char szStatus[255];
		char szStartType[255];
		 
		if(ScManager==NULL) {
		printf("Error querying the service manager");
		return 0; 
		}
		 
		if(::EnumServicesStatus(
		ScManager,//Service manager Handle
		SERVICE_WIN32, //Service type To be retrieved
		SERVICE_STATE_ALL, //Retrive all windows services
		EnService, //Our buffer 
		cbBufSize, //Buffer size
		&pcbBytesNeeded, //number of bytes needed
		&lpServicesReturned, //number of services returned
		&lpResumeHandle //Resume handle (if any)
		)==0){
		printf("Error querying the service manager");
		return 0; 
		}
		 
		
		if((ScService=::OpenService(ScManager,_T("MYSQL"),SERVICE_ALL_ACCESS))==NULL)
		{
		printf("Error opening service");
		}
		 
		//Allocate some memory in our buffer for the actual querying
		/*lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096); 
		if (lpqscBuf == NULL) 
		{
		printf("Error allocating service query");
		sprintf(szStartType,"Unknown");
		}
		 
		//Retrieve the configuration info
		if (! QueryServiceConfig( 
		ScService, 
		lpqscBuf, 
		4096, 
		&dwBytesNeeded) ) 
		{
		printf("Error querying service info");
		sprintf(szStartType,"Unknown");
		}*/
Posted

1 solution

use the function CloseServiceHandle to close the ScManager at the end.
also i hope u put some sleep() in the infinite loop.
 
Share this answer
 
Comments
Gokulnath007 3-Jun-11 8:11am    
Thank You so much Resmi... That worked fine...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900