32 位 JRE 中的 jna 指针

Posted

技术标签:

【中文标题】32 位 JRE 中的 jna 指针【英文标题】:jna Pointer in 32 bit JRE 【发布时间】:2015-11-09 18:08:36 【问题描述】:

我正在使用 jna 在 Java 中调用 Magnification api 函数。

MagImageScalingCallback.java

package jna.extra;

import com.sun.jna.Callback;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HRGN;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;

public interface MagImageScalingCallback extends Callback
    public Boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,MAGIMAGEHEADER srcheader, Pointer destdata,MAGIMAGEHEADER destheader,RECT source,RECT clipped,HRGN dirty);

MAGIMAGEHEADER.java

package jna.extra;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.platform.win32.Guid.GUID;


public class MAGIMAGEHEADER extends com.sun.jna.Structure 
    public int width;
    public int height;
    public GUID format;
    public int stride;
    public int offset;
    public int cbsize;

    public List getFieldOrder() 
        return Arrays.asList("width","height","format","stride","offset","cbsize");
    

Magnification.java

package jna.extra;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;


public interface Magnification extends StdCallLibrary 

    Magnification INSTANCE = (Magnification) Native.loadLibrary("magnification", Magnification.class,
                                                W32APIOptions.DEFAULT_OPTIONS);

    public Boolean MagInitialize();

    public boolean MagSetWindowFilterList(HWND hwndMag, DWORD dword, int i,
            HWND[] excludeHWNDs);

    public boolean MagSetWindowSource(HWND hwndMag, RECT sourceRect);

    public void MagGetWindowFilterList(HWND hwndMag, DWORD dword, int i, HWND[] test);

    public boolean MagSetImageScalingCallback(HWND hwndMag,MagImageScalingCallback MagImageScalingCallback);

    public MagImageScalingCallback MagGetImageScalingCallback(HWND hwndMag);


WinGDIExtra.java

package jna.extra;

import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinGDI;

public interface WinGDIExtra extends WinGDI 

    public DWORD SRCCOPY = new DWORD(0x00CC0020);
    public DWORD SRCPAINT = new DWORD(0x00ee0086);
    public DWORD SRCAND = new DWORD(0x008800c6);
    public DWORD SRCINVERT = new DWORD(0x00660046);
    public DWORD SRCERASE = new DWORD(0x00440328);

    public DWORD NOTSRCCOPY = new DWORD(0x00330008);
    public DWORD NOTSRCERASE = new DWORD(0x001100a6);
    public DWORD MERGECOPY = new DWORD(0x00c000ca);
    public DWORD MERGEPAINT = new DWORD(0x00bb0226);

    public DWORD PATCOPY = new DWORD(0x00f00021);
    public DWORD PATPAINT = new DWORD(0x00fb0a09);
    public DWORD PATINVERT = new DWORD(0x005a0049);
    public DWORD DSTINVERT = new DWORD(0x00550009);
    public DWORD WHITENESS = new DWORD(0x00ff0062);
    public DWORD BLACKNESS = new DWORD(0x00000042);
    public DWORD CAPTUREBLT = new DWORD(0x00CC0020 | 0x40000000);
    public DWORD Black = new DWORD(0x00000000);

    public long WS_CHILD = 0x40000000L;
    public long WS_VISIBLE = 0x10000000L;
    public long MS_SHOWMAGNIFIEDCURSOR = 0x0001L; 


    public long WS_EX_TOPMOST = 0x00000008L;
    public long WS_EX_LAYERED = 0x00080000;
    public long WS_EX_TRANSPARENT = 0x00000020L;

    public long WS_CLIPCHILDREN = 0x02000000L;

    public long MW_FILTERMODE_EXCLUDE = 0;


我的代码

package jna.extra;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HRGN;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

import luz.winapi.jna.User32;

