简体   繁体   English

通过Advapi32(JNA API)更改Java中现有Windows服务的用户名和密码

[英]Change username and password for an existing Windows service in Java via Advapi32 (JNA API)

I have been trying to change configuration for an existing Windows Service via the JNA API, specifically via com.sun.jna.platform.win32.Advapi32 class but could not find enough documentation for the same. 我一直在尝试通过JNA API(特别是通过com.sun.jna.platform.win32.Advapi32类)更改现有Windows Service的配置,但是找不到足够的文档来进行配置。

Same can be achieved by running the 'sc' command in Windows CMD: 通过在Windows CMD中运行'sc'命令可以实现相同的目的:

sc config "MyServiceName" obj="domain\windowsuser" password= "userpassword"

So far this is where I have reached in figuring out Java code (work in progress): 到目前为止,这是我弄清楚Java代码(正在进行的工作)的目的:

private static void printAllServices(String username, String password) {
    boolean success = false;
    SC_HANDLE serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS);
    if (serviceManager != null) {
        SC_HANDLE service = Advapi32.INSTANCE.OpenService(serviceManager, "MyServiceName",
                WinNT.GENERIC_EXECUTE);
        if (service != null) {
            SERVICE_STATUS serviceStatus = new SERVICE_STATUS();
            success = Advapi32.INSTANCE.ControlService(service, Winsvc.SERVICE_CHANGE_CONFIG, serviceStatus);
            Advapi32.INSTANCE.CloseServiceHandle(service);
        }
        Advapi32.INSTANCE.CloseServiceHandle(serviceManager);
    }
}

private static SC_HANDLE openServiceControlManager(String machine, int access) {
        return Advapi32.INSTANCE.OpenSCManager(machine, null, access);
    }

My objective is to change the username/password under which an existing windows service is running via Java API (but not by executing the 'sc' command directly but via JNA API). 我的目标是通过Java API更改运行Windows服务的用户名/密码(但不能直接通过JNA API执行“ sc”命令)。

Any light in the right direction (or documentation reference) would be helpful. 朝着正确方向(或参考文档)的任何提示都将有所帮助。

As there seems to be no JNA API under Advapi32, made a call to an script.exe (passing required arguments) which contains C# code that does DllImport of "advapi32.dll" and using APIs under following imports 由于Advapi32下似乎没有JNA API,因此调用了script.exe(传递必需的参数),该脚本包含执行“ advapi32.dll”的DllImport的C#代码并在以下导入下使用API

using System.Runtime.InteropServices;
using System.ComponentModel;

For reference: https://www.morgantechspace.com/2015/03/csharp-change-service-account-username-and-password.html 供参考: https : //www.morgantechspace.com/2015/03/csharp-change-service-account-username-and-password.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM