use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class SystemBTest method testVMInfo.
public void testVMInfo() {
int machPort = SystemB.INSTANCE.mach_host_self();
assertTrue(machPort > 0);
VMStatistics vmStats = new VMStatistics();
int ret = SystemB.INSTANCE.host_statistics(machPort, SystemB.HOST_VM_INFO, vmStats, new IntByReference(vmStats.size() / SystemB.INT_SIZE));
assertEquals(ret, 0);
// Nonnegative
assertTrue(vmStats.free_count >= 0);
if (Platform.is64Bit()) {
VMStatistics64 vmStats64 = new VMStatistics64();
ret = SystemB.INSTANCE.host_statistics64(machPort, SystemB.HOST_VM_INFO, vmStats64, new IntByReference(vmStats64.size() / SystemB.INT_SIZE));
assertEquals(ret, 0);
// Nonnegative
assertTrue(vmStats64.free_count >= 0);
}
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class Netapi32Util method getUsers.
/**
* Get the names of users on a computer.
* @param serverName Name of the computer.
* @return An array of users.
*/
public static User[] getUsers(String serverName) {
PointerByReference bufptr = new PointerByReference();
IntByReference entriesRead = new IntByReference();
IntByReference totalEntries = new IntByReference();
try {
int rc = Netapi32.INSTANCE.NetUserEnum(serverName, 1, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesRead, totalEntries, null);
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
throw new Win32Exception(rc);
}
LMAccess.USER_INFO_1 user = new LMAccess.USER_INFO_1(bufptr.getValue());
LMAccess.USER_INFO_1[] users = (LMAccess.USER_INFO_1[]) user.toArray(entriesRead.getValue());
ArrayList<User> result = new ArrayList<User>();
for (LMAccess.USER_INFO_1 lu : users) {
User auser = new User();
if (lu.usri1_name != null) {
auser.name = lu.usri1_name.toString();
}
result.add(auser);
}
return result.toArray(new User[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.ptr.IntByReference in project jna by java-native-access.
the class WininetUtil method getCache.
/**
* Helper function for traversing wininet's cache and returning all entries.
* <br>
* Some entries are cookies, some entries are history items, and some are
* actual files.<br>
*
* @return A map of cache URL => local file (or URL => empty string for
* cookie and history entries)
*/
public static Map<String, String> getCache() {
List<INTERNET_CACHE_ENTRY_INFO> items = new ArrayList<Wininet.INTERNET_CACHE_ENTRY_INFO>();
HANDLE cacheHandle = null;
Win32Exception we = null;
int lastError = 0;
// return
Map<String, String> cacheItems = new LinkedHashMap<String, String>();
try {
IntByReference size = new IntByReference();
// for every entry, we call the API twice:
// once to get the size into the IntByReference
// then again to get the actual item
cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size);
lastError = Native.getLastError();
// if there's nothing in the cache, we're done.
if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
return cacheItems;
} else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(lastError);
}
INTERNET_CACHE_ENTRY_INFO entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, entry, size);
if (cacheHandle == null) {
throw new Win32Exception(Native.getLastError());
}
items.add(entry);
while (true) {
size = new IntByReference();
// for every entry, we call the API twice:
// once to get the size into the IntByReference
// then again to get the actual item
boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, null, size);
if (!result) {
lastError = Native.getLastError();
if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
break;
} else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(lastError);
}
}
entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, entry, size);
if (!result) {
lastError = Native.getLastError();
if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
break;
} else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(lastError);
}
}
items.add(entry);
}
for (INTERNET_CACHE_ENTRY_INFO item : items) {
cacheItems.put(item.lpszSourceUrlName.getWideString(0), item.lpszLocalFileName == null ? "" : item.lpszLocalFileName.getWideString(0));
}
} catch (Win32Exception e) {
we = e;
} finally {
if (cacheHandle != null) {
if (!Wininet.INSTANCE.FindCloseUrlCache(cacheHandle)) {
if (we != null) {
Win32Exception e = new Win32Exception(Native.getLastError());
e.addSuppressed(we);
we = e;
}
}
}
}
if (we != null) {
throw we;
}
return cacheItems;
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class WinspoolUtil method getPrinterInfo1.
public static PRINTER_INFO_1[] getPrinterInfo1() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 1, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_1[0];
}
PRINTER_INFO_1 pPrinterEnum = new PRINTER_INFO_1(pcbNeeded.getValue());
if (!Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 1, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
pPrinterEnum.read();
return (PRINTER_INFO_1[]) pPrinterEnum.toArray(pcReturned.getValue());
}
use of com.sun.jna.ptr.IntByReference 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);
}
}
}
}
Aggregations