Search in sources :

Example 16 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project processing by processing.

the class WindowsRegistry method setIntValue.

/**
   * Writes an int value.
   *
   * @return true on success
   * @param rootKey root key
   * @param subKeyName key name
   * @param name value name
   * @param value value
   */
public static boolean setIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, int value) {
    //Advapi32 advapi32;
    //int handle;
    //byte[] data;
    byte[] data = new byte[4];
    data[0] = (byte) (value & 0xff);
    data[1] = (byte) ((value >> 8) & 0xff);
    data[2] = (byte) ((value >> 16) & 0xff);
    data[3] = (byte) ((value >> 24) & 0xff);
    Advapi32 advapi32 = Advapi32.INSTANCE;
    HKEY handle = openKey(rootKey, subKeyName, WinNT.KEY_READ | WinNT.KEY_WRITE);
    boolean ret = false;
    //if(handle != 0) {
    if (handle != null) {
        if (advapi32.RegSetValueEx(handle, name, 0, WinNT.REG_DWORD, data, data.length) == WinError.ERROR_SUCCESS) {
            ret = true;
        }
        advapi32.RegCloseKey(handle);
    }
    return ret;
}
Also used : Advapi32(com.sun.jna.platform.win32.Advapi32) HKEY(com.sun.jna.platform.win32.WinReg.HKEY)

Example 17 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project processing by processing.

the class WindowsRegistry method setStringValue.

/**
   * Writes a String value.
   *
   * @param rootKey root key
   * @param subKeyName key name
   * @param name value name
   * @param value value
   * @throws java.io.UnsupportedEncodingException on error
   * @return true on success
   */
public static boolean setStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, String value) throws UnsupportedEncodingException {
    //int handle;
    //byte[] data;
    // appears to be Java 1.6 syntax, removing [fry]
    //data = Arrays.copyOf(value.getBytes("UTF-16LE"), value.length() * 2 + 2);
    byte[] data = new byte[value.length() * 2 + 2];
    byte[] src = value.getBytes("UTF-16LE");
    System.arraycopy(src, 0, data, 0, src.length);
    Advapi32 advapi32 = Advapi32.INSTANCE;
    HKEY handle = openKey(rootKey, subKeyName, WinNT.KEY_READ | WinNT.KEY_WRITE);
    boolean ret = false;
    //if(handle != 0) {
    if (handle != null) {
        if (advapi32.RegSetValueEx(handle, name, 0, WinNT.REG_SZ, data, data.length) == WinError.ERROR_SUCCESS) {
            ret = true;
        }
        advapi32.RegCloseKey(handle);
    }
    return ret;
}
Also used : Advapi32(com.sun.jna.platform.win32.Advapi32) HKEY(com.sun.jna.platform.win32.WinReg.HKEY)

Example 18 with BYTE

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

the class AlphaMaskDemo method updateW32.

private void updateW32(boolean a, boolean i) {
    User32 user = User32.INSTANCE;
    GDI32 gdi = GDI32.INSTANCE;
    HWND hWnd = null;
    if (!alphaWindow.isDisplayable()) {
        alphaWindow.pack();
        hWnd = getHwnd(alphaWindow);
        int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
        flags |= WinUser.WS_EX_LAYERED;
        user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags);
        Window parent = alphaWindow.getOwner();
        Point where = parent.getLocationOnScreen();
        where.translate(parent.getWidth(), 0);
        alphaWindow.setLocation(where);
    } else {
        hWnd = getHwnd(alphaWindow);
    }
    if (i) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        HDC screenDC = user.GetDC(null);
        HDC memDC = gdi.CreateCompatibleDC(screenDC);
        HBITMAP hBitmap = null;
        HANDLE oldBitmap = null;
        try {
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            BITMAPINFO bmi = new BITMAPINFO();
            bmi.bmiHeader.biWidth = w;
            bmi.bmiHeader.biHeight = h;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = 32;
            bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
            bmi.bmiHeader.biSizeImage = w * h * 4;
            PointerByReference ppbits = new PointerByReference();
            hBitmap = gdi.CreateDIBSection(memDC, bmi, WinGDI.DIB_RGB_COLORS, ppbits, null, 0);
            oldBitmap = gdi.SelectObject(memDC, hBitmap);
            Pointer pbits = ppbits.getValue();
            Raster raster = buf.getData();
            int[] pixel = new int[4];
            int[] bits = new int[w * h];
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    raster.getPixel(x, h - y - 1, pixel);
                    int alpha = (pixel[3] & 0xFF) << 24;
                    int red = (pixel[2] & 0xFF);
                    int green = (pixel[1] & 0xFF) << 8;
                    int blue = (pixel[0] & 0xFF) << 16;
                    bits[x + y * w] = alpha | red | green | blue;
                }
            }
            pbits.write(0, bits, 0, bits.length);
            SIZE size = new SIZE();
            size.cx = w;
            size.cy = h;
            POINT loc = new POINT();
            loc.x = alphaWindow.getX();
            loc.y = alphaWindow.getY();
            POINT srcLoc = new POINT();
            BLENDFUNCTION blend = new BLENDFUNCTION();
            blend.SourceConstantAlpha = (byte) (alpha * 255);
            blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
            user.UpdateLayeredWindow(hWnd, screenDC, loc, size, memDC, srcLoc, 0, blend, WinUser.ULW_ALPHA);
        } finally {
            user.ReleaseDC(null, screenDC);
            if (hBitmap != null) {
                gdi.SelectObject(memDC, oldBitmap);
                gdi.DeleteObject(hBitmap);
            }
            gdi.DeleteDC(memDC);
        }
    } else if (a) {
        BLENDFUNCTION blend = new BLENDFUNCTION();
        blend.SourceConstantAlpha = (byte) (alpha * 255);
        blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
        user.UpdateLayeredWindow(hWnd, null, null, null, null, null, 0, blend, WinUser.ULW_ALPHA);
    }
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) GDI32(com.sun.jna.platform.win32.GDI32) BLENDFUNCTION(com.sun.jna.platform.win32.WinUser.BLENDFUNCTION) HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) Raster(java.awt.image.Raster) SIZE(com.sun.jna.platform.win32.WinUser.SIZE) Pointer(com.sun.jna.Pointer) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) PointerByReference(com.sun.jna.ptr.PointerByReference) User32(com.sun.jna.platform.win32.User32) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) POINT(com.sun.jna.platform.win32.WinDef.POINT) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 19 with BYTE

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