public class screenSkip 

    public static void main(String[] args)
        if(!Magnification.INSTANCE.MagInitialize())
            System.out.println("Cannot Intialize Magnification API");
            System.exit(0);
        

        RECT desktopRect= new RECT();
        HWND desktop = User32.INSTANCE.GetDesktopWindow();
        if(desktop==null)
            System.out.println("Problem with Desktop");
            System.exit(0);
        
        if(!User32Extra.INSTANCE.GetWindowRect(desktop, desktopRect))
            System.err.println("Cannot get window rect");
            System.exit(0);
        
        HWND top = User32Extra.INSTANCE.CreateWindowEx(new DWORD(WinGDIExtra.WS_EX_TOPMOST | WinGDIExtra.WS_EX_LAYERED | WinGDIExtra.WS_EX_TRANSPARENT), "#32770", "Parent Window", new DWORD(WinGDIExtra.WS_CLIPCHILDREN), desktopRect.left, desktopRect.top, desktopRect.right-desktopRect.left, desktopRect.bottom-desktopRect.top, desktop, null, null, null);

        if(top==null)
            System.out.println("Problem while creating Parent Window and the error is "+Native.getLastError());
            System.exit(0);
        

        HWND hwndMag=null;
        System.out.println(Native.getLastError());
        hwndMag = User32Extra.INSTANCE.CreateWindowEx(null, "Magnifier", "MagWindow", new DWORD(WinGDIExtra.WS_CHILD | WinGDIExtra.MS_SHOWMAGNIFIEDCURSOR | WinGDIExtra.WS_VISIBLE), desktopRect.left, desktopRect.top, desktopRect.right-desktopRect.left, desktopRect.bottom-desktopRect.top, top, null, Kernel32.INSTANCE.GetModuleHandle(null), null);
        if(hwndMag==null)
            System.err.println("Problem while creating Magnifier Window and the error is "+Native.getLastError());
            System.exit(0);
        

        RECT sourceRect= new RECT();

        if(!User32Extra.INSTANCE.GetWindowRect(desktop, sourceRect))
            System.err.println("Cannot get window rect");
            System.exit(0);
        

        final BufferedImage image = new BufferedImage(1366, 768, BufferedImage.TYPE_INT_RGB);
        if(!Magnification.INSTANCE.MagSetImageScalingCallback(hwndMag,new MagImageScalingCallback() 

            public Boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,
                    MAGIMAGEHEADER srcheader, Pointer destdata,
                    MAGIMAGEHEADER destheader, RECT source, RECT clipped, HRGN dirty) 
                image.setRGB(0, 0, srcheader.width, srcheader.height, srcdata.getIntArray(0, srcheader.width * srcheader.height ), 0, srcheader.width);
                return true;
            
        ))
            System.err.println("Error occured while setting callback");
            System.exit(0);
        
        if (!Magnification.INSTANCE.MagSetWindowSource(hwndMag, sourceRect))
        
            System.err.println("Cannot copy");
            System.exit(0);
        
        try 
            ImageIO.write(image, "JPEG", new File("printed1.jpg"));
         catch (IOException e) 
            e.printStackTrace();
        
        if (!Magnification.INSTANCE.MagSetWindowSource(hwndMag, sourceRect))
        
            System.err.println("Cannot copy");
            System.exit(0);
        
    

如果调用MagSetWindowSource 函数,则调用MagImageScalingCallback 函数。

问题是,如果我使用 jre7(64 位)运行此代码,一切正常。但是如果我在 jre7(32 位)中运行相同的代码,我会收到以下错误。

