How to run a program from windows service with administrator privileges

瘦欲@ 提交于 2021-02-07 06:47:14

问题


I created a windows service that runs a program on windows startup, but with this solution the program runs without administrator privileges. However, if I run the program by click on the program's shortcut on the Start menu, it runs with administrator privileges

I used this code to run the program:

ApplicationLoader.PROCESS_INFORMATION procInfo;

ApplicationLoader.StartProcessAndBypassUAC(program_name, out procInfo);

So how to run this program with administrator privileges? Thanks


回答1:


To run as administrator (program)

Right click and choose "Run as administrator" (one-time / ad hoc)

or right click and choose properties > Compatibility > check "Run this program as administrator" (set it permanently; can also be handy for setting this property on a shortcut (which is basically what Start menu items are))

To not run as administrator (service)

For services, to make it not run as an admin, open the services snap in (run services.msc from command prompt) and right click to edit properties of the service, then update the user info on the "Logon" tab.

UPDATE

It looks like you've used some code from a CodeProject article that wraps the Windows API function CreateProcessAsUser (http://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-and-bit-Archite). Make sure you read that article and follow the steps. You may not have set the correct user on the service, or your current user may not have correct perms, or may not be a member of Administrators, etc.



来源:https://stackoverflow.com/questions/27263901/how-to-run-a-program-from-windows-service-with-administrator-privileges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!