Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,

I was trying to modify AD credentials for a particular service, For that have been using JNA
com.sun.jna.platform.win32.Advapi32
library but I could not able to change or modify or upgrade AD credentials.

What I have tried:

I have tried in java.

public class JNAExample {
	private static void ModifyADCredentials(String username, String password) {
		SC_HANDLE serviceManager = Advapi32.INSTANCE.OpenSCManager("MLI-LAP-155", null, Winsvc.SC_MANAGER_ALL_ACCESS);
		SC_HANDLE service = Advapi32.INSTANCE.OpenService(serviceManager, "ServiceName", WinNT.GENERIC_EXECUTE);
		ChangeServiceConfigInfo info = new ChangeServiceConfigInfo();
		info.thisAccount = "adminGod";
		info.password = "admin@123";
		boolean value = Advapi32.INSTANCE.ChangeServiceConfig2(service, Winsvc.SERVICE_CHANGE_CONFIG, info);
		Advapi32.INSTANCE.CloseServiceHandle(service);
     }
}


@FieldOrder({ "thisAccount", "password", "confirmPassword" })
public class ChangeServiceConfigInfo extends ChangeServiceConfig2Info {

	static class myRef extends ChangeServiceConfigInfo implements ByReference {}
	
	public String thisAccount;
	public String password;
	public String confirmPassword;

	@Override
	protected Memory autoAllocate(int size) {
		// TODO Auto-generated method stub
		return super.autoAllocate(100);
	}

	@Override
	protected List<String> getFieldOrder() {
		// TODO Auto-generated method stub"
		return Arrays.asList(new String[] { "thisAccount", "password" ,"confirmPassword"});
	}
}
Posted
Updated 18-Apr-22 21:41pm

1 solution

Java
SC_HANDLE service = Advapi32.INSTANCE.OpenService(serviceManager, "ServiceName", WinNT.GENERIC_EXECUTE);

It is unlikely that there is a running service called "ServiceName". And you should always check the results of system calls to see if they succeeded or not. See OpenServiceA function (winsvc.h) - Win32 apps | Microsoft Docs[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900