Solved

Getting Java Canvas Window Handle Through JNI

Posted on 2003-12-04
10
2,089 Views
Last Modified: 2013-11-23
Hi to All,

I am trying to get the Java Canvas window handle using JAWT structure defined in "jawt.h" file. I am using jdk1.4, but it is throwing exception when I am calling native method. I have defined one native method "initialize" in my java class which is extending Canvas and calling this native method through JNI interface.

I am pasting the code here for your reference.Please help me. Its very urgent & important as every body in software industry gets the deadlines so I.

"JAWT_Info.h"
-------------------------------------------------------------------------------
#include <jawt_md.h>
#include <assert.h>

#ifndef JAWT_INFO_H
#define JAWT_INFO_H

// Helper class for accessing JAWT Information.
class JAWT_Info
{
private:
    JAWT awt;
    JAWT_DrawingSurface *ds;
    JAWT_DrawingSurfaceInfo *dsi;
    JAWT_Win32DrawingSurfaceInfo *dsi_win;

public:
    JAWT_Info(JNIEnv *env, jobject canvas_ref)
    {
        jboolean result;
        //jint lock;

        // Get the AWT
        awt.version = JAWT_VERSION_1_4;
        cout << "Before GetAWT" << endl;
        result = JAWT_GetAWT(env, &awt);
        cout << "Inside JAWT_Info constructor" << endl;
        assert(result != JNI_FALSE);
        // Get the drawing surface
        ds = awt.GetDrawingSurface(env, canvas_ref);
        if(ds == NULL) {
            cout << "Drawing surface is null" << endl;
            return;
        } else {
            cout << "Get Drawing surface is Successful" << endl;
        }
        // Lock the drawing surface
        //lock = ds->Lock(ds);
        //assert((lock & JAWT_LOCK_ERROR) == 0);

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

        if (dsi == NULL) {
            cout << "Drawing surface info is null" << endl;
            return;
        } else {
            cout << "Get Drawing surface INfo is Successful" << endl;
        }

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

        if (dsi_win == NULL) {
            cout << "win32 Drawing surface info is null" << endl;
            return;
        } else {
            cout << "Get win32 Drawing surface info is Successful" << endl;
        }
    }
   
    HWND getHWND()
    {
        cout << "Control comes inside the getHWND" << endl;
        if(dsi_win == NULL) {
            cout << "Inside getHWND" << endl;
            return NULL;
        } else {
            if (dsi_win->hwnd == NULL) {
                cout << "HWND value is NULL" << endl;
                return NULL;
            } else {
                cout << "getHWND Successful" << endl;
                return dsi_win->hwnd;
            }
        }
    }

    HDC getHDC()
    {
        if(dsi_win == NULL)
            return NULL;
        return dsi_win->hdc;
    }

    virtual ~JAWT_Info()
    {
        if(ds != NULL)
        {
            // Free the drawing surface info
            ds->FreeDrawingSurfaceInfo(dsi);
            // Unlock the drawing surface
            ds->Unlock(ds);
            // Free the drawing surface
            awt.FreeDrawingSurface(ds);
        }
    }
};

#endif


"JNI cpp file"
-------------------------------------------------------------------------------
#include <jni.h>
#include <iostream.h>

#include "ofi_0005fvc_0005fvideo_0005fdisplay_0005fwin.h"
#include "JAWT_Info.h"

JNIEXPORT void JNICALL Java_ofi_1vc_1video_1display_1win_video_1display_1initialize
                                                                (JNIEnv *env,
                                                                 jobject obj)
{
    cout << "initialized" << endl;
    // get the window handle
    JAWT_Info info(env, obj);
    cout << "JAWT_Info initialized" << endl;
    HWND hWnd = (HWND)info.getHWND();

    if(hWnd == NULL) {
        cout << "Inside Native call hwnd is NULL" << endl;
        return;
    } else {
        cout << "Handle returned" << endl;
    }
}


Thanks in advance. Please Help me!!!
0
Comment
Question by:Devmadhu
10 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 9880833
> but it is throwing exception when I am calling native method

  What is the exception?
0
 

Author Comment

by:Devmadhu
ID: 9880958
The Exception is:


Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x807
49E7
Function=[Unknown.]
Library=C:\Program Files\Java\j2re1.4.2_01\bin\client\jvm.dll

NOTE: We are unable to locate the function name symbol for the error
      just occurred. Please refer to release documentation for possible
      reason and solutions.


Current Java thread:
        at display_test.video_display_initialize(Native Method)
        at display_test.main(display_test.java:23)

