use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class PsapiTest method testEnumProcessModules.
@Test
public void testEnumProcessModules() {
HANDLE me = null;
Win32Exception we = null;
try {
me = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_ALL_ACCESS, false, Kernel32.INSTANCE.GetCurrentProcessId());
assertTrue("Handle to my process should not be null", me != null);
List<HMODULE> list = new LinkedList<HMODULE>();
HMODULE[] lphModule = new HMODULE[100 * 4];
IntByReference lpcbNeeded = new IntByReference();
if (!Psapi.INSTANCE.EnumProcessModules(me, lphModule, lphModule.length, lpcbNeeded)) {
throw new Win32Exception(Native.getLastError());
}
for (int i = 0; i < lpcbNeeded.getValue() / 4; i++) {
list.add(lphModule[i]);
}
assertTrue("List should have at least 1 item in it.", list.size() > 0);
} catch (Win32Exception e) {
we = e;
// re-throw to invoke finally block
throw we;
} finally {
try {
Kernel32Util.closeHandle(me);
} catch (Win32Exception e) {
if (we == null) {
we = e;
} else {
we.addSuppressed(e);
}
}
if (we != null) {
throw we;
}
}
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class PsapiTest method testGetModuleFileNameEx.
@Test
public void testGetModuleFileNameEx() {
final JFrame w = new JFrame();
try {
w.setVisible(true);
final String searchSubStr = "\\bin\\java";
final HWND hwnd = new HWND(Native.getComponentPointer(w));
final IntByReference pid = new IntByReference();
User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid);
final HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pid.getValue());
if (process == null)
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
// check ANSI function
final byte[] filePathAnsi = new byte[1025];
int length = Psapi.INSTANCE.GetModuleFileNameExA(process, null, filePathAnsi, filePathAnsi.length - 1);
if (length == 0)
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathAnsi), Native.toString(filePathAnsi).toLowerCase().contains(searchSubStr));
// check Unicode function
final char[] filePathUnicode = new char[1025];
length = Psapi.INSTANCE.GetModuleFileNameExW(process, null, filePathUnicode, filePathUnicode.length - 1);
if (length == 0)
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathUnicode), Native.toString(filePathUnicode).toLowerCase().contains(searchSubStr));
// check default function
final int memAllocSize = 1025 * Native.WCHAR_SIZE;
final Memory filePathDefault = new Memory(memAllocSize);
length = Psapi.INSTANCE.GetModuleFileNameEx(process, null, filePathDefault, (memAllocSize / Native.WCHAR_SIZE) - 1);
if (length == 0)
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathDefault.getCharArray(0, memAllocSize / Native.WCHAR_SIZE)), Native.toString(filePathDefault.getCharArray(0, memAllocSize / Native.WCHAR_SIZE)).toLowerCase().contains(searchSubStr));
} finally {
w.dispose();
}
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class Rasapi32Test method testRasGetEntryProperties.
public void testRasGetEntryProperties() {
RASENTRY.ByReference rasEntry = new RASENTRY.ByReference();
IntByReference lpdwEntryInfoSize = new IntByReference(rasEntry.size());
int err = Rasapi32.INSTANCE.RasGetEntryProperties(null, "TEST", rasEntry, lpdwEntryInfoSize, null, null);
assertEquals(623, err);
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class WinspoolTest method testEnumPrinters_4.
public void testEnumPrinters_4() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
// if there are no printers installed, EnumPrinters will succeed with zero items returned
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 4, null, 0, pcbNeeded, pcReturned);
assertTrue(pcReturned.getValue() == 0);
if (pcbNeeded.getValue() > 0) {
PRINTER_INFO_4 pPrinterEnum = new PRINTER_INFO_4(pcbNeeded.getValue());
assertTrue(Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 4, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned));
assertTrue(pcReturned.getValue() >= 0);
PRINTER_INFO_4[] printerInfo = (PRINTER_INFO_4[]) pPrinterEnum.toArray(pcReturned.getValue());
for (PRINTER_INFO_4 pi : printerInfo) {
assertTrue(pi.pPrinterName == null || pi.pPrinterName.length() >= 0);
}
}
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class WinspoolTest method testEnumPrinters_2.
public void testEnumPrinters_2() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
// if there are no printers installed, EnumPrinters will succeed with zero items returned
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 2, null, 0, pcbNeeded, pcReturned);
assertTrue(pcReturned.getValue() == 0);
if (pcbNeeded.getValue() > 0) {
PRINTER_INFO_2 pPrinterEnum = new PRINTER_INFO_2(pcbNeeded.getValue());
assertTrue(Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 2, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned));
assertTrue(pcReturned.getValue() >= 0);
PRINTER_INFO_2[] printerInfo = (PRINTER_INFO_2[]) pPrinterEnum.toArray(pcReturned.getValue());
for (PRINTER_INFO_2 pi : printerInfo) {
assertTrue(pi.pPrinterName == null || pi.pPrinterName.length() >= 0);
}
}
}
Aggregations