Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interfacing with applet from IE via JNI & Win32

843804May 26 2005
have my Win32 application and IE running a java applet. My
application gets the hWnd of IE and its children hWnd's . Using spy++
you can see this hierarchy as

IEFrame
->WorkerW
--->WorkerW
----->msctls status bar32
----->Shell DocObjectViewer
------->Internet Explorer_Server
--------->JavaPluginControlwindow
----------->java.plugin.viewer.IExplorerEmbeddedFrame
------------->sunawtcanvas
--------------->sunawtcanvas

Does anyone know why MS's IE has two sunawtcanvas's. My guess is that
the first one of these is the sunawtcomponent. Is that true?
Given the sunawtcanvas HWND, can I

----------subclass.cpp------------
int subclass( HWND& hWndSunAwtCanvas2)
{
// jobject graphics;
int iRc=0;
#ifdef _USEJVM
try {
JavaVM *jvm;
jsize mySize;

jint status =
JNI_GetCreatedJavaVMs( &jvm, mySize, &mySize);

if (JNI_ERR==status) {
OutputDebugString("failed in JNI_CreateJavaVM\n");
iRc=-5;
} else {
JNIEnv* env ;
jint status2 =
JNI_GetDefaultJavaVMInitArgs((void**)&env);

if (JNI_ERR==status2) {
OutputDebugString("failed in JNI_GetDefaultJavaVMInitArgs\n");
iRc=-9;
} else {

JAWT awt;
jobject canvas=(jobject) hWndSunAwtCanvas2;
// Get the AWT
awt.version = JAWT_VERSION_1_4;
jboolean result;
result = JAWT_GetAWT(env, &awt);// need to get the awt from IE, not
your local version
if (result == JNI_FALSE){
OutputDebugString("AWT Not Found\n");
iRc=-8;
} else {

JAWT_DrawingSurface* ds;
JavaVMOption options[1];
JavaVMInitArgs vm_args;
//JNIEnv_ myJNIEnv;// need to init this??
memset( &vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 1;
vm_args.options = options;

// Get the drawing surface
ds = awt.GetDrawingSurface(env, canvas);
if (ds == NULL){
OutputDebugString("NULL drawing surface\n");
iRc=-3;
} else {
jint lock;
// Lock the drawing surface
lock = ds->Lock(ds);
if ((lock & JAWT_LOCK_ERROR) != 0){
OutputDebugString("Error locking surface\n");
iRc=-2;
} else {
//try
JAWT_DrawingSurfaceInfo* dsi;
// Get the drawing surface info
dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi == NULL){
OutputDebugString("Error getting surface info\n");
iRc=-6;
} else {
JAWT_Win32DrawingSurfaceInfo* dsi_win;

// Get the platform-specific drawing info
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
if (dsi_win == NULL){
OutputDebugString("Error getting platform-specific drawing
info\n");
iRc=-7;
} else {

//////////////////////////////
// !!! DO PAINTING HERE !!! //
//////////////////////////////
//dsi_win.
//jint ret = reinterpret_cast<jint>(dsiwin->hwnd);
if (hWndSunAwtCanvas2==dsi_win->hwnd) {
playDIB3(dsi_win->hwnd);
} else{
OutputDebugString("Error dsi_win->hwnd\n");

}//
// Free the drawing surface info
ds->FreeDrawingSurfaceInfo(dsi);
}//

//
// Unlock the drawing surface
ds->Unlock(ds);
}
}//
// Free the drawing surface
awt.FreeDrawingSurface(ds);
}//
jvm->DetachCurrentThread();
}
}//JAWT_GetAWT
}//JNI_CreateJavaVM
} catch(...){
OutputDebugString("Exception caught\n");
throw;
}

return iRc;
}
------------eof----------------------

Now, I want to subclass the java applet through the use of JNI like
this example from sun's jni.
----------------------WindowUtil.java
import java.awt.*;
import javax.swing.*;

public class WindowUtil extends Canvas {
static { System.load("c:\\mylib.dll"); }
public native void flashInTaskBar(Component c, boolean flash);

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++) {
flashInTaskBar(frame,true);
Thread.sleep(intratime);
flashInTaskBar(frame,true);
Thread.sleep(intertime);
}
// turn the flash off
flashInTaskBar(frame,false);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}}).start();
}
}
------------eof----------------------
----------myutil.c ---------------------
JNIEXPORT void JNICALL Java_flash_WindowUtil_flashInTaskBar
(JNIEnv *env, jobject canvas, jobject component, jboolean flash)
{
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, flash);

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


Will this approach let me get into the java model and extract the info
being typed on the screen. Any comments or suggestions on how to read
info from a java applet, which is one over which I have no control?


also see
http://msdn.microsoft.com/library/periodic/period00/hood0200.htm