# # Java 运行时环境检测到一个致命错误: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10002a21, pid=5552, tid=4884 # # JRE 版本:Java(TM) SE 运行时环境 (7.0_80-b15) (build 1.7.0_80-b15) # Java VM:Java HotSpot(TM) Client VM(24.80-b11混合模式,共享windows-x86) # 有问题的框架: #C [jna6797525900717560222.dll+0x2a21] # # 无法写入核心转储。默认情况下,在 Windows 的客户端版本上不启用小型转储 # # 如果您想提交错误报告,请访问: # http://bugreport.java.com/bugreport/crash.jsp # 崩溃发生在 Java 虚拟机之外的本地代码中。 # 查看有问题的框架以了解报告错误的位置。 # 堆栈:[0x05c60000,0x05cb0000],sp=0x05cae4d0,可用空间=313k 本机帧:(J=编译的 Java 代码,j=解释的,Vv=VM 代码,C=本机代码) C [jna6797525900717560222.dll+0x2a21] j com.sun.jna.Pointer._getInt(J)I+0 j com.sun.jna.Pointer.getInt(J)I+6 j com.sun.jna.Pointer.getValue(JLjava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;+340 j com.sun.jna.Structure.readField(Lcom/sun/jna/Structure$StructField;)Ljava/lang/Object;+168 j com.sun.jna.Structure.read()V+82 j com.sun.jna.CallbackReference$DefaultCallbackProxy.convertArgument(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+330 j com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+95 j com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2 v ~StubRoutines::call_stub V [jvm.dll+0x1429aa] V [jvm.dll+0x20743e] V [jvm.dll+0x142a2d] V [jvm.dll+0xcb7b2] V [jvm.dll+0xcd5df] C [jna6797525900717560222.dll+0x9dd2] C [jna6797525900717560222.dll+0xa47f] C [jna6797525900717560222.dll+0xc864] C [jna6797525900717560222.dll+0xcdee] C 0x00b80012 C [放大.dll+0x5434] C [放大.dll+0x5a2c] C [放大.dll+0x5f5d] C [放大.dll+0x612a] C [放大.dll+0x28ab] C [USER32.dll+0x162fa] C [USER32.dll+0x16d3a] C [USER32.dll+0x1965e] C [USER32.dll+0x196c5] C [放大.dll+0x22e2] C [jna6797525900717560222.dll+0xcc77] C [jna6797525900717560222.dll+0xc78a] C [jna6797525900717560222.dll+0x4561] C [jna6797525900717560222.dll+0x4d2e] j com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0 j com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315 j com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214 j com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341 j com.sun.proxy.$Proxy5.MagSetWindowSource(Lcom/sun/jna/platform/win32/WinDef$HWND;Lcom/sun/jna/platform/win32/WinDef$RECT;)Z+20 j org.redfire.screen.ScreenShare$CaptureScreen.run()V+674 j java.lang.Thread.run()V+11 v ~StubRoutines::call_stub V [jvm.dll+0x1429aa] V [jvm.dll+0x20743e] V [jvm.dll+0x142b75] V [jvm.dll+0x142bd7] V [jvm.dll+0xed5cf] V [jvm.dll+0x163c4c] V [jvm.dll+0x1646a7] V [jvm.dll+0x1a92f9] C [msvcr100.dll+0x5c556] C [msvcr100.dll+0x5c600] C [kernel32.dll+0x133aa] C [ntdll.dll+0x39f72] C [ntdll.dll+0x39f45] Java 框架:(J=编译的 Java 代码,j=解释的,Vv=VM 代码) j com.sun.jna.Pointer._getInt(J)I+0 j com.sun.jna.Pointer.getInt(J)I+6 j com.sun.jna.Pointer.getValue(JLjava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;+340 j com.sun.jna.Structure.readField(Lcom/sun/jna/Structure$StructField;)Ljava/lang/Object;+168 j com.sun.jna.Structure.read()V+82 j com.sun.jna.CallbackReference$DefaultCallbackProxy.convertArgument(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+330 j com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+95 j com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2 v ~StubRoutines::call_stub j com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0 j com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315 j com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214 j com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341 j com.sun.proxy.$Proxy5.MagSetWindowSource(Lcom/sun/jna/platform/win32/WinDef$HWND;Lcom/sun/jna/platform/win32/WinDef$RECT;)Z+20 j org.redfire.screen.ScreenShare$CaptureScreen.run()V+674 j java.lang.Thread.run()V+11 v ~StubRoutines::call_stub

如何解决这个问题?如何在 32 位 JRE 上完成这项工作? 谢谢!

【问题讨论】:

我确定我之前已经对您说过,但您只展示了部分代码。为什么不能显示 MCVE?这样我们就不必猜测了。 @DavidHeffernan 更新问题。 没有。请进行网络搜索以了解 MCVE 的含义。您的更新既不是最小的,也不是完整的。可以编译问题中的代码吗? 【参考方案1】:

首先,MagImageScalingCallback 定义为WINAPI 函数,这意味着您必须将回调定义为StdCallCallback 的子级。

