Search in sources :

Example 16 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE 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 17 with SIZE

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

the class WininetTest method testFindFirstUrlCacheEntry.

@Test
public void testFindFirstUrlCacheEntry() {
    IntByReference size = new IntByReference();
    HANDLE cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size);
    int lastError = Native.getLastError();
    // ERROR_INSUFFICIENT_BUFFER is returned when there are items in the cache
    // ERROR_NO_MORE_ITEMS is returned when the cache is empty.
    // Both are acceptable for a mapping test where the state of the cache would be unknown.
    assertTrue("GetLastError should have returned ERROR_INSUFFICIENT_BUFFER or ERROR_NO_MORE_ITEMS.", lastError == WinError.ERROR_INSUFFICIENT_BUFFER || lastError == WinError.ERROR_NO_MORE_ITEMS);
    assertNull("FindFirstUrlCacheEntry should have returned null.", cacheHandle);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 18 with SIZE

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

the class MonitorInfoDemo method enumeratePhysicalMonitor.

/**
	 * @param hPhysicalMonitor
	 */
private static void enumeratePhysicalMonitor(HANDLE hPhysicalMonitor) {
    MC_DISPLAY_TECHNOLOGY_TYPE.ByReference techType = new MC_DISPLAY_TECHNOLOGY_TYPE.ByReference();
    Dxva2.INSTANCE.GetMonitorTechnologyType(hPhysicalMonitor, techType);
    System.out.println("TECHTYPE: " + techType.getValue());
    DWORDByReference temps = new DWORDByReference();
    DWORDByReference caps = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorCapabilities(hPhysicalMonitor, caps, temps);
    System.out.println("CAPS " + EnumUtils.setFromInteger(caps.getValue().intValue(), HighLevelMonitorConfigurationAPI.MC_CAPS.class));
    System.out.println("Temps " + temps.getValue());
    // Brightness
    DWORDByReference pdwMinimumBrightness = new DWORDByReference();
    DWORDByReference pdwCurrentBrightness = new DWORDByReference();
    DWORDByReference pdwMaximumBrightness = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
    System.out.println("Brightness Min: " + pdwMinimumBrightness.getValue());
    System.out.println("Brightness Current: " + pdwCurrentBrightness.getValue());
    System.out.println("Brightness Max: " + pdwMaximumBrightness.getValue());
    // Contrast
    DWORDByReference pdwMinimumContrast = new DWORDByReference();
    DWORDByReference pdwCurrentContrast = new DWORDByReference();
    DWORDByReference pdwMaximumContrast = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorContrast(hPhysicalMonitor, pdwMinimumContrast, pdwCurrentContrast, pdwMaximumContrast);
    System.out.println("Contrast Min: " + pdwMinimumContrast.getValue());
    System.out.println("Contrast Current: " + pdwCurrentContrast.getValue());
    System.out.println("Contrast Max: " + pdwMaximumContrast.getValue());
    // Temperature
    MC_COLOR_TEMPERATURE.ByReference pctCurrentColorTemperature = new MC_COLOR_TEMPERATURE.ByReference();
    Dxva2.INSTANCE.GetMonitorColorTemperature(hPhysicalMonitor, pctCurrentColorTemperature);
    System.out.println("Current Temp: " + pctCurrentColorTemperature.getValue());
    // Capabilities string
    DWORDByReference pdwCapabilitiesStringLengthInCharacters = new DWORDByReference();
    Dxva2.INSTANCE.GetCapabilitiesStringLength(hPhysicalMonitor, pdwCapabilitiesStringLengthInCharacters);
    DWORD capStrLen = pdwCapabilitiesStringLengthInCharacters.getValue();
    LPSTR pszASCIICapabilitiesString = new LPSTR(new Memory(capStrLen.intValue()));
    Dxva2.INSTANCE.CapabilitiesRequestAndCapabilitiesReply(hPhysicalMonitor, pszASCIICapabilitiesString, capStrLen);
    System.out.println("Cap-String:" + new String(pszASCIICapabilitiesString.getPointer().getString(0)));
    // Position
    MC_POSITION_TYPE ptPositionType = MC_POSITION_TYPE.MC_HORIZONTAL_POSITION;
    DWORDByReference pdwMinimumPosition = new DWORDByReference();
    DWORDByReference pdwCurrentPosition = new DWORDByReference();
    DWORDByReference pdwMaximumPosition = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorDisplayAreaPosition(hPhysicalMonitor, ptPositionType, pdwMinimumPosition, pdwCurrentPosition, pdwMaximumPosition);
    System.out.println("Position (horz) Min: " + pdwMinimumPosition.getValue());
    System.out.println("Position (horz) Current: " + pdwCurrentPosition.getValue());
    System.out.println("Position (horz) Max: " + pdwMaximumPosition.getValue());
    // Size
    MC_SIZE_TYPE ptSizeType = MC_SIZE_TYPE.MC_WIDTH;
    DWORDByReference pdwMinimumSize = new DWORDByReference();
    DWORDByReference pdwCurrentSize = new DWORDByReference();
    DWORDByReference pdwMaximumSize = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorDisplayAreaSize(hPhysicalMonitor, ptSizeType, pdwMinimumSize, pdwCurrentSize, pdwMaximumSize);
    System.out.println("Width Min: " + pdwMinimumSize.getValue());
    System.out.println("Width Current: " + pdwCurrentSize.getValue());
    System.out.println("Width Max: " + pdwMaximumSize.getValue());
    // Gain
    MC_GAIN_TYPE ptGainType = MC_GAIN_TYPE.MC_RED_GAIN;
    DWORDByReference pdwMinimumGain = new DWORDByReference();
    DWORDByReference pdwCurrentGain = new DWORDByReference();
    DWORDByReference pdwMaximumGain = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorRedGreenOrBlueGain(hPhysicalMonitor, ptGainType, pdwMinimumGain, pdwCurrentGain, pdwMaximumGain);
    System.out.println("Red Gain Min: " + pdwMinimumSize.getValue());
    System.out.println("Red Gain Current: " + pdwCurrentSize.getValue());
    System.out.println("Red Gain Max: " + pdwMaximumSize.getValue());
    // Drive
    MC_DRIVE_TYPE ptDriveType = MC_DRIVE_TYPE.MC_RED_DRIVE;
    DWORDByReference pdwMinimumDrive = new DWORDByReference();
    DWORDByReference pdwCurrentDrive = new DWORDByReference();
    DWORDByReference pdwMaximumDrive = new DWORDByReference();
    Dxva2.INSTANCE.GetMonitorRedGreenOrBlueDrive(hPhysicalMonitor, ptDriveType, pdwMinimumDrive, pdwCurrentDrive, pdwMaximumDrive);
    System.out.println("Red Drive Min: " + pdwMinimumSize.getValue());
    System.out.println("Red Drive Current: " + pdwCurrentSize.getValue());
    System.out.println("Red Drive Max: " + pdwMaximumSize.getValue());
    // Timing Report
    MC_TIMING_REPORT pmtrMonitorTimingReport = new MC_TIMING_REPORT();
    Dxva2.INSTANCE.GetTimingReport(hPhysicalMonitor, pmtrMonitorTimingReport);
    System.out.println("HorizontalFrequencyInHZ " + pmtrMonitorTimingReport.dwHorizontalFrequencyInHZ);
    System.out.println("VerticalFrequencyInHZ " + pmtrMonitorTimingReport.dwVerticalFrequencyInHZ);
    System.out.println("--------------------------------------");
}
Also used : HighLevelMonitorConfigurationAPI(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) MC_GAIN_TYPE(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_GAIN_TYPE) Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) MC_SIZE_TYPE(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_SIZE_TYPE) MC_DISPLAY_TECHNOLOGY_TYPE(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_DISPLAY_TECHNOLOGY_TYPE) MC_POSITION_TYPE(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_POSITION_TYPE) MC_COLOR_TEMPERATURE(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_COLOR_TEMPERATURE) MC_TIMING_REPORT(com.sun.jna.platform.win32.LowLevelMonitorConfigurationAPI.MC_TIMING_REPORT) LPSTR(com.sun.jna.platform.win32.WTypes.LPSTR) MC_DRIVE_TYPE(com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_DRIVE_TYPE)

