Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Acquire windowHandle of given Swing/AWT window in JNI dll code?  (Read 2694 times)
0 Members and 1 Guest are viewing this topic.
Offline whome

Junior Devvie




Carte Noir Java


« Posted 2005-11-28 19:21:55 »

Acquire windowHandle of given Swing/AWT window in JNI dll code?

If I create the following dll (delphi pascal pseudo-code, dont mind naming errors). And winapi method is just an example but should give you the idea.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// delphi jni.dll helper method
function doSometing(javaWindow: HWND): boolean;
begin
  Result := IsWindowVisible(javawindow); // call WinAPI method with given window handle
end;
. . .
// somewhere in java
public void doSomething() {
   // any method to acquire native HWND handle and give it as a parameter?
   boolean b = doSomething(myFrame);
   System.out.println(b);
}


Can I call it from Java and give a swing/awt window handle as a parameter. Or should I give frame instance as a parameter and then somehow use it inside the native code?
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2005-11-28 19:40:48 »

The later.. use a Frame and get the HWND in the native code.  JAWT (part of JNI) is built for this.  Take a look at the jawt header files from the JDK and google for some examples.

Offline whome

Junior Devvie




Carte Noir Java


« Reply #2 - Posted 2005-11-28 22:34:18 »

Thx, used JAWT keyword and Mr Google gave few helpful links
http://forums.java.sun.com/thread.jspa?threadID=453581&messageID=2145897
http://today.java.net/pub/a/today/2003/12/08/swing.html?page=last&x-maxdepth=0


Most code is c/c++ but I try to study it and convert to ObjectPascal.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #3 - Posted 2005-11-29 03:31:47 »

One thing to note about the code sample in the dorum post at java.sun.com...  They mess around with finding the java home folder to load the AWT DLL.  It can be done MUCH easier than that.  Just use System.loadLibrary("jawt"); on the Java side.

Offline whome

Junior Devvie




Carte Noir Java


« Reply #4 - Posted 2005-11-29 08:16:42 »

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;

    // Get the AWT
    awt.version = JAWT_VERSION_1_3;
    result = JAWT_GetAWT(env, &awt);
    assert(result != JNI_FALSE);
    // Get the drawing surface
    ds = awt.GetDrawingSurface(env, component);
    if(ds == NULL)
        return;
    // Lock the drawing surface
    lock = ds->Lock(ds);
    assert((lock & JAWT_LOCK_ERROR) == 0);

    // Get the drawing surface info
    dsi = ds->GetDrawingSurfaceInfo(ds);

    // Get the platform-specific drawing info
    dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;

    FlashWindow(dsi_win->hwnd,bool);

    // Free the drawing surface info
    ds->FreeDrawingSurfaceInfo(dsi);
    // Unlock the drawing surface
    ds->Unlock(ds);
    // Free the drawing surface
    awt.FreeDrawingSurface(ds);
}

/////////////////////////////
// JAVA SIDE
/////////////////////////////
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 {
    // load the native dll
    static {
        System.loadLibrary("WindowUtil");
    }
    // the actual native method
    public native void flash(Component c, boolean bool);

    /**
    <p>A utility method that flashes the window on and off. This method
    will not block to wait for the flashing.
     Instead it will spawn an thread to do the flashing.
    </p>

     @param frame The JFrame to be flashed
     @param intratime The amount of time between the on and off states of a single flash
     @param intertime The amount of time between different flashes
     @param count The number of times to flash the window
    */

    public void flash(final JFrame frame, final int intratime, final int intertime, final int count) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    // flash on and off each time
                    for(int i=0; i<count; i++) {
                        flash(frame,true);
                        Thread.sleep(intratime);
                        flash(frame,true);
                        Thread.sleep(intertime);
                    }
                    // turn the flash off
                    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();
    }

}

Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #5 - Posted 2005-11-29 13:01:50 »

The canvas method is the class/object containing the native method.  If the native method is not static it is equivalent to "this" in Java.
The component would need to be heavyweight to have a window handle on the native side, otherwise the heavyweight parent container (e.g Frame) would be used.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (101 views)
2014-11-29 21:32:03

toopeicgaming1999 (160 views)
2014-11-26 15:22:04

toopeicgaming1999 (163 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!