Search in sources :

Example 11 with User

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

the class Netapi32Test method testNetUserChangePassword.

public void testNetUserChangePassword() {
    USER_INFO_1 userInfo = new USER_INFO_1();
    userInfo.usri1_name = "JNANetapi32TestUser";
    userInfo.usri1_password = "!JNAP$$Wrd0";
    userInfo.usri1_priv = LMAccess.USER_PRIV_USER;
    // ignore test if not able to add user (need to be administrator to do this).
    if (LMErr.NERR_Success != Netapi32.INSTANCE.NetUserAdd(Kernel32Util.getComputerName(), 1, userInfo, null)) {
        return;
    }
    try {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserChangePassword(Kernel32Util.getComputerName(), userInfo.usri1_name.toString(), userInfo.usri1_password.toString(), "!JNAP%%Wrd1"));
    } finally {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(Kernel32Util.getComputerName(), userInfo.usri1_name.toString()));
    }
}
Also used : USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1)

Example 12 with User

use of com.sun.jna.platform.win32.Netapi32Util.User 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 13 with User

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

the class Secur32UtilTest method main.

public static void main(String[] args) {
    junit.textui.TestRunner.run(Secur32UtilTest.class);
    System.out.println("Current user: " + Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible));
    System.out.println("Security packages:");
    for (SecurityPackage sp : Secur32Util.getSecurityPackages()) {
        System.out.println(" " + sp.name + ": " + sp.comment);
    }
}
Also used : SecurityPackage(com.sun.jna.platform.win32.Secur32Util.SecurityPackage)

Example 14 with User

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

the class PdhTest method testQueryMultipleCounters.

@Test
public void testQueryMultipleCounters() {
    Collection<String> names = new LinkedList<String>();
    PDH_COUNTER_PATH_ELEMENTS elems = new PDH_COUNTER_PATH_ELEMENTS();
    elems.szObjectName = "Processor";
    elems.szInstanceName = "_Total";
    for (String n : new String[] { "% Processor Time", "% Idle Time", "% User Time" }) {
        elems.szCounterName = n;
        String counterName = makeCounterPath(pdh, elems);
        names.add(counterName);
    }
    HANDLEByReference ref = new HANDLEByReference();
    assertErrorSuccess("PdhOpenQuery", pdh.PdhOpenQuery(null, null, ref), true);
    HANDLE hQuery = ref.getValue();
    try {
        Map<String, HANDLE> handlesMap = new HashMap<String, HANDLE>(names.size());
        try {
            for (String counterName : names) {
                ref.setValue(null);
                assertErrorSuccess("PdhAddCounter[" + counterName + "]", pdh.PdhAddEnglishCounter(hQuery, counterName, null, ref), true);
                HANDLE hCounter = ref.getValue();
                handlesMap.put(counterName, hCounter);
            }
            assertErrorSuccess("PdhCollectQueryData", pdh.PdhCollectQueryData(hQuery), true);
            for (Map.Entry<String, HANDLE> ch : handlesMap.entrySet()) {
                String counterName = ch.getKey();
                HANDLE hCounter = ch.getValue();
                PDH_RAW_COUNTER rawCounter = new PDH_RAW_COUNTER();
                DWORDByReference lpdwType = new DWORDByReference();
                assertErrorSuccess("PdhGetRawCounterValue[" + counterName + "]", pdh.PdhGetRawCounterValue(hCounter, lpdwType, rawCounter), true);
                assertEquals("Bad counter data status for " + counterName, PdhMsg.PDH_CSTATUS_VALID_DATA, rawCounter.CStatus);
                showRawCounterData(System.out, counterName, rawCounter);
            }
        } finally {
            names.clear();
            for (Map.Entry<String, HANDLE> ch : handlesMap.entrySet()) {
                String name = ch.getKey();
                HANDLE hCounter = ch.getValue();
                int status = pdh.PdhRemoveCounter(hCounter);
                if (status != WinError.ERROR_SUCCESS) {
                    names.add(name);
                }
            }
            if (names.size() > 0) {
                fail("Failed to remove counters: " + names);
            }
        }
    } finally {
        assertErrorSuccess("PdhCloseQuery", pdh.PdhCloseQuery(hQuery), true);
    }
}
Also used : PDH_COUNTER_PATH_ELEMENTS(com.sun.jna.platform.win32.Pdh.PDH_COUNTER_PATH_ELEMENTS) HashMap(java.util.HashMap) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) PDH_RAW_COUNTER(com.sun.jna.platform.win32.Pdh.PDH_RAW_COUNTER) LinkedList(java.util.LinkedList) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with User

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

the class Netapi32Test method testNetUserAdd.

public void testNetUserAdd() {
    USER_INFO_1 userInfo = new USER_INFO_1();
    userInfo.usri1_name = "JNANetapi32TestUser";
    userInfo.usri1_password = "!JNAP$$Wrd0";
    userInfo.usri1_priv = LMAccess.USER_PRIV_USER;
    // ignore test if not able to add user (need to be administrator to do this).
    if (LMErr.NERR_Success != Netapi32.INSTANCE.NetUserAdd(Kernel32Util.getComputerName(), 1, userInfo, null)) {
        return;
    }
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(Kernel32Util.getComputerName(), userInfo.usri1_name.toString()));
}
Also used : USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)6 IntByReference (com.sun.jna.ptr.IntByReference)6 PointerByReference (com.sun.jna.ptr.PointerByReference)6 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)5 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)3 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 WString (com.sun.jna.WString)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 CLSID (com.sun.jna.platform.win32.Guid.CLSID)2 GROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0)2 HWND (com.sun.jna.platform.win32.WinDef.HWND)2 LCID (com.sun.jna.platform.win32.WinDef.LCID)2 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)2 UserModel (com.gitblit.models.UserModel)1 Pointer (com.sun.jna.Pointer)1 COMException (com.sun.jna.platform.win32.COM.COMException)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1