Search in sources :

Example 71 with Win32Exception

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

the class Kernel32Test method testLoadLibraryEx.

public void testLoadLibraryEx() {
    String winDir = Kernel32Util.getEnvironmentVariable("WINDIR");
    assertNotNull("No WINDIR value returned", winDir);
    assertTrue("Specified WINDIR does not exist: " + winDir, new File(winDir).exists());
    HMODULE hModule = null;
    try {
        hModule = Kernel32.INSTANCE.LoadLibraryEx(new File(winDir, "explorer.exe").getAbsolutePath(), null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
        if (hModule == null) {
            throw new Win32Exception(Native.getLastError());
        }
        assertNotNull("hModule should not be null.", hModule);
    } finally {
        if (hModule != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(hModule)) {
                throw new Win32Exception(Native.getLastError());
            }
        }
    }
}
Also used : HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) File(java.io.File)

Example 72 with Win32Exception

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

the class WininetTest method testFindNextUrlCacheEntry.

@Test
public void testFindNextUrlCacheEntry() {
    HANDLE cacheHandle = null;
    try {
        IntByReference size = new IntByReference();
        int lastError = 0;
        // 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();
        assertNull("FindFirstUrlCacheEntry should have returned null.", cacheHandle);
        // just to ensure the mapping gets tested
        if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
            boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(null, null, size);
            lastError = Native.getLastError();
            assertFalse("FindNextUrlCacheEntry should have returned false.", result);
            assertEquals("GetLastError should have returned ERROR_INVALID_PARAMETER.", WinError.ERROR_INVALID_PARAMETER, lastError);
            return;
        }
        assertEquals("GetLastError should have returned ERROR_INSUFFICIENT_BUFFER.", WinError.ERROR_INSUFFICIENT_BUFFER, lastError);
        INTERNET_CACHE_ENTRY_INFO entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, entry, size);
        lastError = Native.getLastError();
        assertNotNull("FindFirstUrlCacheEntry should not have returned null.", cacheHandle);
        assertEquals("GetLastError should have returned ERROR_SUCCESS.", WinError.ERROR_SUCCESS, lastError);
        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);
        lastError = Native.getLastError();
        assertFalse("FindNextUrlCacheEntry should have returned false.", result);
        assertEquals("GetLastError should have returned ERROR_INSUFFICIENT_BUFFER.", WinError.ERROR_INSUFFICIENT_BUFFER, lastError);
        entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
        result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, entry, size);
        lastError = Native.getLastError();
        assertTrue("FindNextUrlCacheEntry should have returned true.", result);
        assertEquals("GetLastError should have returned ERROR_SUCCESS.", WinError.ERROR_SUCCESS, lastError);
    } finally {
        if (cacheHandle != null) {
            if (!Wininet.INSTANCE.FindCloseUrlCache(cacheHandle)) {
                throw new Win32Exception(Native.getLastError());
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) INTERNET_CACHE_ENTRY_INFO(com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 73 with Win32Exception

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

the class WevtapiTest method testEvtCreateBookmark.

public void testEvtCreateBookmark() throws Exception {
    EVT_HANDLE queryHandle = null;
    EVT_HANDLE contextHandle = null;
    File testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample1.evtx").toURI());
    StringBuilder sb = new StringBuilder();
    try {
        queryHandle = Wevtapi.INSTANCE.EvtQuery(null, testEvtx.getPath(), null, Winevt.EVT_QUERY_FLAGS.EvtQueryFilePath);
        // test EvtCreateBookmark
        EVT_HANDLE hBookmark = Wevtapi.INSTANCE.EvtCreateBookmark("<BookmarkList><Bookmark Channel='" + testEvtx.getAbsolutePath() + "' RecordId='" + 11 + "' IsCurrent='true'/></BookmarkList>");
        if (hBookmark == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // test EvtSeek
        if (!Wevtapi.INSTANCE.EvtSeek(queryHandle, 0L, hBookmark, 0, Winevt.EVT_SEEK_FLAGS.EvtSeekRelativeToBookmark)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        String[] targets = { "Event/System/EventRecordID" };
        contextHandle = Wevtapi.INSTANCE.EvtCreateRenderContext(targets.length, targets, Winevt.EVT_RENDER_CONTEXT_FLAGS.EvtRenderContextValues);
        int eventArraySize = 10;
        int evtNextTimeout = 1000;
        int arrayIndex = 1;
        Memory buff;
        IntByReference propertyCount = new IntByReference();
        Winevt.EVT_VARIANT evtVariant = new Winevt.EVT_VARIANT();
        EVT_HANDLE[] eventArray = new EVT_HANDLE[eventArraySize];
        IntByReference returned = new IntByReference();
        while (Wevtapi.INSTANCE.EvtNext(queryHandle, eventArraySize, eventArray, evtNextTimeout, 0, returned)) {
            for (int i = 0; i < returned.getValue(); i++) {
                EVT_HANDLE evtHandle = eventArray[i];
                try {
                    buff = WevtapiUtil.EvtRender(contextHandle, eventArray[i], Winevt.EVT_RENDER_FLAGS.EvtRenderEventValues, propertyCount);
                    useMemory(evtVariant, buff, 0);
                    assertThat("EventRecordID", (Long) evtVariant.getValue(), is((long) arrayIndex * eventArraySize + i + 1));
                    sb.append(evtVariant.getValue());
                    // test EvtUpdateBookmark
                    if (!Wevtapi.INSTANCE.EvtUpdateBookmark(hBookmark, eventArray[i])) {
                        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
                    }
                } finally {
                    if (eventArray[i] != null) {
                        Wevtapi.INSTANCE.EvtClose(eventArray[i]);
                    }
                }
            }
            arrayIndex++;
        }
        if (Kernel32.INSTANCE.GetLastError() != WinError.ERROR_SUCCESS && Kernel32.INSTANCE.GetLastError() != WinError.ERROR_NO_MORE_ITEMS) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        buff = WevtapiUtil.EvtRender(null, hBookmark, Winevt.EVT_RENDER_FLAGS.EvtRenderBookmark, propertyCount);
        assertThat(buff.getWideString(0), is("<BookmarkList>\r\n  <Bookmark Channel='" + testEvtx.getAbsolutePath() + "' RecordId='" + 20 + "' IsCurrent='true'/>\r\n</BookmarkList>"));
        assertThat(sb.length() > 0, is(true));
    } finally {
        if (queryHandle != null) {
            Wevtapi.INSTANCE.EvtClose(queryHandle);
        }
        if (contextHandle != null) {
            Wevtapi.INSTANCE.EvtClose(contextHandle);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) File(java.io.File)

Example 74 with Win32Exception

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

the class WevtapiTest method testEvtGetEventInfo.

public void testEvtGetEventInfo() throws Exception {
    EVT_HANDLE queryHandle = null;
    EVT_HANDLE contextHandle = null;
    File testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample1.evtx").toURI());
    StringBuilder sb = new StringBuilder();
    try {
        queryHandle = Wevtapi.INSTANCE.EvtQuery(null, testEvtx.getPath(), null, Winevt.EVT_QUERY_FLAGS.EvtQueryFilePath);
        String[] targets = { "Event/System/EventRecordID" };
        contextHandle = Wevtapi.INSTANCE.EvtCreateRenderContext(targets.length, targets, Winevt.EVT_RENDER_CONTEXT_FLAGS.EvtRenderContextValues);
        int eventArraySize = 10;
        int evtNextTimeout = 1000;
        Memory buff = new Memory(1024);
        Winevt.EVT_VARIANT evtVariant = new Winevt.EVT_VARIANT();
        EVT_HANDLE[] eventArray = new EVT_HANDLE[eventArraySize];
        IntByReference buffUsed = new IntByReference();
        IntByReference returned = new IntByReference();
        while (Wevtapi.INSTANCE.EvtNext(queryHandle, eventArraySize, eventArray, evtNextTimeout, 0, returned)) {
            for (int i = 0; i < returned.getValue(); i++) {
                try {
                    if (!Wevtapi.INSTANCE.EvtGetEventInfo(eventArray[i], Winevt.EVT_EVENT_PROPERTY_ID.EvtEventPath, (int) buff.size(), buff, buffUsed)) {
                        if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_INSUFFICIENT_BUFFER) {
                            buff = new Memory(buffUsed.getValue());
                            if (!Wevtapi.INSTANCE.EvtGetEventInfo(eventArray[i], Winevt.EVT_EVENT_PROPERTY_ID.EvtEventPath, (int) buff.size(), buff, buffUsed)) {
                                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
                            }
                        }
                    }
                    useMemory(evtVariant, buff, 0);
                    assertThat("Evtx Path", (String) evtVariant.getValue(), is(testEvtx.getAbsolutePath()));
                    sb.append((String) evtVariant.getValue());
                } finally {
                    if (eventArray[i] != null) {
                        Wevtapi.INSTANCE.EvtClose(eventArray[i]);
                    }
                }
            }
        }
        if (Kernel32.INSTANCE.GetLastError() != WinError.ERROR_SUCCESS && Kernel32.INSTANCE.GetLastError() != WinError.ERROR_NO_MORE_ITEMS) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        assertThat(sb.length() > 0, is(true));
    } finally {
        if (queryHandle != null) {
            Wevtapi.INSTANCE.EvtClose(queryHandle);
        }
        if (contextHandle != null) {
            Wevtapi.INSTANCE.EvtClose(contextHandle);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) File(java.io.File)

Example 75 with Win32Exception

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

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