当前位置: 首页 > 知识库问答 >
问题:

32位JRE中的jna指针

莫翰藻
2023-03-14

我正在使用jna调用Java中的放大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");
    }
}

华丽.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位)中运行相同的代码,我会得到以下错误。

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10002a21, pid=5552, tid=4884
#
# JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15)
# Java VM: Java HotSpot(TM) Client VM (24.80-b11 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [jna6797525900717560222.dll+0x2a21]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Stack: [0x05c60000,0x05cb0000],  sp=0x05cae4d0,  free space=313k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
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  [magnification.dll+0x5434]
C  [magnification.dll+0x5a2c]
C  [magnification.dll+0x5f5d]
C  [magnification.dll+0x612a]
C  [magnification.dll+0x28ab]
C  [USER32.dll+0x162fa]
C  [USER32.dll+0x16d3a]
C  [USER32.dll+0x1965e]
C  [USER32.dll+0x196c5]
C  [magnification.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 frames: (J=compiled Java code, j=interpreted, Vv=VM code)
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上实现这一功能?谢谢!

共有1个答案

冉伯寅
2023-03-14

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

其次,返回类型是一个简单的boolean(您不应该为此使用boolean)。

第三,回调的参数是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);
}

您需要向MagImageHeader添加ByValue支持;并对结构的最后一个元素使用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上的显示,因此它是新的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);
}

在32bit或64bit模式下运行的最终产品是一个名为this is about as minimal的文件。

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

 类似资料:
  • 问题内容: 我已经使用Java一段时间了,而我典型的设置新开发机的习惯要求从Oracle站点下载并安装最新的JDK。 今天这引发了一个不寻常的问题, 回想起来,我已经安装了之前的两个版本,并且很高兴将普通的工具链插入(Eclipse)。在我的日常编程中,我不会回想起曾经因为使用64位JRE(或为此目的而针对64位JRE)而不得不以其他方式进行更改或思考的事情。 根据我对64位和32位的理解- 确实

  • 我已经在Java工作了8个月。目前,我正在使用JNA加载一个第三方DLL,它在32bit WinXP和32bit Win7机器上运行良好。 我将使用procexp来查找是否缺少任何依赖项。我会相应地更新这篇文章。 请提供您的专家建议。提前感谢!

  • 第三方DLL有一个函数,该函数需要一个指向结构的指针作为参数: 它将手指指纹(通常为4个)的“拍打”图像分割成单独的指纹(手指的文件名)。 SrapInfo的定义是: C中的示例片段: 根据JNA常见问题解答,在我的情况下,我应该使用“结构”: 所以,我用Java/JNA做了这样的映射: 但是使用这种方法,我得到了错误: 我还尝试了: < li >指针引用而不是SlapInfo。ByReferen

  • 我试图使用shobjidl_core. h库来运行代码,改变一个特定的监视器的壁纸,特别是我需要运行这两个函数:GetMonitor orDevicePathAt,SetWallfile。 现在,当我运行以下代码时: 我收到一个错误,提示“无法加载库'shobjidl'” 我已经尝试了任何变体的“舒比德”,但仍然找不到一个有效的。从我对JNA的理解来看,它需要一个包含库的dll,但是我找不到任何与

  • 问题内容: 我的电脑正在使用Windows 7 64位。但是将要部署我的jsp Web应用程序的服务器是32位。 我需要在PC上安装32位JDK / JRE才能进行开发吗?我正在使用Eclipse。 非常感谢你。 问题答案: 您绝对不需要安装32位JRE即可进行开发。您构建的Java代码不会跟踪您的64位。(我假设您没有使用JNI,这会使事情变得有些复杂。) 不过,您 可能 需要安装32位JRE进