use of com.sun.jna.platform.win32.Winsvc.SC_ACTION 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.Winsvc.SC_ACTION in project jna by java-native-access.
the class W32Service method setFailureActions.
/**
* Set the failure actions of the specified service. Corresponds to
* <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988.aspx">ChangeServiceConfig2</a>
* with parameter dwInfoLevel set to SERVICE_CONFIG_FAILURE_ACTIONS.
*/
public void setFailureActions(List<SC_ACTION> actions, int resetPeriod, String rebootMsg, String command) {
SERVICE_FAILURE_ACTIONS.ByReference actionStruct = new SERVICE_FAILURE_ACTIONS.ByReference();
actionStruct.dwResetPeriod = resetPeriod;
actionStruct.lpRebootMsg = rebootMsg;
actionStruct.lpCommand = command;
actionStruct.cActions = actions.size();
actionStruct.lpsaActions = new SC_ACTION.ByReference();
SC_ACTION[] actionArray = (SC_ACTION[]) actionStruct.lpsaActions.toArray(actions.size());
boolean hasShutdownPrivilege = false;
int i = 0;
for (SC_ACTION action : actions) {
if (!hasShutdownPrivilege && action.type == Winsvc.SC_ACTION_REBOOT) {
addShutdownPrivilegeToProcess();
hasShutdownPrivilege = true;
}
actionArray[i].type = action.type;
actionArray[i].delay = action.delay;
i++;
}
if (!Advapi32.INSTANCE.ChangeServiceConfig2(_handle, Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS, actionStruct)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
Aggregations