Example 19 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE 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 20 with SIZE

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

the class Advapi32Util method getTokenGroups.

/**
	 * This function returns the groups associated with a security token, such
	 * as a user token.
	 *
	 * @param hToken
	 *            Token.
	 * @return Token groups.
	 */
public static Account[] getTokenGroups(HANDLE hToken) {
    // get token group information size
    IntByReference tokenInformationLength = new IntByReference();
    if (Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, null, 0, tokenInformationLength)) {
        throw new RuntimeException("Expected GetTokenInformation to fail with ERROR_INSUFFICIENT_BUFFER");
    }
    int rc = Kernel32.INSTANCE.GetLastError();
    if (rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
        throw new Win32Exception(rc);
    }
    // get token group information
    WinNT.TOKEN_GROUPS groups = new WinNT.TOKEN_GROUPS(tokenInformationLength.getValue());
    if (!Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, groups, tokenInformationLength.getValue(), tokenInformationLength)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    ArrayList<Account> userGroups = new ArrayList<Account>();
    // make array of names
    for (SID_AND_ATTRIBUTES sidAndAttribute : groups.getGroups()) {
        Account group = null;
        try {
            group = Advapi32Util.getAccountBySid(sidAndAttribute.Sid);
        } catch (Exception e) {
            group = new Account();
            group.sid = sidAndAttribute.Sid.getBytes();
            group.sidString = Advapi32Util.convertSidToStringSid(sidAndAttribute.Sid);
            group.name = group.sidString;
            group.fqn = group.sidString;
            group.accountType = SID_NAME_USE.SidTypeGroup;
        }
        userGroups.add(group);
    }
    return userGroups.toArray(new Account[0]);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) SID_AND_ATTRIBUTES(com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES) IOException(java.io.IOException)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)12 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)6 Pointer (com.sun.jna.Pointer)5 Test (org.junit.Test)5 Memory (com.sun.jna.Memory)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 HWND (com.sun.jna.platform.win32.WinDef.HWND)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 WString (com.sun.jna.WString)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)2 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)2 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)2 BITMAPINFO (com.sun.jna.platform.win32.WinGDI.BITMAPINFO)2 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)2 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)1 COMException (com.sun.jna.platform.win32.COM.COMException)1 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)1 ObjectFactory (com.sun.jna.platform.win32.COM.util.ObjectFactory)1