Thats what I looked and wondered is it really necessary to load library that way. Looks a rather fragile trying to find a right jawt.dll version to be loaded.
They use hAWT handle to get pointer to JAWT_GetAWT function. If library is loaded at java side, can I then just call "JAWT_GetAWT(env, &jawt)" function directly in a native code and it should work fine?
javaforum code example:
...clip...
// Get a reference to the AWT DLL
hAWT = LoadLibrary ((LPCTSTR)buf);
if (!hAWT) return 1;
JAWT_GetAWT = (PJAWT_GetAWT)GetProcAddress((HMODULE)hAWT, "_JAWT_GetAWT@8");
// reference to JAWT structure containing native jawt functions
result = JAWT_GetAWT (env, &jawt);
// This call gets the JAWT_DrawingSurface
jds = jawt.GetDrawingSurface (env, canvas);.
...clip...
EDIT:
Ok, I looked at the other today.java.net article and it had I think more clean example. Sources were hidden in a
https://mad-chatter.dev.java.net/ site. It does not load JAWT.dll library either in native or java side. It just loads WindowUtil.dll library and nothing more.
One thing I dont understand about native method arguments;
// the actual native method declaration in java code
public native void flash(Component c, boolean bool);
// use flash method somewhere in java code
flash(frame,true);
// native method declaration
JNIEXPORT void JNICALL Java_org_joshy_jni_WindowUtil_flash(
JNIEnv * env, jobject canvas, jobject component, jboolean bool) { .... }
Now this method has "jobject canvas, jobject component, jboolean bool" arguments. And code uses component argument to get a window handle for given java side frame. But what is canvas argument and where it came from, java side method has only two arguments. And canvas argument is not used in native side?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include <assert.h> #include "jawt_md.h" #include "org_joshy_jni_WindowUtil.h"
JNIEXPORT void JNICALL Java_org_joshy_jni_WindowUtil_flash( JNIEnv * env, jobject canvas, jobject component, jboolean bool) { JAWT awt; JAWT_DrawingSurface* ds; JAWT_DrawingSurfaceInfo* dsi; JAWT_Win32DrawingSurfaceInfo* dsi_win; jboolean result;
jint lock;
awt.version = JAWT_VERSION_1_3; result = JAWT_GetAWT(env, &awt); assert(result != JNI_FALSE); ds = awt.GetDrawingSurface(env, component); if(ds == NULL) return; lock = ds->Lock(ds); assert((lock & JAWT_LOCK_ERROR) == 0);
dsi = ds->GetDrawingSurfaceInfo(ds);
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
FlashWindow(dsi_win->hwnd,bool);
ds->FreeDrawingSurfaceInfo(dsi); ds->Unlock(ds); awt.FreeDrawingSurface(ds); }
package org.joshy.jni; import java.awt.Component; import java.awt.event.*; import javax.swing.*; import java.awt.Canvas; import java.awt.*;
public class WindowUtil extends Canvas { static { System.loadLibrary("WindowUtil"); } public native void flash(Component c, boolean bool);
public void flash(final JFrame frame, final int intratime, final int intertime, final int count) { new Thread(new Runnable() { public void run() { try { for(int i=0; i<count; i++) { flash(frame,true); Thread.sleep(intratime); flash(frame,true); Thread.sleep(intertime); } flash(frame,false); } catch (Exception ex) { System.out.println(ex.getMessage()); } }}).start(); }
public static void main(String[] args) throws Exception { final JFrame frame = new JFrame(); JButton button = new JButton("stuff"); frame.getContentPane().add(button); final WindowUtil winutil = new WindowUtil(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { winutil.flash(frame,750,1500,5); } }); frame.pack(); frame.show(); }
} |