Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List service and services status under Win-7

I have a service monitor app that monitors the status of three other servers app - you know those kind of green, red status stuff, start, stop, etc.

The problem is that it shows the wrong state in Windows 7 even if the user is the administrator.

The start, stop buttons are disabled and the install button enabled, the status color is grey which is also wrong. The start button should be enabled with the service status showing green - the apps are running.

If the application is run with the setting "run as administrator" then it behaves normally.

The application is written in Delphi 7 and works perfectly in other versions of Windows. This line of code:

OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_ALL_ACCESS);

always return 0 under Win7, causing the problem.

Any ideas and if possible, any workaround apart from "run as administrator".

like image 420
ronaldosantana Avatar asked Feb 28 '23 06:02

ronaldosantana


1 Answers

The service manager can be opened without requiring Administrator privileges if limited access is requested. This line of code:

ManagerHandle := OpenSCManager(nil, SERVICES_ACTIVE_DATABASE,
  SC_MANAGER_ENUMERATE_SERVICE);

opens the database of active services with the service manager, even when used from a limited user account. The returned handle can then be used to call EnumServicesStatusEx() to get information about the running services.

Passing SC_MANAGER_ALL_ACCESS as the dwDesiredAccess parameter implies passing SC_MANAGER_CREATE_SERVICE, and as the documentation states:

Only processes with Administrator privileges are able to open a database handle that can be used by the CreateService function.

So if you limit your program to actions that can be performed by standard users, then your code should run without changes on Windows 7. Everything else needs to be done either by the installer, or by a special program that runs elevated.

This is BTW no new requirement of Windows 7, your statement

The application is written in Delphi 7 and works perfectly in other versions of Windows.

shows only that you never properly tested the application on limited accounts in earlier versions of the OS. With Windows NT already (nearly 20 years ago) a limited user was not allowed to open the service manager with full access rights, so the code would fail there just the same.

like image 58
mghie Avatar answered Mar 07 '23 11:03

mghie