the class Advapi32Util method registrySetBinaryValue.

/**
	 * Set a binary value in registry.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param name
	 *            Value name.
	 * @param data
	 *            Data to write to registry.
	 */
public static void registrySetBinaryValue(HKEY root, String keyPath, String name, byte[] data) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        registrySetBinaryValue(phkKey.getValue(), name, data);
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 20 with BYTE

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

the class Kernel32Util method getResource.

/**
     * Gets the specified resource out of the specified executable file
     *
     * @param path
     *            The path to the executable file
     * @param type
     *            The type of the resource (either a type name or type ID is
     *            allowed)
     * @param name
     *            The name or ID of the resource
     * @return The resource bytes, or null if no such resource exists.
     * @throws IllegalStateException if the call to LockResource fails
     */
public static byte[] getResource(String path, String type, String name) {
    HMODULE target = Kernel32.INSTANCE.LoadLibraryEx(path, null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
    if (target == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception err = null;
    Pointer start = null;
    int length = 0;
    byte[] results = null;
    try {
        Pointer t = null;
        try {
            t = new Pointer(Long.parseLong(type));
        } catch (NumberFormatException e) {
            t = new Memory(Native.WCHAR_SIZE * (type.length() + 1));
            t.setWideString(0, type);
        }
        Pointer n = null;
        try {
            n = new Pointer(Long.parseLong(name));
        } catch (NumberFormatException e) {
            n = new Memory(Native.WCHAR_SIZE * (name.length() + 1));
            n.setWideString(0, name);
        }
        HRSRC hrsrc = Kernel32.INSTANCE.FindResource(target, n, t);
        if (hrsrc == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // according to MSDN, on 32 bit Windows or newer, calling FreeResource() is not necessary - and in fact does nothing but return false.
        HANDLE loaded = Kernel32.INSTANCE.LoadResource(target, hrsrc);
        if (loaded == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        length = Kernel32.INSTANCE.SizeofResource(target, hrsrc);
        if (length == 0) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // MSDN: It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.
        // MSDN does not say that LockResource sets GetLastError
        start = Kernel32.INSTANCE.LockResource(loaded);
        if (start == null) {
            throw new IllegalStateException("LockResource returned null.");
        }
        // have to capture it into a byte array before you free the library, otherwise bad things happen.
        results = start.getByteArray(0, length);
    } catch (Win32Exception we) {
        err = we;
    } finally {
        // from what I can tell on MSDN, the only thing that needs cleanup on this is the HMODULE from LoadLibrary
        if (target != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(target)) {
                Win32Exception we = new Win32Exception(Kernel32.INSTANCE.GetLastError());
                if (err != null) {
                    we.addSuppressed(err);
                }
                throw we;
            }
        }
    }
    if (err != null) {
        throw err;
    }
    return results;
}
Also used : Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)12 Pointer (com.sun.jna.Pointer)8 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 PointerByReference (com.sun.jna.ptr.PointerByReference)5 Memory (com.sun.jna.Memory)4 GUID (com.sun.jna.platform.win32.Guid.GUID)4 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)4 File (java.io.File)4 Date (java.util.Date)4 Test (org.junit.Test)4 DATE (com.sun.jna.platform.win32.OaIdl.DATE)3 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)3 FE_EXPORT_FUNC (com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 LONG (com.sun.jna.platform.win32.WinDef.LONG)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3