Convert Windows Service Status DWORD value into human readable format

1

I have a function for enumerating all the services on a Windows system using win32service.EnumServicesStatus() function from PyWin32 module in Python 3. I'm able to get the Status Name, Service Description and Status. The problem is that for the status instead of getting a string like "running", "paused", "stopping", "restarting" etc. I get a tuple of 7 elements.

An example:

status_code = (32, 1, 0, 1077, 0, 0, 0)

The SERVICE_STATUS structure is like this:

C++

typedef struct _SERVICE_STATUS {
  DWORD dwServiceType;
  DWORD dwCurrentState;
  DWORD dwControlsAccepted;
  DWORD dwWin32ExitCode;
  DWORD dwServiceSpecificExitCode;
  DWORD dwCheckPoint;
  DWORD dwWaitHint;
} SERVICE_STATUS, *LPSERVICE_STATUS;

There are many information available anyway I'm only interested in the CurrentState of the service which is represented by the second element of the tuple. In the example, it's number "1" (0x00000001).

This table shows the meaning of each value

SERVICE_CONTINUE_PENDING 0x00000005 The service continue is pending.

SERVICE_PAUSE_PENDING 0x00000006 The service pause is pending.

SERVICE_PAUSED 0x00000007 The service is paused.

SERVICE_RUNNING 0x00000004 The service is running.

SERVICE_START_PENDING 0x00000002 The service is starting.

SERVICE_STOP_PENDING 0x00000003 The service is stopping.

SERVICE_STOPPED 0x00000001 The service is not running.

How can I map a string to each value and display a string showing the current state?

I'm thinking that I could create a dictionary like this and then extract the string corresponding to the value:

status = {"1": "not running", "2": "starting", "3": "stopping", "4": "running", "5": "continue pending", "6": "pause pending", "7": "paused"}

Then I extract the second value of the tuple: status_code[1] and I need to compare it to the value inside the dictionary status. In order to print the string of the Service Status I can do something like:

print(status[str(status_code[1])])

Am I doing right? Is there a better way to do it?

python
winapi
service
status
pywin32
asked on Stack Overflow Jul 16, 2016 by Fabio

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0