Home > Backend Development > C++ > body text

How Can Non-Administrator Users Start and Stop Windows Services in C ?

Barbara Streisand
Release: 2024-10-25 04:07:28
Original
982 people have browsed it

How Can Non-Administrator Users Start and Stop Windows Services in C  ?

Starting Windows Service from Application without Administrator Rights (C )

Question:

How can a user start or stop a Windows service from a non-administrator application without the need for administrator rights?

Answer:

To grant non-administrator users the ability to start and stop a Windows service, modify the service permissions. This process should ideally be performed during service installation.

Use the following steps:

  1. Obtain the Service Handle: Open a handle to the target service with the WRITE_DAC permission.
  2. Create the Security Descriptor (SDDL): Construct an SDDL string that specifies the desired permissions for various user groups, such as allowing interactive users to start the service:
<code class="pre">wchar_t sddl[] = L"D:"
  L"(A;;CCLCSWRPWPDTLOCRRC;;;SY)"           // default permissions for local system
  L"(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)"   // default permissions for administrators
  L"(A;;CCLCSWLOCRRC;;;AU)"                 // default permissions for authenticated users
  L"(A;;CCLCSWRPWPDTLOCRRC;;;PU)"           // default permissions for power users
  L"(A;;RP;;;IU)"                           // added permission: start service for interactive users
  ;</code>
Copy after login
  1. Convert and Set the SD: Convert the SDDL string to a security descriptor and set it on the service object using the SetServiceObjectSecurity function:
<code class="pre">PSECURITY_DESCRIPTOR sd;

if (!ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, SDDL_REVISION_1, &amp;sd, NULL))
{
   fail();
}

if (!SetServiceObjectSecurity(service, DACL_SECURITY_INFORMATION, sd))
{
   fail();
}</code>
Copy after login

To grant non-admin users the right to stop the service as well, include the WP right in the SDDL string:

<code class="pre">L"(A;;RPWP;;;IU)"                           
  // added permissions: start service, stop service for interactive users</code>
Copy after login

Refer to Wayne Martin's blog entry for additional SDDL codes for service rights.

The above is the detailed content of How Can Non-Administrator Users Start and Stop Windows Services in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!