Search in sources :

Example 1 with User

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

the class Advapi32Test method testRegQueryValueEx.

public void testRegQueryValueEx() {
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, WinNT.KEY_READ, phKey));
    IntByReference lpcbData = new IntByReference();
    IntByReference lpType = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 0, lpType, (char[]) null, lpcbData));
    assertEquals(WinNT.REG_SZ, lpType.getValue());
    assertTrue(lpcbData.getValue() > 0);
    char[] buffer = new char[lpcbData.getValue()];
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 0, lpType, buffer, lpcbData));
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Example 2 with User

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

the class ITypeLibTest method loadShellTypeLib.

private ITypeLib loadShellTypeLib() {
    CLSID.ByReference clsid = new CLSID.ByReference();
    // get CLSID from string
    HRESULT hr = Ole32.INSTANCE.CLSIDFromString(new WString(SHELL_CLSID), clsid);
    assertTrue(COMUtils.SUCCEEDED(hr));
    // get user default lcid
    LCID lcid = Kernel32.INSTANCE.GetUserDefaultLCID();
    PointerByReference pShellTypeLib = new PointerByReference();
    // load typelib
    hr = OleAuto.INSTANCE.LoadRegTypeLib(clsid, SHELL_MAJOR, SHELL_MINOR, lcid, pShellTypeLib);
    assertTrue(COMUtils.SUCCEEDED(hr));
    return new TypeLib(pShellTypeLib.getValue());
}
Also used : WString(com.sun.jna.WString) LCID(com.sun.jna.platform.win32.WinDef.LCID) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) CLSID(com.sun.jna.platform.win32.Guid.CLSID) PointerByReference(com.sun.jna.ptr.PointerByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) USHORTByReference(com.sun.jna.platform.win32.WinDef.USHORTByReference)

Example 3 with User

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

the class Advapi32Test method testCreateProcessWithLogonW.

public void testCreateProcessWithLogonW() {
    String winDir = Kernel32Util.getEnvironmentVariable("WINDIR");
    assertNotNull("No WINDIR value returned", winDir);
    assertTrue("Specified WINDIR does not exist: " + winDir, new File(winDir).exists());
    STARTUPINFO si = new STARTUPINFO();
    si.lpDesktop = null;
    PROCESS_INFORMATION results = new PROCESS_INFORMATION();
    // i have the same combination on my luggage
    boolean result = Advapi32.INSTANCE.CreateProcessWithLogonW("A" + System.currentTimeMillis(), "localhost", "12345", Advapi32.LOGON_WITH_PROFILE, new File(winDir, "notepad.exe").getAbsolutePath(), "", 0, null, "", si, results);
    // we tried to run notepad as a bogus user, so it should fail.
    assertFalse("CreateProcessWithLogonW should have returned false because the username was bogus.", result);
    // should fail with "the user name or password is incorrect" (error 1326)
    assertEquals("GetLastError() should have returned ERROR_LOGON_FAILURE because the username was bogus.", W32Errors.ERROR_LOGON_FAILURE, Native.getLastError());
}
Also used : PROCESS_INFORMATION(com.sun.jna.platform.win32.WinBase.PROCESS_INFORMATION) STARTUPINFO(com.sun.jna.platform.win32.WinBase.STARTUPINFO) File(java.io.File)

Example 4 with User

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

the class Advapi32Test method testImpersonateLoggedOnUser.

public void testImpersonateLoggedOnUser() {
    USER_INFO_1 userInfo = new USER_INFO_1();
    userInfo.usri1_name = "JNAAdvapi32TestImp";
    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(null, 1, userInfo, null)) {
        return;
    }
    try {
        HANDLEByReference phUser = new HANDLEByReference();
        try {
            assertTrue(Advapi32.INSTANCE.LogonUser(userInfo.usri1_name.toString(), null, userInfo.usri1_password.toString(), WinBase.LOGON32_LOGON_NETWORK, WinBase.LOGON32_PROVIDER_DEFAULT, phUser));
            assertTrue(Advapi32.INSTANCE.ImpersonateLoggedOnUser(phUser.getValue()));
            assertTrue(Advapi32.INSTANCE.RevertToSelf());
        } finally {
            HANDLE hUser = phUser.getValue();
            if (!WinBase.INVALID_HANDLE_VALUE.equals(hUser)) {
                Kernel32Util.closeHandle(hUser);
            }
        }
    } finally {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(null, userInfo.usri1_name.toString()));
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 5 with User

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

the class Advapi32UtilTest method testGetUserAccount.

public void testGetUserAccount() {
    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(null, 1, userInfo, null)) {
        return;
    }
    try {
        HANDLEByReference phUser = new HANDLEByReference();
        try {
            assertTrue(Advapi32.INSTANCE.LogonUser(userInfo.usri1_name.toString(), null, userInfo.usri1_password.toString(), WinBase.LOGON32_LOGON_NETWORK, WinBase.LOGON32_PROVIDER_DEFAULT, phUser));
            Advapi32Util.Account account = Advapi32Util.getTokenAccount(phUser.getValue());
            assertTrue(account.name.length() > 0);
            assertEquals(userInfo.usri1_name.toString(), account.name);
        } finally {
            HANDLE hUser = phUser.getValue();
            if (!WinBase.INVALID_HANDLE_VALUE.equals(hUser)) {
                Kernel32Util.closeHandle(hUser);
            }
        }
    } finally {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(null, userInfo.usri1_name.toString()));
    }
}
Also used : Account(com.sun.jna.platform.win32.Advapi32Util.Account) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

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