Search in sources :

Example 71 with IntByReference

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);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) VMStatistics64(com.sun.jna.platform.mac.SystemB.VMStatistics64) VMStatistics(com.sun.jna.platform.mac.SystemB.VMStatistics)

Example 72 with IntByReference

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);
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) ArrayList(java.util.ArrayList)

Example 73 with IntByReference

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;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) INTERNET_CACHE_ENTRY_INFO(com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) LinkedHashMap(java.util.LinkedHashMap)

Example 74 with IntByReference

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());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PRINTER_INFO_1(com.sun.jna.platform.win32.Winspool.PRINTER_INFO_1)

Example 75 with IntByReference

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);
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) GROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0) LOCALGROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0) PointerByReference(com.sun.jna.ptr.PointerByReference) ArrayList(java.util.ArrayList)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)199 PointerByReference (com.sun.jna.ptr.PointerByReference)38 Memory (com.sun.jna.Memory)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)26 File (java.io.File)19 Pointer (com.sun.jna.Pointer)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 PSID (com.sun.jna.platform.win32.WinNT.PSID)13 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)11 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)11 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)7 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)7 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)6 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)5