/
DcxWebView2Module.cpp
110 lines (90 loc) · 4.32 KB
/
DcxWebView2Module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "defines.h"
#include "DcxWebView2Module.h"
#include "Dcx.h"
#pragma warning(push)
#pragma warning(disable: 26425) //warning C26425 : Assigning 'nullptr' to a static variable.
DcxWebView2Module::~DcxWebView2Module() noexcept
{
unload();
}
bool DcxWebView2Module::load()
{
if (isUseable())
return false;
// WebView2Module Loading
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Loading WebView2Loader.dll..."));
m_hModule = LoadLibrary(TEXT("WebView2Loader.dll"));
if (!m_hModule)
{
TString tsBuf(gsl::narrow_cast<TString::size_type>(MIRC_BUFFER_SIZE_CCH));
GetModuleFileNameW(getDllModule(), tsBuf.to_wchr(), gsl::narrow_cast<DWORD>(tsBuf.capacity_cch()));
tsBuf.deltok(gsl::narrow_cast<UINT>(tsBuf.numtok(L'\\')), L'\\');
tsBuf.addtok(L"WebView2Loader.dll", L'\\');
m_hModule = LoadLibrary(tsBuf.to_wchr());
}
if (m_hModule)
{
// Get function pointers.
#pragma warning(push)
#pragma warning(disable: 4191)
#pragma warning(disable: 26493) //warning C26493 : Don't use C-style casts that would perform a static_cast downcast, const_cast, or reinterpret_cast. (type.4: http://go.microsoft.com/fwlink/p/?LinkID=620420)
GetAvailableCoreWebView2BrowserVersionStringUx = reinterpret_cast<decltype(::GetAvailableCoreWebView2BrowserVersionString)*>(::GetProcAddress(m_hModule, "GetAvailableCoreWebView2BrowserVersionString"));
CreateCoreWebView2EnvironmentWithOptionsUx = reinterpret_cast<decltype(::CreateCoreWebView2EnvironmentWithOptions)*>(::GetProcAddress(m_hModule, "CreateCoreWebView2EnvironmentWithOptions"));
CreateCoreWebView2EnvironmentUx = reinterpret_cast<decltype(::CreateCoreWebView2Environment)*>(::GetProcAddress(m_hModule, "CreateCoreWebView2Environment"));
CompareBrowserVersionsUx = reinterpret_cast<decltype(::CompareBrowserVersions)*>(::GetProcAddress(m_hModule, "CompareBrowserVersions"));
#pragma warning(pop)
if (GetAvailableCoreWebView2BrowserVersionStringUx && CreateCoreWebView2EnvironmentWithOptionsUx && CreateCoreWebView2EnvironmentUx && CompareBrowserVersionsUx)
{
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Found WebView2 Loader Functions"));
}
else {
unload();
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Unable to find WebView2Loader.dll functions."));
//throw Dcx::dcxException("There was a problem loading WebView2Loader Library");
}
}
else {
DCX_DEBUG(mIRCLinker::debug, __FUNCTIONW__, TEXT("Failed to load WebView2 dll"));
Dcx::error(TEXT("LoadDLL"), TEXT("Warning Unable to Load WebView2Loader.dll, web2ctrl will not work."));
}
return isUseable();
}
bool DcxWebView2Module::unload() noexcept
{
if (isUseable())
{
GSL_SUPPRESS(lifetime.1) FreeLibrary(m_hModule);
m_hModule = nullptr;
}
// make sure all functions are nullptr
GetAvailableCoreWebView2BrowserVersionStringUx = nullptr;
CreateCoreWebView2EnvironmentWithOptionsUx = nullptr;
CreateCoreWebView2EnvironmentUx = nullptr;
CompareBrowserVersionsUx = nullptr;
return isUseable();
}
#pragma warning(pop)
HRESULT DcxWebView2Module::dcxGetAvailableCoreWebView2BrowserVersionString(PCWSTR browserExecutableFolder, LPWSTR* versionInfo) noexcept
{
if (GetAvailableCoreWebView2BrowserVersionStringUx)
return GetAvailableCoreWebView2BrowserVersionStringUx(browserExecutableFolder, versionInfo);
return E_NOTIMPL;
}
HRESULT DcxWebView2Module::dcxCreateCoreWebView2EnvironmentWithOptions(PCWSTR browserExecutableFolder, PCWSTR userDataFolder, ICoreWebView2EnvironmentOptions* environmentOptions, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler* environmentCreatedHandler) noexcept
{
if (CreateCoreWebView2EnvironmentWithOptionsUx)
return CreateCoreWebView2EnvironmentWithOptionsUx(browserExecutableFolder, userDataFolder, environmentOptions, environmentCreatedHandler);
return E_NOTIMPL;
}
HRESULT DcxWebView2Module::dcxCreateCoreWebView2Environment(ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler* environmentCreatedHandler) noexcept
{
if (CreateCoreWebView2EnvironmentUx)
return CreateCoreWebView2EnvironmentUx(environmentCreatedHandler);
return E_NOTIMPL;
}
HRESULT DcxWebView2Module::dcxCompareBrowserVersions(PCWSTR version1, PCWSTR version2, int* result) noexcept
{
if (CompareBrowserVersionsUx)
return CompareBrowserVersionsUx(version1, version2, result);
return E_NOTIMPL;
}