http://java.sys-con.com/read/48177.htm

http://www.codeproject.com

apihijack

subhook
-----------------------------
1) Does anyone know how to get to the Sun supplied ActiveX control which
contains the jvm?
2) Another approach is to intercept the communications between the java
applet and IE. Does anyone know how to do this?
-----------
I want to get at the displayed java image.

Given that IE has a java applet and
this is the hierarchy of hWnd as seen by Spy++

IEFrame
-> Shell DocObject View
---> Internet Explorer_Server
-----> Java Plug-in Control Window
-------> sun.plugin.viewer.frame.IExplorerEmbeddedFrame
---------> sunawtcanvas
------------> sunawtcanvas

the final leaf node I call sunawtcanvas2 (it's parent is sunawtcanvas1)

If I sendMessage(sunawtcanvas2, WM_SETTEXT, 0, LPARAM)"Some text");
the "Some text" does not show up in my window, sunawtcanvas2 .

I also tried to pull the bitmap out, but did not get anything but 1
pixel, e.g.
------------------- getDIB.cpp -----------------
#define bmiSize 1024
// reads the dib by getting them directly
void getDIB( HWND& hWndSunAwtCanvas2)
{
char szBuf[1024];
if (NULL==hWndSunAwtCanvas2){
DWORD dw=GetLastError();
sprintf(szBuf, "NULL==hWndSunAwtCanvas2\n GetLastError returns %d\n",
dw);
OutputDebugString( szBuf);
return;
}

int ibits =0;

UINT uStartScan=0;
UINT cScanLines=1;
HDC hdc = GetWindowDC(hWndSunAwtCanvas2);
if (NULL==hdc){
DWORD dw=GetLastError();

sprintf(szBuf, "GetWindowDC failed\n GetLastError returns %d\n", dw);
OutputDebugString( szBuf);
return;
}

int nWidth =32;
int nHeight=32;
HBITMAP hbmp = CreateCompatibleBitmap(
hdc, // handle to device context
nWidth, // width of bitmap, in pixels
nHeight // height of bitmap, in pixels
);
if (0==hbmp){
DWORD dw=GetLastError();

sprintf(szBuf, "CreateCompatibleBitmap failed\n GetLastError returns
%d\n", dw);
OutputDebugString( szBuf);
return;
}
LPVOID lpvBits = NULL;
//BITMAPINFO bitmapinfo;
LPBITMAPINFO lpbi=(BITMAPINFO *) new char[bmiSize];

if (NULL!= lpbi) {
void* pv = memset( lpbi, 0, bmiSize);

if (NULL!=pv) {
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

UINT uUsage =DIB_RGB_COLORS;
ibits = GetDIBits(
hdc, // handle to device context
hbmp, // handle to bitmap
0, // first scan line to set in destination bitmap
0, // number of scan lines to copy
NULL, // address of array for bitmap bits
lpbi, // address of structure with bitmap data
uUsage // RGB or palette index
);
if (0==ibits) {
DWORD dw=GetLastError();

sprintf(szBuf, "getDIbits1\n GetLastError returns %d\n", dw);
OutputDebugString( szBuf);
}

else {
ibits = GetDIBits(
hdc, // handle to device context
hbmp, // handle to bitmap
uStartScan, // first scan line to set in destination bitmap
lpbi->bmiHeader.biHeight, // number of scan lines to copy
lpvBits, // address of array for bitmap bits
lpbi, // address of structure with bitmap data
uUsage // RGB or palette index
);
if (0==ibits) {
DWORD dw=GetLastError();

sprintf(szBuf, "getDIbits2\n GetLastError returns %d\n", dw);
OutputDebugString( szBuf);
}
// unfortunately ibits is 1, why
HINSTANCE hInstance=hInst; // handle to application instance
LPCTSTR lpTemplate = MAKEINTRESOURCE(IDD_DIALOG_1); //
identifies dialog box template name
HWND hWndParent=NULL; // handle to owner window
DLGPROC lpDialogFunc =DlgProc1; // pointer to dialog box procedure
// display the image returned
HWND hwndDlg = CreateDialog(
hInstance, // handle to application instance
lpTemplate, // identifies dialog box template name
hWndParent, // handle to owner window
lpDialogFunc // pointer to dialog box procedure
);
if (NULL==hwndDlg ){
DWORD dw=GetLastError();

sprintf(szBuf, "CreateDialog failed\n GetLastError returns %d\n",
dw);
OutputDebugString( szBuf);
} else {
SendMessage( hwndDlg, IDC_COMMAND_SENT, 0, (LPARAM)lpbi);
ShowWindow(hwndDlg, SW_SHOWNORMAL);
}
}
}
delete lpbi;
}
}
------------eof----------------------
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2005
Added on May 26 2005
0 comments
257 views