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