Getting Java Canvas Window Handle Through JNI

Posted on 2003-12-04
Medium Priority
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.

#include <jawt_md.h>
#include <assert.h>

#ifndef JAWT_INFO_H
#define JAWT_INFO_H

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

    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;
        } 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;
        } 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;
        } 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
            // Unlock the drawing surface
            // Free the drawing surface


"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;
    } else {
        cout << "Handle returned" << endl;

Thanks in advance. Please Help me!!!
Question by:Devmadhu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Expert Comment

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

  What is the exception?

Author Comment

ID: 9880958
The Exception is:

Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x807
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
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
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
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:
 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
   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.
LVL 35

Expert Comment

ID: 9880980
It looks like a bug to me. Have you tried it with newer (or older) versions than 1.4?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 35

Expert Comment

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.

Author Comment

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.

LVL 35

Expert Comment

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.

Expert Comment

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?

Author Comment

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.

Accepted Solution

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

Community Support Moderator

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …
Suggested Courses

719 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