gpt4 book ai didi

java - 32 位 JRE 中的 jna 指针

转载 作者:行者123 更新时间:2023-11-29 05:01:12 26 4
gpt4 key购买 nike

我正在使用 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");
}
}

放大倍数.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=313kNative 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+0j  com.sun.jna.Pointer.getInt(J)I+6j  com.sun.jna.Pointer.getValue(JLjava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;+340j  com.sun.jna.Structure.readField(Lcom/sun/jna/Structure$StructField;)Ljava/lang/Object;+168j  com.sun.jna.Structure.read()V+82j  com.sun.jna.CallbackReference$DefaultCallbackProxy.convertArgument(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+330j  com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+95j  com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2v  ~StubRoutines::call_stubV  [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  0x00b80012C  [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+0j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341j  com.sun.proxy.$Proxy5.MagSetWindowSource(Lcom/sun/jna/platform/win32/WinDef$HWND;Lcom/sun/jna/platform/win32/WinDef$RECT;)Z+20j  org.redfire.screen.ScreenShare$CaptureScreen.run()V+674j  java.lang.Thread.run()V+11v  ~StubRoutines::call_stubV  [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+0j  com.sun.jna.Pointer.getInt(J)I+6j  com.sun.jna.Pointer.getValue(JLjava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;+340j  com.sun.jna.Structure.readField(Lcom/sun/jna/Structure$StructField;)Ljava/lang/Object;+168j  com.sun.jna.Structure.read()V+82j  com.sun.jna.CallbackReference$DefaultCallbackProxy.convertArgument(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+330j  com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+95j  com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2v  ~StubRoutines::call_stubj  com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341j  com.sun.proxy.$Proxy5.MagSetWindowSource(Lcom/sun/jna/platform/win32/WinDef$HWND;Lcom/sun/jna/platform/win32/WinDef$RECT;)Z+20j  org.redfire.screen.ScreenShare$CaptureScreen.run()V+674j  java.lang.Thread.run()V+11v  ~StubRoutines::call_stub

如何解决这个问题?我怎样才能使它在 32 位 JRE 上工作?谢谢!

最佳答案

首先,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);
}

您需要将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 位上给了我一个空白图像。这似乎不正确。

关于java - 32 位 JRE 中的 jna 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32044727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com