Search in sources :

Example 21 with User

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

the class Advapi32Test method testGetTokenUserInformation.

public void testGetTokenUserInformation() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
        IntByReference tokenInformationLength = new IntByReference();
        assertFalse(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, null, 0, tokenInformationLength));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        WinNT.TOKEN_USER user = new WinNT.TOKEN_USER(tokenInformationLength.getValue());
        assertTrue(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, user, tokenInformationLength.getValue(), tokenInformationLength));
        assertTrue(tokenInformationLength.getValue() > 0);
        assertTrue(Advapi32.INSTANCE.IsValidSid(user.User.Sid));
        int sidLength = Advapi32.INSTANCE.GetLengthSid(user.User.Sid);
        assertTrue(sidLength > 0);
        assertTrue(sidLength < tokenInformationLength.getValue());
    // System.out.println(Advapi32Util.convertSidToStringSid(user.User.Sid));
    } finally {
        Kernel32Util.closeHandleRef(phToken);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 22 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 23 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 24 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)

Example 25 with User

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

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