2323 */
2424package com .sun .jna .platform .win32 ;
2525
26+ import com .sun .jna .Pointer ;
27+ import com .sun .jna .platform .win32 .COM .COMLateBindingObject ;
28+ import com .sun .jna .platform .win32 .COM .COMUtils ;
29+ import com .sun .jna .platform .win32 .COM .Dispatch ;
30+ import com .sun .jna .platform .win32 .COM .IDispatch ;
31+ import com .sun .jna .ptr .PointerByReference ;
2632import java .util .Map ;
2733
2834import org .junit .After ;
@@ -37,13 +43,41 @@ public static void main(String[] args) {
3743 jUnitCore .run (WininetUtilTest .class );
3844 }
3945
46+ private static final Guid .CLSID CLSID_InternetExplorer = new Guid .CLSID ("{0002DF01-0000-0000-C000-000000000046}" );
47+
48+ private PointerByReference ieApp ;
49+ private Dispatch ieDispatch ;
50+
4051 @ Before
4152 public void setUp () throws Exception {
4253 // Launch IE in a manner that should ensure it opens even if the system
4354 // default browser is Chrome, Firefox, or something else.
4455 // Launching IE to a page ensures there will be content in the WinInet
4556 // cache.
46- Runtime .getRuntime ().exec ("cmd /c start iexplore.exe -nomerge -nohome \" http://www.google.com\" " );
57+ WinNT .HRESULT hr ;
58+
59+ hr = Ole32 .INSTANCE .CoInitializeEx (Pointer .NULL , Ole32 .COINIT_MULTITHREADED );
60+ COMUtils .checkRC (hr );
61+
62+ // IE can not be launched directly anymore - so load it via COM
63+
64+ ieApp = new PointerByReference ();
65+ hr = Ole32 .INSTANCE
66+ .CoCreateInstance (CLSID_InternetExplorer , null , WTypes .CLSCTX_SERVER , IDispatch .IID_IDISPATCH , ieApp );
67+ COMUtils .checkRC (hr );
68+
69+ ieDispatch = new Dispatch (ieApp .getValue ());
70+ LocalLateBinding ie = new LocalLateBinding (ieDispatch );
71+
72+ ie .setProperty ("Visible" , true );
73+
74+ Variant .VARIANT url = new Variant .VARIANT ("http://www.google.com" );
75+ Variant .VARIANT result = ie .invoke ("Navigate" , url );
76+ OleAuto .INSTANCE .VariantClear (url );
77+ OleAuto .INSTANCE .VariantClear (result );
78+
79+ ieDispatch .Release ();
80+ Ole32 .INSTANCE .CoUninitialize ();
4781
4882 // There's no easy way to monitor IE and see when it's done loading
4983 // google.com, so just give it 10 seconds.
@@ -77,6 +111,24 @@ public void testGetCache() throws Exception {
77111 @ After
78112 public void tearDown () throws Exception {
79113 // only kill the freshly opened Google window, unless someone has two IE windows open to Google.
80- Runtime .getRuntime ().exec ("taskkill.exe /f /im iexplore.exe /fi \" windowtitle eq Google*\" " );
114+ Runtime .getRuntime ().exec ("taskkill.exe /f /im iexplore.exe" );
115+ }
116+
117+ private static class LocalLateBinding extends COMLateBindingObject {
118+
119+ public LocalLateBinding (IDispatch iDispatch ) {
120+ super (iDispatch );
121+ }
122+
123+ @ Override
124+ public void setProperty (String propertyName , boolean value ) {
125+ super .setProperty (propertyName , value );
126+ }
127+
128+ @ Override
129+ public Variant .VARIANT invoke (String methodName , Variant .VARIANT arg ) {
130+ return super .invoke (methodName , arg );
131+ }
132+
81133 }
82134}
0 commit comments