Search in sources :

Example 76 with Kernel32

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

the class User32WindowMessagesTest method hookwinProc.

private HHOOK hookwinProc(HWND hwndToHook) {
    HOOKPROC hookProc = new HOOKPROC() {

        /**
			 * Callback method. cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms644975(v=vs.85).aspx
			 *
			 * nCode [in] : Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the
			 * hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the
			 * message to the CallNextHookEx function without further processing and must return the value returned by
			 * CallNextHookEx. wParam [in] : Specifies whether the message was sent by the current thread. If the
			 * message was sent by the current thread, it is nonzero; otherwise, it is zero. lParam [in] : A pointer to
			 * a CWPSTRUCT structure that contains details about the message.
			 *
			 */
        // used by introspection from jna.
        @SuppressWarnings("unused")
        public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
            if (nCode < 0) {
                return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
            }
            try {
                WinUser.CWPSTRUCT cwp = new WinUser.CWPSTRUCT(new Pointer(lParam.longValue()));
                switch(cwp.message) {
                    case WinUser.WM_USER:
                        {
                            HWND hWndSource = new HWND(new Pointer(cwp.wParam.longValue()));
                            log(cwp.hwnd + " - Received a message from " + hWndSource + " hooked proc : code= " + cwp.wParam + ", value = " + cwp.lParam);
                            assertEqualsForCallbackExecution(MSG_HOOKED_CODE, cwp.wParam.intValue());
                            assertEqualsForCallbackExecution(MSG_HOOKED_VAL, cwp.lParam.intValue());
                            return new LRESULT(0);
                        }
                }
                // Send message to next hook.
                return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
            } catch (Throwable t) {
                t.printStackTrace();
                return new LRESULT(0);
            }
        }
    };
    HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
    int threadtoHook = User32.INSTANCE.GetWindowThreadProcessId(hwndToHook, null);
    // Hook of the wndProc
    return User32.INSTANCE.SetWindowsHookEx(WinUser.WH_CALLWNDPROC, hookProc, hInst, threadtoHook);
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) HINSTANCE(com.sun.jna.platform.win32.WinDef.HINSTANCE) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) HOOKPROC(com.sun.jna.platform.win32.WinUser.HOOKPROC)

Example 77 with Kernel32

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

the class NtDllTest method testNtQuerySetSecurityObjectNoSACL.

public void testNtQuerySetSecurityObjectNoSACL() throws Exception {
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    // create a temp file
    File file = createTempFile();
    String filePath = file.getAbsolutePath();
    HANDLE hFile = WinBase.INVALID_HANDLE_VALUE;
    try {
        hFile = Kernel32.INSTANCE.CreateFile(filePath, WinNT.GENERIC_WRITE | WinNT.WRITE_OWNER | WinNT.WRITE_DAC, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
        assertFalse("Failed to create file handle: " + filePath, WinBase.INVALID_HANDLE_VALUE.equals(hFile));
        int Length = 64 * 1024;
        Memory SecurityDescriptor = new Memory(Length);
        IntByReference LengthNeeded = new IntByReference();
        assertEquals("NtQuerySecurityObject(" + filePath + ")", 0, NtDll.INSTANCE.NtQuerySecurityObject(hFile, infoType, SecurityDescriptor, Length, LengthNeeded));
        assertTrue(LengthNeeded.getValue() > 0);
        assertTrue(LengthNeeded.getValue() < 64 * 1024);
        assertEquals("NtSetSecurityObject(" + filePath + ")", 0, NtDll.INSTANCE.NtSetSecurityObject(hFile, infoType, SecurityDescriptor));
    } finally {
        if (hFile != WinBase.INVALID_HANDLE_VALUE)
            Kernel32.INSTANCE.CloseHandle(hFile);
        file.delete();
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 78 with Kernel32

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

Example 79 with Kernel32

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

the class Kernel32DiskManagementFunctionsTest method testGetDiskFreeSpace.

private void testGetDiskFreeSpace(String lpRootPathName) {
    DWORDByReference lpSectorsPerCluster = new DWORDByReference();
    DWORDByReference lpBytesPerSector = new DWORDByReference();
    DWORDByReference lpNumberOfFreeClusters = new DWORDByReference();
    DWORDByReference lpTotalNumberOfClusters = new DWORDByReference();
    assertCallSucceeded("GetDiskFreeSpace(" + lpRootPathName + ")", Kernel32.INSTANCE.GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters));
    //        System.out.append(getCurrentTestName()).append('[').append(lpRootPathName).println(']');
    //        System.out.append('\t').append("SectorsPerCluster: ").println(lpSectorsPerCluster.getValue());
    //        System.out.append('\t').append("BytesPerSector: ").println(lpBytesPerSector.getValue());
    //        System.out.append('\t').append("NumberOfFreeClusters: ").println(lpNumberOfFreeClusters.getValue());
    //        System.out.append('\t').append("TotalNumberOfClusters: ").println(lpTotalNumberOfClusters.getValue());
    DWORD freeSize = lpNumberOfFreeClusters.getValue();
    assertTrue("No free clusters for " + lpRootPathName, freeSize.longValue() > 0L);
    DWORD totalSize = lpTotalNumberOfClusters.getValue();
    assertTrue("Free clusters (" + freeSize + ") not below total (" + totalSize + ") for " + lpRootPathName, freeSize.longValue() < totalSize.longValue());
}
Also used : DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 80 with Kernel32

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

the class Kernel32Test method testFindFirstFileExFindExInfoBasic.

public void testFindFirstFileExFindExInfoBasic() throws IOException {
    Path tmpDir = Files.createTempDirectory("testFindFirstFileExFindExInfoBasic");
    File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFileExFindExInfoBasic", ".jna").toString());
    Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
    // Add the file name to the search to get just that one entry
    HANDLE hFile = Kernel32.INSTANCE.FindFirstFileEx(tmpDir.toAbsolutePath().toString() + "\\" + tmpFile.getName(), WinBase.FindExInfoBasic, p, WinBase.FindExSearchNameMatch, null, new DWORD(0));
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        // Get data and confirm the 1st name is for the file itself
        WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
        String actualFileName = new String(fd.getFileName());
        actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals(tmpFile.getName()));
        // FindExInfoBasic does not return the short name, so confirm that its empty
        String alternateFileName = fd.getAlternateFileName();
        assertTrue(alternateFileName.isEmpty());
        // No more files in directory
        assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
        assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32.INSTANCE.FindClose(hFile);
        tmpFile.delete();
        Files.delete(tmpDir);
    }
}
Also used : Path(java.nio.file.Path) Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) WIN32_FIND_DATA(com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)74 IntByReference (com.sun.jna.ptr.IntByReference)47 File (java.io.File)33 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)30 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)27 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)26 Memory (com.sun.jna.Memory)25 PointerByReference (com.sun.jna.ptr.PointerByReference)14 Pointer (com.sun.jna.Pointer)12 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Test (org.junit.Test)7 SYSTEMTIME (com.sun.jna.platform.win32.WinBase.SYSTEMTIME)6 ArrayList (java.util.ArrayList)6 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)5 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)5 PSID (com.sun.jna.platform.win32.WinNT.PSID)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 WString (com.sun.jna.WString)4 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 ShortByReference (com.sun.jna.ptr.ShortByReference)4