Search in sources :

Example 1 with MODULEINFO

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

the class PsapiTest method testGetModuleInformation.

@Test
public void testGetModuleInformation() {
    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);
        MODULEINFO lpmodinfo = new MODULEINFO();
        if (!Psapi.INSTANCE.GetModuleInformation(me, list.get(0), lpmodinfo, lpmodinfo.size())) {
            throw new Win32Exception(Native.getLastError());
        }
        assertTrue("MODULEINFO.EntryPoint should not be null.", lpmodinfo.EntryPoint != null);
    } 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;
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) LinkedList(java.util.LinkedList) MODULEINFO(com.sun.jna.platform.win32.Psapi.MODULEINFO) Test(org.junit.Test)

Aggregations

MODULEINFO (com.sun.jna.platform.win32.Psapi.MODULEINFO)1 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1