Dynamic libraries:
0x00400000 - 0x00406000         C:\WINDOWS\system32\java.exe
0x77F50000 - 0x77FF7000         C:\WINDOWS\System32\ntdll.dll
0x77E60000 - 0x77F46000         C:\WINDOWS\system32\kernel32.dll
0x77DD0000 - 0x77E5D000         C:\WINDOWS\system32\ADVAPI32.dll
0x78000000 - 0x78086000         C:\WINDOWS\system32\RPCRT4.dll
0x77C10000 - 0x77C63000         C:\WINDOWS\system32\MSVCRT.dll
0x08000000 - 0x08136000         C:\Program Files\Java\j2re1.4.2_01\bin\client\jv
m.dll
0x77D40000 - 0x77DCC000         C:\WINDOWS\system32\USER32.dll
0x77C70000 - 0x77CB0000         C:\WINDOWS\system32\GDI32.dll
0x76B40000 - 0x76B6C000         C:\WINDOWS\system32\WINMM.dll
0x10000000 - 0x10007000         C:\Program Files\Java\j2re1.4.2_01\bin\hpi.dll
0x00390000 - 0x0039E000         C:\Program Files\Java\j2re1.4.2_01\bin\verify.dl
l
0x003A0000 - 0x003B8000         C:\Program Files\Java\j2re1.4.2_01\bin\java.dll
0x003C0000 - 0x003CD000         C:\Program Files\Java\j2re1.4.2_01\bin\zip.dll
0x02C60000 - 0x02D6F000         C:\Program Files\Java\j2re1.4.2_01\bin\awt.dll
0x73000000 - 0x73023000         C:\WINDOWS\system32\WINSPOOL.DRV
0x76390000 - 0x763AC000         C:\WINDOWS\system32\IMM32.dll
0x771B0000 - 0x772D1000         C:\WINDOWS\system32\ole32.dll
0x5AD70000 - 0x5ADA4000         C:\WINDOWS\System32\uxtheme.dll
0x02DD0000 - 0x02E20000         C:\Program Files\Java\j2re1.4.2_01\bin\fontmanag
er.dll
0x03020000 - 0x03025000         D:\Canvas_window\canvas_wind.dll
0x03030000 - 0x03035000         D:\Canvas_window\jawt.dll
0x03040000 - 0x03050000         C:\WINDOWS\system32\MSVCIRT.dll
0x76C90000 - 0x76CB2000         C:\WINDOWS\system32\imagehlp.dll
0x6D510000 - 0x6D58D000         C:\WINDOWS\system32\DBGHELP.dll
0x77C00000 - 0x77C07000         C:\WINDOWS\system32\VERSION.dll
0x76BF0000 - 0x76BFB000         C:\WINDOWS\system32\PSAPI.DLL

Heap at VM Abort:
Heap
 def new generation   total 576K, used 422K [0x10010000, 0x100b0000, 0x104f0000)

  eden space 512K,  82% used [0x10010000, 0x100799f8, 0x10090000)
  from space 64K,   0% used [0x10090000, 0x10090000, 0x100a0000)
  to   space 64K,   0% used [0x100a0000, 0x100a0000, 0x100b0000)
 tenured generation   total 1408K, used 0K [0x104f0000, 0x10650000, 0x14010000)
   the space 1408K,   0% used [0x104f0000, 0x104f0000, 0x104f0200, 0x10650000)
 compacting perm gen  total 4096K, used 1476K [0x14010000, 0x14410000, 0x1801000
0)
   the space 4096K,  36% used [0x14010000, 0x141813b8, 0x14181400, 0x14410000)

Local Time = Fri Dec 05 14:13:48 2003
Elapsed Time = 0
#
# HotSpot Virtual Machine Error : EXCEPTION_ACCESS_VIOLATION
# Error ID : 4F530E43505002EF
# Please report this error at
# http://java.sun.com/cgi-bin/bugreport.cgi
#
# Java VM: Java HotSpot(TM) Client VM (1.4.2_01-b06 mixed mode)
#
# An error report file has been saved as hs_err_pid900.log.
# Please refer to the file for further information.
#
0
 
LVL 35

Expert Comment

by:girionis
ID: 9880980
It looks like a bug to me. Have you tried it with newer (or older) versions than 1.4?
0
 
LVL 35

Expert Comment

by:girionis
ID: 9880992
 Take a look here:http://www.unidata.ucar.edu/staff/russ/visad/msg17312.html

  Someone claims it is a bug with OpenGL implementation.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Devmadhu
ID: 9881522
Boss I am not using any OpenGL, I simply want to get the Java Canvas Window Handle. I have nothing to do with OpenGL.

0
 
LVL 35

Expert Comment

by:girionis
ID: 9881562
 Still it is more than likely a bug... Look at the error message:

***# Please report this error at**
***# http://java.sun.com/cgi-bin/bugreport.cgi**

  Try to use a different (I suggest a greater) version of java and see what happens.
0
 
LVL 9

Expert Comment

by:doronb
ID: 9883771
I'm not sure about this, so I may be saying something dumb here but I don't think the awt member is initialized anywhere in your code, don't YOU have to do something to initialize it?
0
 

Author Comment

by:Devmadhu
ID: 9886828
Thanks but now I get rid of the problem. Now my code is not throwing exception as I mentioned.

Actually to get the java window handle, we need to add the canvas in a concrete Window like Frame etc and then try to get its handle, because I think Canvas has no existence of its own unless untill it is added in Frame. I was not adding canvas in Frame. Now its fine.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 11688323
Submitted to PAQ with points refunded (125)

DarthMod
Community Support Moderator
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now