Search in sources :

Example 1 with GROUP_USERS_INFO_0

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

the class Netapi32Util method getUserGroups.

/**
     * Get groups of a given user on a given system.
     * @param userName User name.
     * @param serverName Server name.
     * @return Groups.
     */
public static Group[] getUserGroups(String userName, String serverName) {
    PointerByReference bufptr = new PointerByReference();
    IntByReference entriesread = new IntByReference();
    IntByReference totalentries = new IntByReference();
    try {
        int rc = Netapi32.INSTANCE.NetUserGetGroups(serverName, userName, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries);
        if (rc != LMErr.NERR_Success) {
            throw new Win32Exception(rc);
        }
        GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
        GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
        ArrayList<Group> result = new ArrayList<Group>();
        for (GROUP_USERS_INFO_0 lgpi : lgroups) {
            Group lgp = new Group();
            if (lgpi.grui0_name != null) {
                lgp.name = lgpi.grui0_name.toString();
            }
            result.add(lgp);
        }
        return result.toArray(new Group[0]);
    } finally {
        if (bufptr.getValue() != Pointer.NULL) {
            int rc = Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
            if (LMErr.NERR_Success != rc) {
                throw new Win32Exception(rc);
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) GROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0) LOCALGROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0) PointerByReference(com.sun.jna.ptr.PointerByReference) ArrayList(java.util.ArrayList)

Example 2 with GROUP_USERS_INFO_0

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

the class Netapi32Test method testNetUserGetGroups.

public void testNetUserGetGroups() {
    User[] users = Netapi32Util.getUsers();
    assertTrue(users.length >= 1);
    PointerByReference bufptr = new PointerByReference();
    IntByReference entriesread = new IntByReference();
    IntByReference totalentries = new IntByReference();
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserGetGroups(null, users[0].name, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries));
    GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
    GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
    for (GROUP_USERS_INFO_0 localGroupInfo : lgroups) {
        assertTrue(localGroupInfo.grui0_name.length() > 0);
    }
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue()));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) User(com.sun.jna.platform.win32.Netapi32Util.User) GROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0) LOCALGROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0) PointerByReference(com.sun.jna.ptr.PointerByReference)

Aggregations

GROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 IntByReference (com.sun.jna.ptr.IntByReference)2 PointerByReference (com.sun.jna.ptr.PointerByReference)2 User (com.sun.jna.platform.win32.Netapi32Util.User)1 ArrayList (java.util.ArrayList)1