java - jna Pointer in 32 bit JRE -
i using jna call magnification api functions in java.
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; }
my code
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 desktop"); system.exit(0); } if(!user32extra.instance.getwindowrect(desktop, desktoprect)){ system.err.println("cannot 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 , error "+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 , error "+native.getlasterror()); system.exit(0); } rect sourcerect= new rect(); if(!user32extra.instance.getwindowrect(desktop, sourcerect)){ system.err.println("cannot 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); } } }
if magsetwindowsource
function called magimagescalingcallback
function called.
the problem if run code jre7(64 bit) works fine. if run same code in jre7(32 bit), getting following error.
# # fatal error has been detected java runtime environment: # # exception_access_violation (0xc0000005) @ 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 write core dump. minidumps not enabled default on client versions of windows # # if submit bug report, please visit: # http://bugreport.java.com/bugreport/crash.jsp # crash happened outside java virtual machine in native code. # see problematic frame report 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
how solve issue? how can make work on 32-bit jre? thanks!
firstly, magimagescalingcallback
defined winapi
function, means have define callback being child of stdcallcallback
.
secondly, return type simple boolean (you should not use boolean this).
thirdly, parameters of callback magimageheader
, , not magimageheader *
, , rect
not rect *
, means needs passed value; callback declaration needs changed so:
i added rectbyvalue
class, containing following:
import com.sun.jna.platform.win32.windef; import com.sun.jna.structure; public class rectbyvalue extends windef.rect implements structure.byvalue {}
i changed magimagescalingcallback
class following:
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); }
you need add byvalue
support magimageheader
; , use size_t
last element of structure:
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() { } }
i changed screen size match display on screenskip
it's new bufferedimage(2160, 1440...
instead of 1366, 786
, , changed callback code creator to:
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); }
the end product of running in 32bit or 64bit mode file called minimal can think achieve want.
.... huh ... gives me blank image on 32bit. seems incorrect.
Comments
Post a Comment