Search in sources :

Example 76 with PointerByReference

use of com.sun.jna.ptr.PointerByReference 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 77 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Util method getSecurityDescriptorForObject.

/**
     * Get a self relative security descriptor for the given object type. The value is returned in Memory
     * @param absoluteObjectPath
     *         A pointer to a null-terminated string that specifies the name of the object
     *         from which to retrieve security information. For descriptions of the string
     *         formats for the different object types, see SE_OBJECT_TYPE in
     *         {@link AccCtrl.SE_OBJECT_TYPE}
     * @param objectType
     *         Object type referred to by the path. See  {@link AccCtrl.SE_OBJECT_TYPE} for valid definitions.
     * @param getSACL
     *         Get SACL of the object. See {@link Advapi32#GetNamedSecurityInfo} for process privilege requirements in getting the SACL.
     * @return Memory containing the self relative security descriptor
     */
public static Memory getSecurityDescriptorForObject(final String absoluteObjectPath, int objectType, boolean getSACL) {
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | (getSACL ? SACL_SECURITY_INFORMATION : 0);
    PointerByReference ppSecurityDescriptor = new PointerByReference();
    int lastError = Advapi32.INSTANCE.GetNamedSecurityInfo(absoluteObjectPath, objectType, infoType, null, null, null, null, ppSecurityDescriptor);
    if (lastError != 0) {
        throw new Win32Exception(lastError);
    }
    int nLength = Advapi32.INSTANCE.GetSecurityDescriptorLength(ppSecurityDescriptor.getValue());
    Memory memory = new Memory(nLength);
    Pointer secValue = ppSecurityDescriptor.getValue();
    try {
        byte[] data = secValue.getByteArray(0, nLength);
        memory.write(0, data, 0, nLength);
        return memory;
    } finally {
        Kernel32Util.freeLocalMemory(secValue);
    }
}
Also used : Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer)

Example 78 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Util method convertSidToStringSid.

/**
	 * Convert a security identifier (SID) to a string format suitable for
	 * display, storage, or transmission.
	 *
	 * @param sid
	 *            SID bytes.
	 * @return String SID.
	 */
public static String convertSidToStringSid(PSID sid) {
    PointerByReference stringSid = new PointerByReference();
    if (!Advapi32.INSTANCE.ConvertSidToStringSid(sid, stringSid)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Pointer ptr = stringSid.getValue();
    try {
        return ptr.getWideString(0);
    } finally {
        Kernel32Util.freeLocalMemory(ptr);
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer)

Example 79 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Util method getAccountBySid.

/**
	 * Get the account by SID.
	 *
	 * @param systemName
	 *            Name of the system.
	 * @param sid
	 *            SID.
	 * @return Account.
	 */
public static Account getAccountBySid(String systemName, PSID sid) {
    IntByReference cchName = new IntByReference();
    IntByReference cchDomainName = new IntByReference();
    PointerByReference peUse = new PointerByReference();
    if (Advapi32.INSTANCE.LookupAccountSid(null, sid, null, cchName, null, cchDomainName, peUse)) {
        throw new RuntimeException("LookupAccountSidW was expected to fail with ERROR_INSUFFICIENT_BUFFER");
    }
    int rc = Kernel32.INSTANCE.GetLastError();
    if (cchName.getValue() == 0 || rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
        throw new Win32Exception(rc);
    }
    char[] domainName = new char[cchDomainName.getValue()];
    char[] name = new char[cchName.getValue()];
    if (!Advapi32.INSTANCE.LookupAccountSid(null, sid, name, cchName, domainName, cchDomainName, peUse)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Account account = new Account();
    account.accountType = peUse.getPointer().getInt(0);
    account.name = Native.toString(name);
    if (cchDomainName.getValue() > 0) {
        account.domain = Native.toString(domainName);
        account.fqn = account.domain + "\\" + account.name;
    } else {
        account.fqn = account.name;
    }
    account.sid = sid.getBytes();
    account.sidString = convertSidToStringSid(sid);
    return account;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 80 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Util method getAccountByName.

/**
	 * Retrieves a security identifier (SID) for a given account.
	 *
	 * @param systemName
	 *            Name of the system.
	 * @param accountName
	 *            Account name.
	 * @return A structure containing the account SID.
	 */
public static Account getAccountByName(String systemName, String accountName) {
    IntByReference pSid = new IntByReference(0);
    IntByReference cchDomainName = new IntByReference(0);
    PointerByReference peUse = new PointerByReference();
    if (Advapi32.INSTANCE.LookupAccountName(systemName, accountName, null, pSid, null, cchDomainName, peUse)) {
        throw new RuntimeException("LookupAccountNameW was expected to fail with ERROR_INSUFFICIENT_BUFFER");
    }
    int rc = Kernel32.INSTANCE.GetLastError();
    if (pSid.getValue() == 0 || rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
        throw new Win32Exception(rc);
    }
    Memory sidMemory = new Memory(pSid.getValue());
    PSID result = new PSID(sidMemory);
    char[] referencedDomainName = new char[cchDomainName.getValue() + 1];
    if (!Advapi32.INSTANCE.LookupAccountName(systemName, accountName, result, pSid, referencedDomainName, cchDomainName, peUse)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Account account = new Account();
    account.accountType = peUse.getPointer().getInt(0);
    account.name = accountName;
    String[] accountNamePartsBs = accountName.split("\\\\", 2);
    String[] accountNamePartsAt = accountName.split("@", 2);
    if (accountNamePartsBs.length == 2) {
        account.name = accountNamePartsBs[1];
    } else if (accountNamePartsAt.length == 2) {
        account.name = accountNamePartsAt[0];
    } else {
        account.name = accountName;
    }
    if (cchDomainName.getValue() > 0) {
        account.domain = Native.toString(referencedDomainName);
        account.fqn = account.domain + "\\" + account.name;
    } else {
        account.fqn = account.name;
    }
    account.sid = result.getBytes();
    account.sidString = convertSidToStringSid(new PSID(account.sid));
    return account;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)128 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)54 IntByReference (com.sun.jna.ptr.IntByReference)35 Pointer (com.sun.jna.Pointer)20 Test (org.junit.Test)19 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)18 REFIID (com.sun.jna.platform.win32.Guid.REFIID)13 File (java.io.File)13 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)10 PSID (com.sun.jna.platform.win32.WinNT.PSID)10 Memory (com.sun.jna.Memory)8 WString (com.sun.jna.WString)8 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)7 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 WinNT (com.sun.jna.platform.win32.WinNT)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IID (com.sun.jna.platform.win32.Guid.IID)6 ArrayList (java.util.ArrayList)6 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)5 UINT (com.sun.jna.platform.win32.WinDef.UINT)5