Search in sources :

Example 81 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Win32Service method start.

/**
   * Ask the ServiceControlManager to start the service.
   * @return true on success
   */
public boolean start() {
    Advapi32 advapi32;
    SC_HANDLE serviceManager, service;
    boolean success = false;
    advapi32 = Advapi32.INSTANCE;
    serviceManager = openServiceControlManager(null, WinNT.GENERIC_EXECUTE);
    if (serviceManager != null) {
        service = advapi32.OpenService(serviceManager, serviceName, WinNT.GENERIC_EXECUTE);
        if (service != null) {
            success = advapi32.StartService(service, 0, null);
            advapi32.CloseServiceHandle(service);
        }
        advapi32.CloseServiceHandle(serviceManager);
    }
    return (success);
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 82 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Win32Service method openServiceControlManager.

/**
   * Get a handle to the ServiceControlManager.
   *
   * @param machine name of the machine or null for localhost
   * @param access access flags
   * @return handle to ServiceControlManager or null when failed
   */
private SC_HANDLE openServiceControlManager(String machine, int access) {
    SC_HANDLE handle = null;
    Advapi32 advapi32;
    advapi32 = Advapi32.INSTANCE;
    handle = advapi32.OpenSCManager(machine, null, access);
    return (handle);
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 83 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Util method getCurrentUserGroups.

/**
	 * Return the group memberships of the currently logged on user.
	 *
	 * @return An array of groups.
	 */
public static Account[] getCurrentUserGroups() {
    HANDLEByReference phToken = new HANDLEByReference();
    Win32Exception err = null;
    try {
        // open thread or process token
        HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();
        if (!Advapi32.INSTANCE.OpenThreadToken(threadHandle, TOKEN_DUPLICATE | TOKEN_QUERY, true, phToken)) {
            int rc = Kernel32.INSTANCE.GetLastError();
            if (rc != W32Errors.ERROR_NO_TOKEN) {
                throw new Win32Exception(rc);
            }
            HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
            if (!Advapi32.INSTANCE.OpenProcessToken(processHandle, TOKEN_DUPLICATE | TOKEN_QUERY, phToken)) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }
        }
        return getTokenGroups(phToken.getValue());
    } catch (Win32Exception e) {
        err = e;
        // re-throw in order to invoke finally block
        throw err;
    } finally {
        HANDLE hToken = phToken.getValue();
        if (!WinBase.INVALID_HANDLE_VALUE.equals(hToken)) {
            try {
                Kernel32Util.closeHandle(hToken);
            } catch (Win32Exception e) {
                if (err == null) {
                    err = e;
                } else {
                    err.addSuppressed(e);
                }
            }
        }
        if (err != null) {
            throw err;
        }
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 84 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Util method getTokenGroups.

/**
	 * This function returns the groups associated with a security token, such
	 * as a user token.
	 *
	 * @param hToken
	 *            Token.
	 * @return Token groups.
	 */
public static Account[] getTokenGroups(HANDLE hToken) {
    // get token group information size
    IntByReference tokenInformationLength = new IntByReference();
    if (Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, null, 0, tokenInformationLength)) {
        throw new RuntimeException("Expected GetTokenInformation to fail with ERROR_INSUFFICIENT_BUFFER");
    }
    int rc = Kernel32.INSTANCE.GetLastError();
    if (rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
        throw new Win32Exception(rc);
    }
    // get token group information
    WinNT.TOKEN_GROUPS groups = new WinNT.TOKEN_GROUPS(tokenInformationLength.getValue());
    if (!Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, groups, tokenInformationLength.getValue(), tokenInformationLength)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    ArrayList<Account> userGroups = new ArrayList<Account>();
    // make array of names
    for (SID_AND_ATTRIBUTES sidAndAttribute : groups.getGroups()) {
        Account group = null;
        try {
            group = Advapi32Util.getAccountBySid(sidAndAttribute.Sid);
        } catch (Exception e) {
            group = new Account();
            group.sid = sidAndAttribute.Sid.getBytes();
            group.sidString = Advapi32Util.convertSidToStringSid(sidAndAttribute.Sid);
            group.name = group.sidString;
            group.fqn = group.sidString;
            group.accountType = SID_NAME_USE.SidTypeGroup;
        }
        userGroups.add(group);
    }
    return userGroups.toArray(new Account[0]);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) SID_AND_ATTRIBUTES(com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES) IOException(java.io.IOException)

Example 85 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Util method fileEncryptionStatus.

/**
     * Checks the encryption status of a file.
     *
     * @param file
     *         The file to check the status for.
     * @return The status of the file.
     */
public static int fileEncryptionStatus(File file) {
    DWORDByReference status = new DWORDByReference();
    String lpFileName = file.getAbsolutePath();
    if (!Advapi32.INSTANCE.FileEncryptionStatus(lpFileName, status)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    return status.getValue().intValue();
}
Also used : DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference)

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