use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class NtDllUtilTest method testGetKeyName.
public void testGetKeyName() {
HKEYByReference phKey = new HKEYByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
// Keys are case insensitive (https://msdn.microsoft.com/de-de/library/windows/desktop/ms724946(v=vs.85).aspx)
assertEquals("software", NtDllUtil.getKeyName(phKey.getValue()).toLowerCase());
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class W32ServiceTest method testSetAndGetFailureActions.
public void testSetAndGetFailureActions() {
final String svcId = "w32time";
final String rebootMsg = "Restarting " + svcId + " due to service failure";
final String command = "echo " + svcId + " failure";
final int resetPeriod = 5000;
W32Service service = _serviceManager.openService(svcId, Winsvc.SC_MANAGER_ALL_ACCESS);
SERVICE_FAILURE_ACTIONS prevActions = service.getFailureActions();
List<SC_ACTION> actions = new LinkedList<SC_ACTION>();
SC_ACTION action = new SC_ACTION();
action.type = Winsvc.SC_ACTION_RESTART;
action.delay = 1000;
actions.add(action);
action = new SC_ACTION();
action.type = Winsvc.SC_ACTION_REBOOT;
action.delay = 2000;
actions.add(action);
action = new SC_ACTION();
action.type = Winsvc.SC_ACTION_RUN_COMMAND;
action.delay = 3000;
actions.add(action);
action = new SC_ACTION();
action.type = Winsvc.SC_ACTION_NONE;
action.delay = 4000;
actions.add(action);
service.setFailureActions(actions, resetPeriod, rebootMsg, command);
SERVICE_FAILURE_ACTIONS changedActions = service.getFailureActions();
assertEquals(changedActions.lpRebootMsg, rebootMsg);
assertEquals(changedActions.lpCommand, command);
assertEquals(changedActions.dwResetPeriod, resetPeriod);
assertEquals(changedActions.cActions, 4);
SC_ACTION[] actionArray = (SC_ACTION[]) changedActions.lpsaActions.toArray(changedActions.cActions);
assertEquals(actionArray[0].type, Winsvc.SC_ACTION_RESTART);
assertEquals(actionArray[0].delay, 1000);
assertEquals(actionArray[1].type, Winsvc.SC_ACTION_REBOOT);
assertEquals(actionArray[1].delay, 2000);
assertEquals(actionArray[2].type, Winsvc.SC_ACTION_RUN_COMMAND);
assertEquals(actionArray[2].delay, 3000);
assertEquals(actionArray[3].type, Winsvc.SC_ACTION_NONE);
assertEquals(actionArray[3].delay, 4000);
// restore old settings
Advapi32.INSTANCE.ChangeServiceConfig2(service._handle, Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS, prevActions);
service.close();
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Win32Service method uninstall.
/**
* Uninstall the service.
*
* @throws java.lang.Exception
* @return true on success
*/
public boolean uninstall() {
Advapi32 advapi32;
SC_HANDLE serviceManager, service;
boolean success = false;
advapi32 = Advapi32.INSTANCE;
serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS);
if (serviceManager != null) {
service = advapi32.OpenService(serviceManager, serviceName, Winsvc.SERVICE_ALL_ACCESS);
if (service != null) {
success = advapi32.DeleteService(service);
advapi32.CloseServiceHandle(service);
}
advapi32.CloseServiceHandle(serviceManager);
}
return (success);
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Util method registryGetKey.
/**
* Get a registry key, the caller is responsible to close the key after use.
*
* @param root
* Root key.
* @param keyPath
* Path to a registry key.
*
* @param samDesired
* Access level (e.g. WinNT.KEY_READ)
*
* @return HKEYByReference.
*/
public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) {
HKEYByReference phkKey = new HKEYByReference();
int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
return phkKey;
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Util method getFileSecurity.
public static ACCESS_ACEStructure[] getFileSecurity(String fileName, boolean compact) {
int infoType = WinNT.DACL_SECURITY_INFORMATION;
int nLength = 1024;
boolean repeat = false;
Memory memory = null;
do {
repeat = false;
memory = new Memory(nLength);
IntByReference lpnSize = new IntByReference();
boolean succeded = Advapi32.INSTANCE.GetFileSecurity(fileName, infoType, memory, nLength, lpnSize);
if (!succeded) {
int lastError = Kernel32.INSTANCE.GetLastError();
memory.clear();
if (W32Errors.ERROR_INSUFFICIENT_BUFFER != lastError) {
throw new Win32Exception(lastError);
}
}
int lengthNeeded = lpnSize.getValue();
if (nLength < lengthNeeded) {
repeat = true;
nLength = lengthNeeded;
memory.clear();
}
} while (repeat);
SECURITY_DESCRIPTOR_RELATIVE sdr = new WinNT.SECURITY_DESCRIPTOR_RELATIVE(memory);
memory.clear();
ACL dacl = sdr.getDiscretionaryACL();
ACCESS_ACEStructure[] aceStructures = dacl.getACEStructures();
if (compact) {
Map<String, ACCESS_ACEStructure> aceMap = new HashMap<String, ACCESS_ACEStructure>();
for (ACCESS_ACEStructure aceStructure : aceStructures) {
boolean inherted = ((aceStructure.AceFlags & WinNT.VALID_INHERIT_FLAGS) != 0);
String key = aceStructure.getSidString() + "/" + inherted + "/" + aceStructure.getClass().getName();
ACCESS_ACEStructure aceStructure2 = aceMap.get(key);
if (aceStructure2 != null) {
int accessMask = aceStructure2.Mask;
accessMask = accessMask | aceStructure.Mask;
aceStructure2.Mask = accessMask;
} else {
aceMap.put(key, aceStructure);
}
}
return aceMap.values().toArray(new ACCESS_ACEStructure[aceMap.size()]);
}
return aceStructures;
}
Aggregations