Skip to content

Commit 7c23ac2

Browse files
Internet Explorer can not be started directly anymore, COM is used as workaround
1 parent df02193 commit 7c23ac2

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

contrib/platform/test/com/sun/jna/platform/win32/COM/ShellApplicationWindowsTest.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
package com.sun.jna.platform.win32.COM;
2424

2525
import com.sun.jna.Pointer;
26+
import com.sun.jna.platform.win32.Guid;
2627
import com.sun.jna.platform.win32.Ole32;
27-
import java.util.Iterator;
28-
import java.util.NoSuchElementException;
29-
28+
import com.sun.jna.platform.win32.OleAuto;
3029
import com.sun.jna.platform.win32.Variant;
3130
import com.sun.jna.platform.win32.Variant.VARIANT;
31+
import com.sun.jna.platform.win32.WTypes;
3232
import com.sun.jna.platform.win32.WinDef.LONG;
33+
import com.sun.jna.platform.win32.WinNT;
34+
import com.sun.jna.ptr.PointerByReference;
35+
36+
import java.util.Iterator;
37+
import java.util.NoSuchElementException;
3338

3439
import org.junit.After;
3540
import org.junit.Before;
@@ -39,16 +44,41 @@
3944

4045
public class ShellApplicationWindowsTest {
4146

47+
private static final Guid.CLSID CLSID_InternetExplorer = new Guid.CLSID("{0002DF01-0000-0000-C000-000000000046}");
48+
4249
static {
4350
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
4451
}
4552

53+
private PointerByReference ieApp;
54+
private Dispatch ieDispatch;
55+
4656
@Before
4757
public void setUp() throws Exception {
48-
Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
58+
WinNT.HRESULT hr;
4959

50-
// Launch IE in a manner that should ensure it opens even if the system default browser is Chrome, Firefox, or something else.
51-
Runtime.getRuntime().exec("cmd /c start iexplore.exe -nohome \"about:blank\"");
60+
hr = Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
61+
COMUtils.checkRC(hr);
62+
63+
// IE can not be launched directly anymore - so load it via COM
64+
65+
ieApp = new PointerByReference();
66+
hr = Ole32.INSTANCE
67+
.CoCreateInstance(CLSID_InternetExplorer, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, ieApp);
68+
COMUtils.checkRC(hr);
69+
70+
ieDispatch = new Dispatch(ieApp.getValue());
71+
InternetExplorer ie = new InternetExplorer(ieDispatch);
72+
73+
ie.setProperty("Visible", true);
74+
COMUtils.checkRC(hr);
75+
76+
VARIANT url = new VARIANT("about:blank");
77+
VARIANT result = ie.invoke("Navigate", url);
78+
OleAuto.INSTANCE.VariantClear(url);
79+
OleAuto.INSTANCE.VariantClear(result);
80+
81+
ieDispatch.Release();
5282

5383
// Even when going to "about:blank", IE still needs a few seconds to start up and add itself to Shell.Application.Windows
5484
// Removing this delay will cause the test to fail even on the fastest boxes I can find.
@@ -85,6 +115,7 @@ public void testWindowsCount()
85115
@After
86116
public void tearDown() throws Exception
87117
{
118+
Ole32.INSTANCE.CoUninitialize();
88119
Runtime.getRuntime().exec("taskkill.exe /f /im iexplore.exe");
89120
}
90121

contrib/platform/test/com/sun/jna/platform/win32/WininetUtilTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
*/
2424
package 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;
2632
import java.util.Map;
2733

2834
import 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

Comments
 (0)