在没有管理员权限的情况下从应用程序启动 Win

Start Windows Service From Application without Admin right(c++)(在没有管理员权限的情况下从应用程序启动 Windows 服务 (c++))
本文介绍了在没有管理员权限的情况下从应用程序启动 Windows 服务 (c++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 Windows 服务(并且运行良好).现在我有一个单独的应用程序,我想从中启动此服务,但如果没有管理员权限,这似乎是不可能的.

I wrote a windows service (and it runs fine). Now i have a separate app where I want to start this service from, but it seems this is not possible without administrator rights.

用户可以启动/停止服务(例如从托盘或应用程序)的正确解决方案是怎样的

How would a proper solution look like that a user can start/stop the service (e.g. from a tray or application)

恕我直言,应用程序必须始终以管理员权限启动是不好的.

IMHO its bad that the application must always be started with administrator rights.

推荐答案

您只需要更改服务对象的权限,最好在安装的同时更改.

You just need to change the permissions on the service object, preferably at the same time you install it.

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
  ;

PSECURITY_DESCRIPTOR sd;

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

if (!SetServiceObjectSecurity(service, DACL_SECURITY_INFORMATION, sd))
{
   fail();
}

我在这里假设您已经打开了服务句柄.您需要 WRITE_DAC 权限.

I'm assuming here you've already opened the service handle. You need WRITE_DAC permission.

如果您还希望非管理员用户能够停止服务,请添加 WP 权限,即

If you also want non-admin users to be able to stop the service, add the WP right, i.e.,

L"(A;;RPWP;;;IU)"                           
  // added permissions: start service, stop service for interactive users

服务权限的 SDDL 代码可以在 Wayne Martin 的博客条目中找到,非管理员的服务控制管理器安全.

SDDL codes for service rights can be found in Wayne Martin's blog entry, Service Control Manager Security for non-admins.

这篇关于在没有管理员权限的情况下从应用程序启动 Windows 服务 (c++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)