Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Windoes Service Status ?
QtWS25 Last Chance

Windoes Service Status ?

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 2 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rhino219
    wrote on last edited by
    #1

    Hi all,

    I have an C# Windows Service and a QT GUI. Now I want to check the running status out of the GUI.

    I know that I get the status via dos command "sc query myserive"

    Is there no method or instance to get the Servicestatus ?

    Regards

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      First of all, this is no Qt related question.
      You can use the windows service api.

      If you follow this "link":http://www.lmgtfy.com/?q=windows+service+api , you come to this "result":http://msdn.microsoft.com/en-us/library/ms685141(v=vs.85).aspx

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rhino219
        wrote on last edited by
        #3

        The question was if I can use a command like

        QtService->getstatus("servicename");

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rhino219
          wrote on last edited by
          #4

          Or like this. But this is C#

          ServiceController sc = new ServiceController("Spooler", "Server1");

          if (sc.Status == ServiceControllerStatus.Running)
          {
          MessageBox.Show("The service is running.");
          }

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            The C# code uses the "Win32 Service api":http://msdn.microsoft.com/en-us/library/ms685942(v=VS.85).aspx , that can be done.
            like this:

            @
            SC_HANDLE hScManager = OpenSCManager(0, // local computer or add computer name here
            0, // SERVICES_ACTIVE_DATABASE database is opened by default.
            GENERIC_READ); // onyl read info
            if(0 != hScManager)
            {
            SC_HANDLE hSvc = OpenService(hScManager, // service manager
            "Spooler", // service name
            GENERIC_READ); // onyl read info
            if(0 != hSvc)
            {
            SERVICE_STATUS_PROCESS sInfo;
            DWORD bytesNeeded = 0;

                    if(QueryServiceStatusEx(hSvc,                   // A handle to the service. 
                                            SC_STATUS_PROCESS_INFO, // info requested
                                            &sInfo,                 // structure to load info to
                                            sizeof(sInfo),          // size of the buffer
                                            &bytesNeeded))
                    {
                        if(sInfo.dwCurrentState == SERVICE_RUNNING)
                        {
                            QMessageBox::information(0, tr("MyApp"), tr("Service is running"));
                        }
                    }
                }
            }
            

            @

            Warning: the code is not tested, just build from the documentation without compilation. Might need some adjustments.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rhino219
              wrote on last edited by
              #6

              Thanks alot,

              i have it. This is the working code.

              @LPCWSTR ServiceName = L"My ServiceName";

              SC_HANDLE hScManager = OpenSCManager(0, // local computer or add computer name here
              0, // SERVICES_ACTIVE_DATABASE database is opened by default.
              GENERIC_READ); // onyl read info
              if(0 != hScManager)
              {
              SC_HANDLE hSvc = OpenService(hScManager, // service manager
              ServiceName, // service name
              GENERIC_READ); // onyl read info
              if(0 != hSvc)
              {
              SERVICE_STATUS_PROCESS sInfo;

                  DWORD bytesNeeded = 0;
                   
                  if(QueryServiceStatusEx(hSvc,                   // A handle to the service. 
                                          SC_STATUS_PROCESS_INFO, // info requested
                                          (LPBYTE)&sInfo,                 // structure to load info to
                                          sizeof(sInfo),          // size of the buffer
                                          &bytesNeeded))
                  {
                      if(sInfo.dwCurrentState == SERVICE_RUNNING)
                      {
                          traymessage("test", "start!!!", 3);
                      }
                      else
                      {
                            traymessage("test", "stop!!!", 3);
                      }
                  }
              }
              

              }@

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved