Skip to content

Commit

Permalink
v1.2
Browse files Browse the repository at this point in the history
fix HRESULT: 0x8150002E
use SHDocVw.InternetExplorer replace MSScriptControl
  • Loading branch information
AigioL committed Jun 5, 2022
1 parent 3f7ace0 commit 47da7c2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
4 changes: 2 additions & 2 deletions ImplicitUsings.cs
@@ -1,6 +1,6 @@
global using System;
global using System.Linq;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
global using System.Windows.Forms;
global using Microsoft.Win32;
global using MSScriptControl;
global using SHDocVw;
10 changes: 5 additions & 5 deletions OpenInternetExplorer.csproj
Expand Up @@ -6,22 +6,22 @@
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<PlatformTarget>x86</PlatformTarget>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
</PropertyGroup>

<ItemGroup>
<Reference Include="System.Windows.Forms" />
</ItemGroup>

<ItemGroup>
<COMReference Include="MSScriptControl">
<COMReference Include="SHDocVw">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMinor>1</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>0e59f1d2-1fbe-11d0-8ff2-00a0d10038bc</Guid>
<Guid>eab22ac0-30c1-11cf-a7eb-0000c05bae0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>

Expand Down
61 changes: 49 additions & 12 deletions Program.cs
Expand Up @@ -4,13 +4,17 @@

try
{
var obj = new ScriptControl
var url = GetStartPage(args);
try
{
Language = "vbscript",
};
// https://docs.microsoft.com/en-us/office/vba/api/Outlook.Application.CreateObject#example
const string content = $"Set Web = CreateObject(\"InternetExplorer.Application\")\r\nWeb.Visible = True\r\nWeb.Navigate \"{{0}}\"";
obj.ExecuteStatement(string.Format(content, GetStartPage(args)));
OpenInternetExplorer(url);
}
catch
{
// https://stackoverflow.com/questions/56044878/how-to-fix-an-hresult-0x8150002e-exception
TryKillInternetExplorer();
OpenInternetExplorer(url);
}
}
catch (Exception ex)
{
Expand All @@ -28,12 +32,12 @@ static string GetStartPage(string[] args)
if (IsHttpUrl(url, httpsOnly)) return url;
try
{
using var registryKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser,
Environment.Is64BitOperatingSystem ?
RegistryView.Registry64 :
RegistryView.Registry32)
.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main");
url = registryKey.GetValue("Start Page")?.ToString();
if (Environment.Is64BitOperatingSystem)
{
url = GetStartPageByRegistry(RegistryView.Registry64);
if (IsHttpUrl(url, httpsOnly)) return url;
}
url = GetStartPageByRegistry(RegistryView.Registry32);
if (IsHttpUrl(url, httpsOnly)) return url;
}
catch
Expand All @@ -43,6 +47,13 @@ static string GetStartPage(string[] args)
return "https://www.bing.com";
}

static string? GetStartPageByRegistry(RegistryView registryView)
{
using var registryKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, registryView)
.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main");
return registryKey.GetValue("Start Page")?.ToString();
}

static string? GetArgument(string[] args, int index)
{
try
Expand All @@ -65,4 +76,30 @@ static bool GetArgumentB(string[] args, int index, bool defaultValue = false)
{
}
return defaultValue;
}

static void OpenInternetExplorer(string url)
{
// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752084(v=vs.85)
var IE = new InternetExplorer
{
Visible = true
};
IE.Navigate(url);
}

static void TryKillInternetExplorer()
{
var processes = Process.GetProcessesByName("iexplore");
foreach (var process in processes)
{
try
{
process.Kill();
}
catch
{

}
}
}

0 comments on commit 47da7c2

Please sign in to comment.