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()));
}
}
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);
}
}
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()));
}
}
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);
}
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));
}
Aggregations