use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.
the class Netapi32Test method testNetUserChangePassword.
public void testNetUserChangePassword() {
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;
}
try {
assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserChangePassword(Kernel32Util.getComputerName(), userInfo.usri1_name.toString(), userInfo.usri1_password.toString(), "!JNAP%%Wrd1"));
} finally {
assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(Kernel32Util.getComputerName(), userInfo.usri1_name.toString()));
}
}
use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.
the class Netapi32Util method getUserGroups.
/**
* Get groups of a given user on a given system.
* @param userName User name.
* @param serverName Server name.
* @return Groups.
*/
public static Group[] getUserGroups(String userName, String serverName) {
PointerByReference bufptr = new PointerByReference();
IntByReference entriesread = new IntByReference();
IntByReference totalentries = new IntByReference();
try {
int rc = Netapi32.INSTANCE.NetUserGetGroups(serverName, userName, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries);
if (rc != LMErr.NERR_Success) {
throw new Win32Exception(rc);
}
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());
ArrayList<Group> result = new ArrayList<Group>();
for (GROUP_USERS_INFO_0 lgpi : lgroups) {
Group lgp = new Group();
if (lgpi.grui0_name != null) {
lgp.name = lgpi.grui0_name.toString();
}
result.add(lgp);
}
return result.toArray(new Group[0]);
} finally {
if (bufptr.getValue() != Pointer.NULL) {
int rc = Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
if (LMErr.NERR_Success != rc) {
throw new Win32Exception(rc);
}
}
}
}
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);
}
}
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);
}
}
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()));
}
Aggregations