Search in sources :

Example 41 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 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 42 with Advapi32

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

the class Advapi32Test method testCreateProcessAsUser.

public void testCreateProcessAsUser() {
    HANDLEByReference hToken = new HANDLEByReference();
    HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
    assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, hToken));
    try {
        assertFalse(Advapi32.INSTANCE.CreateProcessAsUser(hToken.getValue(), null, "InvalidCmdLine.jna", null, null, false, 0, null, null, new WinBase.STARTUPINFO(), new WinBase.PROCESS_INFORMATION()));
        assertEquals(W32Errors.ERROR_FILE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32Util.closeHandleRef(hToken);
    }
}
Also used : PROCESS_INFORMATION(com.sun.jna.platform.win32.WinBase.PROCESS_INFORMATION) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) STARTUPINFO(com.sun.jna.platform.win32.WinBase.STARTUPINFO) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 43 with Advapi32

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

Example 44 with Advapi32

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

the class SetupApiTest method testSetupDiOpenDevRegKey.

/**
	 * Tests the mapping of {@link SetupApi#SetupDiOpenDevRegKey(HANDLE, SP_DEVINFO_DATA, int, int, int, int)} .
	 * <p>
	 * The test pass if SetupDiOpenDevRegKey(..) returns a valid {@link HKEY} pointing to the first found device on the current machine.
	 * <p>
	 * NOTE: We only test the mapping of SetupDiOpenDevRegKey(..), not it's functionality.
	 */
public void testSetupDiOpenDevRegKey() {
    // hDevInfoSet repesents a list of installed devices for all device
    // setup classes or all device interface classes
    HANDLE hDevInfoSet = SetupApi.INSTANCE.SetupDiGetClassDevs(null, null, null, DIGCF_ALLCLASSES);
    assertTrue(hDevInfoSet != INVALID_HANDLE_VALUE);
    SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA();
    // there must be least one device (drive,processor,pci,usb,...) on the
    // current machine
    assertTrue(SetupApi.INSTANCE.SetupDiEnumDeviceInfo(hDevInfoSet, FIRST_MEMBER, devInfo));
    HKEY hDeviceKey = SetupApi.INSTANCE.SetupDiOpenDevRegKey(hDevInfoSet, devInfo, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
    assertTrue(hDeviceKey != INVALID_HANDLE_VALUE);
    Advapi32.INSTANCE.RegCloseKey(hDeviceKey);
}
Also used : SP_DEVINFO_DATA(com.sun.jna.platform.win32.SetupApi.SP_DEVINFO_DATA) HKEY(com.sun.jna.platform.win32.WinReg.HKEY) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 45 with Advapi32

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

the class Netapi32UtilTest method testGetUserInfoWithDomainSpecified.

public void testGetUserInfoWithDomainSpecified() {
    UserInfo userInfo = Netapi32Util.getUserInfo(Advapi32Util.getUserName(), System.getenv("USERDOMAIN"));
    assertNotNull("No user info retrieved", userInfo);
    assertTrue("Invalid user SID", Advapi32.INSTANCE.IsValidSid(userInfo.sid));
}
Also used : UserInfo(com.sun.jna.platform.win32.Netapi32Util.UserInfo)

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