Search in sources :

Example 26 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Kernel32Util method getModules.

/**
     * Returns all the executable modules for a given process ID.<br>
     *
     * @param processID
     *            The process ID to get executable modules for
     * @return All the modules in the process.
     */
public static List<Tlhelp32.MODULEENTRY32W> getModules(int processID) {
    HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(processID));
    if (snapshot == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception we = null;
    try {
        Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
        if (!Kernel32.INSTANCE.Module32FirstW(snapshot, first)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        List<Tlhelp32.MODULEENTRY32W> modules = new ArrayList<Tlhelp32.MODULEENTRY32W>();
        modules.add(first);
        Tlhelp32.MODULEENTRY32W next = new Tlhelp32.MODULEENTRY32W();
        while (Kernel32.INSTANCE.Module32NextW(snapshot, next)) {
            modules.add(next);
            next = new Tlhelp32.MODULEENTRY32W();
        }
        int lastError = Kernel32.INSTANCE.GetLastError();
        // or if something went wrong.
        if (lastError != W32Errors.ERROR_SUCCESS && lastError != W32Errors.ERROR_NO_MORE_FILES) {
            throw new Win32Exception(lastError);
        }
        return modules;
    } catch (Win32Exception e) {
        we = e;
        // re-throw so finally block is executed
        throw we;
    } finally {
        try {
            closeHandle(snapshot);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 27 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception 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 28 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception 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 29 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception 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)

Example 30 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Netapi32Util method getUserInfo.

public static UserInfo getUserInfo(String accountName, String domainName) {
    PointerByReference bufptr = new PointerByReference();
    int rc = -1;
    try {
        rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short) 23, bufptr);
        if (rc == LMErr.NERR_Success) {
            USER_INFO_23 info_23 = new USER_INFO_23(bufptr.getValue());
            UserInfo userInfo = new UserInfo();
            if (info_23.usri23_comment != null) {
                userInfo.comment = info_23.usri23_comment.toString();
            }
            userInfo.flags = info_23.usri23_flags;
            if (info_23.usri23_full_name != null) {
                userInfo.fullName = info_23.usri23_full_name.toString();
            }
            if (info_23.usri23_name != null) {
                userInfo.name = info_23.usri23_name.toString();
            }
            if (info_23.usri23_user_sid != null) {
                userInfo.sidString = Advapi32Util.convertSidToStringSid(info_23.usri23_user_sid);
            }
            userInfo.sid = info_23.usri23_user_sid;
            return userInfo;
        } else {
            throw new Win32Exception(rc);
        }
    } finally {
        if (bufptr.getValue() != Pointer.NULL) {
            Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
        }
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) USER_INFO_23(com.sun.jna.platform.win32.LMAccess.USER_INFO_23)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)35 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)18 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Memory (com.sun.jna.Memory)15 PointerByReference (com.sun.jna.ptr.PointerByReference)11 ArrayList (java.util.ArrayList)11 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Pointer (com.sun.jna.Pointer)6 File (java.io.File)6 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)5 Test (org.junit.Test)5 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)4 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)3 LOCALGROUP_INFO_1 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_INFO_1)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 DATA_BLOB (com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2