Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   finding the status of service in windows (http://www.go4expert.com/forums/finding-status-service-windows-t17700/)

suresh_rtp 26May2009 10:54

finding the status of service in windows
 
hi,
this is the cod that i was written for getting the service status.i have some problem in this code.i am not able to getting the staus of the service.anybody have idea plaese help me.

Code:

DWORD ServiceGetStatus(char *sService)
{
    SC_HANDLE schm;
    SC_HANDLE schs;
      LPSERVICE_STATUS TServiceStatus = NULL;
    DWORD dwStat = -1;
    //dwStat = -1;
    // connect to the service control manager
    //schm = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT);
    schm = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT);
    if(schm > 0)
    {
        schs = OpenService(schm,sService,SERVICE_QUERY_STATUS);

        if(schs > 0)
        {
            if(QueryServiceStatus(schs,TServiceStatus))
            {
                dwStat = TServiceStatus->dwCurrentState;
                CloseServiceHandle(schs);
                CloseServiceHandle(schm);
            }
            std::cout<<"status is false"<<std::endl;
        }
    }
    return dwStat;
}
int main()
{
    DWORD status = 0;
    const char machine[]="";
    CHAR  Service_Name[32];
    strcpy(Service_Name,"Sundar_Service");
    status = ServiceGetStatus(Service_Name);
    if(status == 1)
        std::cout<<"service stopped"<<std::endl;
    if(status == 4)
        std::cout<<"service is running"<<std::endl;
    return 0;
}


SaswatPadhi 26May2009 19:05

Re: finding the status of service in windows
 
Quote:

Originally Posted by suresh_rtp (Post 48428)
Code: cpp

.
.
schs = OpenService(schm,sService,SERVICE_QUERY_STATUS);
.
.


I think you should do it like this :
Code: cpp

schs = OpenService(schm,sService,SERVICE_ALL_ACCESS);


The other mistake is here :
Quote:

Originally Posted by suresh_rtp (Post 48428)
Code: cpp

.
.
if(QueryServiceStatus(schs,TServiceStatus))
.
.


I think it should be done like this :
Code: cpp

if(QueryServiceStatus(schs,&TServiceStatus))

Note that you should pass the address of TServiceStatus and not itself, and declare TServiceStatus of type SERVICE_STATUS.
What you were doing is passing a NULL pointer as arg !

So, I think the working code becomes :
Code: cpp

DWORD ServiceGetStatus(char *sService)
{
      SC_HANDLE schm;
      SC_HANDLE schs;
      SERVICE_STATUS TServiceStatus;
      DWORD dwStat = -1;
      schm = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT);
      if(schm > 0)
      {
            schs = OpenService(schm,sService,SERVICE_ALL_ACCESS);
            if(schs > 0)
            {
                  if(QueryServiceStatus(schs, &TServiceStatus))
                  {
                        dwStat = TServiceStatus->dwCurrentState;
                        CloseServiceHandle(schs);
                        CloseServiceHandle(schm);
                    }
                    std::cout<<"status is false"<<std::endl;
            }
      }
      return dwStat;
}

int main()
{
      DWORD status = 0;
      const char machine[]="";
      CHAR  Service_Name[32];
      strcpy(Service_Name,"Sundar_Service");
      status = ServiceGetStatus(Service_Name);
      if(status == 1)
            std::cout<<"service stopped"<<std::endl;
      else if(status == 4)
            std::cout<<"service is running"<<std::endl;
      return 0;
}


suresh_rtp 29May2009 15:52

Re: finding the status of service in windows
 
hi,
thank you for your help.

regards
suresh

SaswatPadhi 29May2009 16:56

Re: finding the status of service in windows
 
My pleasure ! :biggrin:


All times are GMT +5.5. The time now is 16:08.