Click here to Skip to main content
15,889,211 members
Articles / Desktop Programming / ATL
Article

Embed ActiveX controls inside Java GUI

Rate me:
Please Sign up or sign in to vote.
4.78/5 (18 votes)
8 May 2000 448K   859   39   106
With this your Java projects can take advantage of ActiveX controls and Office documents such as spreadsheets, charts, calendars, word processors, specialized graphics, and many more.
  • Download source and binary files - 6.9 Kb
  • Sample Image - javacom.gif

    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

    // Returns the HWND for panel. This is a hack which works with
    // JDK1.1.8, JDK1.2.2 and JDK1.3. This is undocumented. Also 
    // you should call this only after addNotify has been called.
    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

    // Creates IE control
    VOID CreateIEControl(ThreadParam *pThreadParam)
    {
        AtlAxWinInit();
        printf("Create AtlAxWin Begin...[0x%x][%s]\n",pThreadParam->hwnd,pThreadParam->szURL);
        // In the 2nd Param you can use ProgID or UUID of any activex control.
        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);
    
        // get an interface to set the URL.
        CComPtr<IWebBrowser2> spBrowser;
        pUnk->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser);
        if (spBrowser)
        {
            CComVariant ve;
            CComVariant vurl(pThreadParam->szURL);
    #pragma warning(disable: 4310) // cast truncates constant value
            spBrowser->put_Visible(VARIANT_TRUE);
    #pragma warning(default: 4310) // cast truncates constant value
            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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralRe: embedding on a Pocket PC Pin
    rubao19-Nov-03 0:18
    rubao19-Nov-03 0:18 
    GeneralA problem of Dependencies is caused when deploying the project in VB .NET while application containing a third party ActiveX Pin
    rizoriz16-Apr-03 11:45
    rizoriz16-Apr-03 11:45 
    QuestionHow to get Hwnd with jdk 1.4 Pin
    Anonymous13-Apr-03 21:59
    Anonymous13-Apr-03 21:59 
    AnswerRe: How to get Hwnd with jdk 1.4 Pin
    gusp14-Apr-03 5:39
    gusp14-Apr-03 5:39 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty2-Jun-03 1:36
    brighty2-Jun-03 1:36 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    gusp3-Jun-03 0:13
    gusp3-Jun-03 0:13 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty5-Jun-03 4:48
    brighty5-Jun-03 4:48 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty5-Jun-03 23:16
    brighty5-Jun-03 23:16 
    Smile | :)
    hi gusp, hi all,

    yeah! I have been successful and can run the example now using jre 1.4.1_01-b01 ! Thanx again to gusp who gave the great hints. Yes, now i obtain the hwnd of the canvas by the first call to the overridden and native made paint() function and store it in a static HWND variable. Like this further calls to other functions in the DLL can also use the canvas HWND.

    If anyone is interested in the code just let me know: peter@brightman.de

    I want to write a native function now that uses the "spBrowser" pointer obtained from the call to pUnk->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser) and do a spBrowser->Navigate2(). Is this a good way to store the "spBrowser" in a static variable or is it better to get a pointer again and again with each need of a navigation? Any hints are highly appreciated.

    What i want to do too is to use for example html code which resides in memory and pass it to the browser by input stream. But this requires to know if the "DocumentComplete" event has occured. I also want to query the browser's DOM for objects, for this i also need this event.

    But i don't have a plan how to get the events from the browser. Does anyone have an idea or code regarding this issue? Thanks.

    cheers,

    brighty Big Grin | :-D

    XHXHXHXHXHXHXHXHXHXHXHXHXHXHX
    Peter Brightman
    XHXHXHXHXHXHXHXHXHXHXHXHXHXHX

    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    TorgeirV23-Jul-03 1:23
    sussTorgeirV23-Jul-03 1:23 
    QuestionRe: How to get Hwnd with jdk 1.4 Pin
    conquestkid7-Aug-06 11:17
    conquestkid7-Aug-06 11:17 
    AnswerRe: How to get Hwnd with jdk 1.4 Pin
    conquestkid18-Aug-06 4:09
    conquestkid18-Aug-06 4:09 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    James Duy Trinh (VietDoor)19-Jan-05 16:09
    James Duy Trinh (VietDoor)19-Jan-05 16:09 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    Txarli6-Jun-05 5:39
    Txarli6-Jun-05 5:39 
    GeneralExperience with embedding IE Pin
    brighty10-Apr-03 3:19
    brighty10-Apr-03 3:19 
    GeneralRe: Experience with embedding IE Pin
    Anonymous12-Apr-03 19:13
    Anonymous12-Apr-03 19:13 
    GeneralRe: Experience with embedding IE Pin
    brighty15-Apr-03 1:44
    brighty15-Apr-03 1:44 
    GeneralRe: Experience with embedding IE Pin
    Member 4599051-Jul-03 23:07
    Member 4599051-Jul-03 23:07 
    Generalnjawin Pin
    sjava31-Mar-03 16:31
    sjava31-Mar-03 16:31 
    GeneralRe: njawin Pin
    brighty5-Jun-03 22:53
    brighty5-Jun-03 22:53 
    GeneralRe: njawin Pin
    sjava6-Jun-03 17:54
    sjava6-Jun-03 17:54 
    GeneralCasting error Pin
    Member 16812927-Jan-03 19:38
    Member 16812927-Jan-03 19:38 
    GeneralEmbed Excel Pin
    Anonymous14-Nov-02 19:57
    Anonymous14-Nov-02 19:57 
    GeneralRe: Embed Excel Pin
    Anonymous10-Jul-03 1:32
    Anonymous10-Jul-03 1:32 
    Generalerror on win32drawingsurface Pin
    vinothlee14-Nov-02 7:08
    vinothlee14-Nov-02 7:08 
    GeneralRe: error on win32drawingsurface Pin
    holepuncher6-Dec-02 10:07
    holepuncher6-Dec-02 10:07 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.