Search in sources :

Example 46 with Advapi32

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()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 47 with Advapi32

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();
}
Also used : SC_ACTION(com.sun.jna.platform.win32.Winsvc.SC_ACTION) SERVICE_FAILURE_ACTIONS(com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS) LinkedList(java.util.LinkedList)

Example 48 with Advapi32

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);
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 49 with Advapi32

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;
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 50 with Advapi32

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;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HashMap(java.util.HashMap) Memory(com.sun.jna.Memory) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) ACCESS_ACEStructure(com.sun.jna.platform.win32.WinNT.ACCESS_ACEStructure) ACL(com.sun.jna.platform.win32.WinNT.ACL)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)51 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)39 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)31 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)31 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)23 PSID (com.sun.jna.platform.win32.WinNT.PSID)20 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Advapi32 (com.sun.jna.platform.win32.Advapi32)15 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)15 File (java.io.File)15 Memory (com.sun.jna.Memory)13 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)13 PSIDByReference (com.sun.jna.platform.win32.WinNT.PSIDByReference)10 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)9 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)7 Pointer (com.sun.jna.Pointer)6 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)5