'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.
Same can be achieved by running the 'sc' command in Windows CMD:
sc config "MyServiceName" obj="domain\windowsuser" password= "userpassword"
So far this is where I have reached in figuring out Java code (work in progress):
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).
Any light in the right direction (or documentation reference) would be helpful.
Solution 1:[1]
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
using System.Runtime.InteropServices;
using System.ComponentModel;
For reference: https://www.morgantechspace.com/2015/03/csharp-change-service-account-username-and-password.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Viraj |