Search in sources :

Example 26 with DWORD

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

the class Kernel32Test method testGetPrivateProfileString.

public final void testGetPrivateProfileString() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileString", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    writer.println("[Section]");
    writer.println("existingKey = ABC");
    writer.close();
    final char[] buffer = new char[8];
    DWORD len = Kernel32.INSTANCE.GetPrivateProfileString("Section", "existingKey", "DEF", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(3, len.intValue());
    assertEquals("ABC", Native.toString(buffer));
    len = Kernel32.INSTANCE.GetPrivateProfileString("Section", "missingKey", "DEF", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(3, len.intValue());
    assertEquals("DEF", Native.toString(buffer));
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 27 with DWORD

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

the class Kernel32Test method testModule32NextW.

public void testModule32NextW() {
    HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(Kernel32.INSTANCE.GetCurrentProcessId()));
    if (snapshot == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception we = null;
    Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
    try {
        if (!Kernel32.INSTANCE.Module32NextW(snapshot, first)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // not sure if this will be run against java.exe or javaw.exe but this
        // check tests both
        assertTrue("The first module in the current process should be java.exe or javaw.exe", first.szModule().startsWith("java"));
        assertEquals("The process ID of the module ID should be our process ID", Kernel32.INSTANCE.GetCurrentProcessId(), first.th32ProcessID.intValue());
    } catch (Win32Exception e) {
        we = e;
        // re-throw so finally block is executed
        throw we;
    } finally {
        try {
            Kernel32Util.closeHandle(snapshot);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 28 with DWORD

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

the class EnumMoniker method iterator.

@Override
public Iterator<IDispatch> iterator() {
    return new Iterator<IDispatch>() {

        @Override
        public boolean hasNext() {
            return null != EnumMoniker.this.rawNext;
        }

        @Override
        public IDispatch next() {
            assert COMUtils.comIsInitialized() : "COM not initialized";
            final Moniker moniker = EnumMoniker.this.rawNext;
            final PointerByReference ppunkObject = new PointerByReference();
            WinNT.HRESULT hr = EnumMoniker.this.rawRot.GetObject(moniker.getPointer(), ppunkObject);
            COMUtils.checkRC(hr);
            // To assist debug, can use the following code
            // PointerByReference ppbc = new
            // PointerByReference();
            // Ole32.INSTANCE.CreateBindCtx(new DWORD(), ppbc);
            //
            // BSTRByReference ppszDisplayName = new
            // BSTRByReference();
            // hr = moniker.GetDisplayName(ppbc.getValue(),
            // moniker.getPointer(), ppszDisplayName);
            // COMUtils.checkRC(hr);
            // String name = ppszDisplayName.getString();
            // Ole32.INSTANCE.CoTaskMemFree(ppszDisplayName.getPointer().getPointer(0));
            // TODO: Can we assume that the object is an
            // IDispatch ?
            // Unknown unk = new
            // Unknown(ppunkObject.getValue());
            Dispatch dispatch = new Dispatch(ppunkObject.getValue());
            EnumMoniker.this.cacheNext();
            IDispatch d = EnumMoniker.this.factory.createProxy(IDispatch.class, dispatch);
            //must release a COM Ref, GetObject returns a pointer with +1
            int n = dispatch.Release();
            return d;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove");
        }
    };
}
Also used : IEnumMoniker(com.sun.jna.platform.win32.COM.IEnumMoniker) Moniker(com.sun.jna.platform.win32.COM.Moniker) PointerByReference(com.sun.jna.ptr.PointerByReference) Iterator(java.util.Iterator) WinNT(com.sun.jna.platform.win32.WinNT) Dispatch(com.sun.jna.platform.win32.COM.Dispatch)

Example 29 with DWORD

use of com.sun.jna.platform.win32.WinDef.DWORD 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 30 with DWORD

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

the class Shell32Util method getFolderPath.

/**
	 * Get a special folder path.
	 * @param hwnd
	 *  Parent window.
	 * @param nFolder
	 *  Folder CSLID.
	 * @param dwFlags
	 *  Flags.
	 * @return
	 *  Special folder.
	 */
public static String getFolderPath(HWND hwnd, int nFolder, DWORD dwFlags) {
    char[] pszPath = new char[WinDef.MAX_PATH];
    HRESULT hr = Shell32.INSTANCE.SHGetFolderPath(hwnd, nFolder, null, dwFlags, pszPath);
    if (!hr.equals(W32Errors.S_OK)) {
        throw new Win32Exception(hr);
    }
    return Native.toString(pszPath);
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Aggregations

DWORD (com.sun.jna.platform.win32.WinDef.DWORD)56 PointerByReference (com.sun.jna.ptr.PointerByReference)23 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 File (java.io.File)17 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)16 Test (org.junit.Test)15 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)11 Memory (com.sun.jna.Memory)9 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)9 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)7 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 UINT_PTR (com.sun.jna.platform.win32.WinDef.UINT_PTR)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 IntByReference (com.sun.jna.ptr.IntByReference)5 APPBARDATA (com.sun.jna.platform.win32.ShellAPI.APPBARDATA)4 WinNT (com.sun.jna.platform.win32.WinNT)4 Pointer (com.sun.jna.Pointer)3 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)3 BufferedWriter (java.io.BufferedWriter)3