1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

finding the status of service in windows

Discussion in 'C' started by suresh_rtp, May 26, 2009.

  1. suresh_rtp

    suresh_rtp New Member

    Joined:
    Oct 12, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
    Last edited by a moderator: May 26, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    I think you should do it like this :
    Code:
    schs = OpenService(schm,sService,SERVICE_ALL_ACCESS);
    The other mistake is here :
    I think it should be done like this :
    Code:
    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:
    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;
    }
     
    Last edited: May 26, 2009
  3. suresh_rtp

    suresh_rtp New Member

    Joined:
    Oct 12, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    hi,
    thank you for your help.

    regards
    suresh
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My pleasure ! :biggrin:
     

Share This Page