Download source and binary files - 6.9 Kb
Introduction
This article provides a skeleton for embedding ActiveX controls within your Java Applications.
The sample project shows how to embed the Internet Explorer 5.0 just like any other Java Widget.
There are two major problems that needed to be solved to be able to do this.
Problem #1 - Get the HWND of a Java Heavy Weight component.
Problem #2 - How to attach an activex control as a child of the HWND above.
Problem #1 was solved by an undocumented API present in all versions of JDK's from Sun, for example
JDK 1.1.8, JDK 1.2.2 and JDK 1.3. Here's the URL which explains how to get the HWND of a java.awt.Canvas object
and an extract from the project which shows the implementation.
http://www.jguru.com/jguru/faq/view.jsp?EID=44507
public int getHWND()
{
int hwnd = 0;
DrawingSurfaceInfo drawingSurfaceInfo = ((DrawingSurface)(getPeer())).getDrawingSurfaceInfo();
if (null != drawingSurfaceInfo)
{
drawingSurfaceInfo.lock();
Win32DrawingSurface win32DrawingSurface = (Win32DrawingSurface)drawingSurfaceInfo.getSurface();
hwnd = win32DrawingSurface.getHWnd();
drawingSurfaceInfo.unlock();
}
return hwnd;
}
Problem #2 was a bit more complicated but MSDN's Online KB was of great help in solving this.
Here's the URL which explains how to add an ActiveX control as a child of any HWND using ATL
and an extract from the project which shows how the concept is implemented.
http://support.microsoft.com/support/kb/articles/Q192/5/60.ASP
VOID CreateIEControl(ThreadParam *pThreadParam)
{
AtlAxWinInit();
printf("Create AtlAxWin Begin...[0x%x][%s]\n",pThreadParam->hwnd,pThreadParam->szURL);
HWND hwndChild = ::CreateWindow("AtlAxWin",
"Shell.Explorer.1",
WS_CHILD|WS_VISIBLE,
0,0,0,0,
pThreadParam->hwnd,NULL,
::GetModuleHandle(NULL),
NULL);
IUnknown *pUnk = NULL;
AtlAxGetControl(hwndChild,&pUnk);
printf("Create AtlAxWin Done...[0x%x]\n",pUnk);
CComPtr spBrowser;
pUnk->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser);
if (spBrowser)
{
CComVariant ve;
CComVariant vurl(pThreadParam->szURL);
#pragma warning(disable: 4310)
spBrowser->put_Visible(VARIANT_TRUE);
#pragma warning(default: 4310)
spBrowser->Navigate2(&vurl, &ve, &ve, &ve, &ve);
}
}
To Build the Project edit and use Build.BAT present in the downloadable Zip file above.
To run the Java Application, use "java MyWindow http://www.codeproject.com" from the command line.
That's it! Have Fun.