其次,返回类型是一个简单的布尔值(你不应该为此使用布尔值)。

第三,回调的参数是MAGIMAGEHEADER,而不是MAGIMAGEHEADER *,并且RECT不是RECT *,也就是说需要传值;所以回调声明需要像这样改变:

我添加了一个RectByValue 类,包含以下内容:

import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.Structure;

public class RectByValue extends WinDef.RECT implements Structure.ByValue 

我用以下内容更改了MagImageScalingCallback 类:

import com.sun.jna.win32.StdCallLibrary;

public interface MagImageScalingCallback extends StdCallLibrary.StdCallCallback 
   public boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,MAGIMAGEHEADER.ByValue srcheader, Pointer destdata,MAGIMAGEHEADER.ByValue destheader,RectByValue source,RectByValue clipped,HRGN dirty);

您需要将ByValue 支持添加到MAGIMAGEHEADER;并且还使用SIZE_T 作为结构的最后一个元素:

import java.util.Arrays;
import java.util.List;

import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;

public class MAGIMAGEHEADER extends Structure 
    public int width;
    public int height;
    public GUID format;
    public int stride;
    public int offset;
    public BaseTSD.SIZE_T cbsize;

    public List getFieldOrder() 
        return Arrays.asList("width","height","format", "stride","offset","cbsize");
    

    public static class ByValue extends MAGIMAGEHEADER implements Structure.ByValue 
        public ByValue() 

        public ByValue(MAGIMAGEHEADER magimageheader) 
            super (magimageheader.getPointer());

            width = magimageheader.width;
            height = magimageheader.height;
            format = magimageheader.format;
            stride = magimageheader.stride;
            offset = magimageheader.offset;
            cbsize = magimageheader.cbsize;
        

        public ByValue(Pointer memory) 
            super(memory);
        
    

    public MAGIMAGEHEADER(Pointer memory) 
        super(memory);
        read();
    

    public MAGIMAGEHEADER() 
    

我更改了屏幕大小以匹配我在screenSkip 上的显示,所以它是new BufferedImage(2160, 1440... 而不是1366, 786,并将回调代码创建者更改为:

if(!Magnification.INSTANCE.MagSetImageScalingCallback(hwndMag, new MagImageScalingCallback() 

    public boolean MagImageScalingCallback(HWND hwnd, Pointer srcdata,
            MAGIMAGEHEADER.ByValue srcheader, Pointer destdata,
            MAGIMAGEHEADER.ByValue destheader, RectByValue source, RectByValue clipped, HRGN dirty) 
        image.setRGB(0, 0, srcheader.width, srcheader.height, srcdata.getIntArray(0, srcheader.width * srcheader.height ), 0, srcheader.width);
        return true;
    
))
    System.err.println("Error occured while setting callback");
    System.exit(0);

在 32 位或 64 位模式下运行的最终产品是一个名为 这大约是我认为可以实现您想要的最少的。

....呵呵...它给了我一个 32 位的空白图像。这似乎不正确。

【讨论】:

谢谢!我找不到任何StdCallLibrary.StdCallback 接口。所以,我使用了StdCallLibrary.StdCallCallback。但我遇到了同样的错误。 @Mahesh 请为所有相关类(例如 MAGIMAGEHEADER)添加您的定义,因为您可能定义错误。 @Mahesh 需要进行一些更改。我会在答案中解释 @Mahesh 我有你的代码,对CreateWindowEx 稍作改动 - 我使用了User32Util.createWindowEx,并添加了一个实现GetWindowRectGetDesktopWindow 的类,只是为了让那些缺失的类正常工作,为 java 7 32bit、java 8 32bit 和 java 8 64bit 生成 .jpg。 @Petesh 你能在答案中添加修改吗?

以上是关于32 位 JRE 中的 jna 指针的主要内容,如果未能解决你的问题,请参考以下文章

JNA 结构和指针映射

使用jna和CreateProcessW时如何获取进程输出

32位或64位系统中的指针算术长短

带有 JNA 的 C 回调使 JRE 崩溃

使用 32 位 JRE 打开 64 位 windows 虚拟键盘

Java Keytool jdk/jre 64/32bit - 有区别吗?