use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.
the class Advapi32Test method testGetTokenUserInformation.
public void testGetTokenUserInformation() {
HANDLEByReference phToken = new HANDLEByReference();
try {
HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
IntByReference tokenInformationLength = new IntByReference();
assertFalse(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, null, 0, tokenInformationLength));
assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
WinNT.TOKEN_USER user = new WinNT.TOKEN_USER(tokenInformationLength.getValue());
assertTrue(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, user, tokenInformationLength.getValue(), tokenInformationLength));
assertTrue(tokenInformationLength.getValue() > 0);
assertTrue(Advapi32.INSTANCE.IsValidSid(user.User.Sid));
int sidLength = Advapi32.INSTANCE.GetLengthSid(user.User.Sid);
assertTrue(sidLength > 0);
assertTrue(sidLength < tokenInformationLength.getValue());
// System.out.println(Advapi32Util.convertSidToStringSid(user.User.Sid));
} finally {
Kernel32Util.closeHandleRef(phToken);
}
}
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()));
}
use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.
the class Netapi32Test method testNetUserGetGroups.
public void testNetUserGetGroups() {
User[] users = Netapi32Util.getUsers();
assertTrue(users.length >= 1);
PointerByReference bufptr = new PointerByReference();
IntByReference entriesread = new IntByReference();
IntByReference totalentries = new IntByReference();
assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserGetGroups(null, users[0].name, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries));
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());
for (GROUP_USERS_INFO_0 localGroupInfo : lgroups) {
assertTrue(localGroupInfo.grui0_name.length() > 0);
}
assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue()));
}
Aggregations