Getting Java Canvas Window Handle Through JNI

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!!!
Who is Participating?
DarthModConnect With a Mentor Commented:
Submitted to PAQ with points refunded (125)

Community Support Moderator
> but it is throwing exception when I am calling native method

  What is the exception?
DevmadhuAuthor Commented:
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(

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
# 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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

It looks like a bug to me. Have you tried it with newer (or older) versions than 1.4?
 Take a look here:

  Someone claims it is a bug with OpenGL implementation.
DevmadhuAuthor Commented:
Boss I am not using any OpenGL, I simply want to get the Java Canvas Window Handle. I have nothing to do with OpenGL.

 Still it is more than likely a bug... Look at the error message:

***# Please report this error at**

  Try to use a different (I suggest a greater) version of java and see what happens.
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?
DevmadhuAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.