Skip to content

Commit eeb5bf3

Browse files
committed
Move Win32 project to use latest WebView2 SDK 1.0.702-prerelease
1 parent 93614d9 commit eeb5bf3

38 files changed

+1646
-281
lines changed

SampleApps/WebView2APISample/App.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
4242
DPI_AWARENESS_CONTEXT dpiAwarenessContext =
4343
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
4444
std::wstring appId(L"EBWebView.SampleApp");
45-
std::wstring initialUri(L"https://www.bing.com");
45+
std::wstring initialUri;
4646
DWORD creationModeId = IDM_CREATION_MODE_WINDOWED;
4747

4848
if (lpCmdLine && lpCmdLine[0])
@@ -81,7 +81,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
8181
}
8282
else if (NEXT_PARAM_CONTAINS(L"noinitialnavigation"))
8383
{
84-
initialUri = L"";
84+
initialUri = L"none";
8585
}
8686
else if (NEXT_PARAM_CONTAINS(L"appid="))
8787
{
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "stdafx.h"
6+
7+
#include <algorithm>
8+
#include <pathcch.h>
9+
#include <Psapi.h>
10+
11+
#include "AppStartPage.h"
12+
#include "AppWindow.h"
13+
#include "CheckFailure.h"
14+
15+
using namespace Microsoft::WRL;
16+
17+
namespace AppStartPage
18+
{
19+
20+
bool AreFileUrisEqual(std::wstring leftUri, std::wstring rightUri)
21+
{
22+
// Have to to lower due to current bug
23+
std::transform(leftUri.begin(), leftUri.end(),
24+
leftUri.begin(), ::tolower);
25+
std::transform(rightUri.begin(), rightUri.end(),
26+
rightUri.begin(), ::tolower);
27+
28+
return leftUri == rightUri;
29+
}
30+
31+
std::wstring ResolvePathAndTrimFile(std::wstring path)
32+
{
33+
wchar_t resultPath[MAX_PATH];
34+
PathCchCanonicalize(resultPath, ARRAYSIZE(resultPath), path.c_str());
35+
PathCchRemoveFileSpec(resultPath, ARRAYSIZE(resultPath));
36+
return resultPath;
37+
}
38+
39+
std::wstring GetSdkBuild()
40+
{
41+
auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
42+
wil::unique_cotaskmem_string targetVersion;
43+
CHECK_FAILURE(options->get_TargetCompatibleBrowserVersion(&targetVersion));
44+
45+
// The full version string A.B.C.D
46+
const wchar_t* targetVersionMajorAndRest = targetVersion.get();
47+
// Should now be .B.C.D
48+
const wchar_t* targetVersionMinorAndRest = wcschr(targetVersionMajorAndRest, L'.');
49+
CHECK_FAILURE((targetVersionMinorAndRest != nullptr && *targetVersionMinorAndRest == L'.') ? S_OK : E_UNEXPECTED);
50+
51+
// Should now be .C.D
52+
const wchar_t* targetVersionBuildAndRest = wcschr(targetVersionMinorAndRest + 1, L'.');
53+
CHECK_FAILURE((targetVersionBuildAndRest != nullptr && *targetVersionBuildAndRest == L'.') ? S_OK : E_UNEXPECTED);
54+
55+
// Return + 1 to skip the first . so just C.D
56+
return targetVersionBuildAndRest + 1;
57+
}
58+
59+
std::wstring GetRuntimeVersion(AppWindow* appWindow)
60+
{
61+
wil::com_ptr<ICoreWebView2Environment> environment = appWindow->GetWebViewEnvironment();
62+
wil::unique_cotaskmem_string runtimeVersion;
63+
CHECK_FAILURE(environment->get_BrowserVersionString(&runtimeVersion));
64+
65+
return runtimeVersion.get();
66+
}
67+
68+
std::wstring GetAppPath()
69+
{
70+
wchar_t appPath[MAX_PATH];
71+
GetModuleFileName(nullptr, appPath, ARRAYSIZE(appPath));
72+
return ResolvePathAndTrimFile(appPath);
73+
}
74+
75+
std::wstring GetRuntimePath(AppWindow* appWindow)
76+
{
77+
wil::com_ptr<ICoreWebView2> webview = appWindow->GetWebView();
78+
UINT32 browserProcessId = 0;
79+
wchar_t runtimePath[MAX_PATH];
80+
CHECK_FAILURE(webview->get_BrowserProcessId(&browserProcessId));
81+
82+
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, browserProcessId);
83+
CHECK_FAILURE(processHandle == nullptr ? E_FAIL : S_OK);
84+
GetModuleFileNameEx(processHandle, nullptr, runtimePath, ARRAYSIZE(runtimePath));
85+
CloseHandle(processHandle);
86+
87+
return ResolvePathAndTrimFile(runtimePath);
88+
}
89+
90+
std::wstring GetUri(AppWindow* appWindow)
91+
{
92+
std::wstring uri = appWindow->GetLocalUri(L"AppStartPage.html");
93+
94+
uri += L"?sdkBuild=";
95+
uri += GetSdkBuild();
96+
97+
uri += L"&runtimeVersion=";
98+
uri += GetRuntimeVersion(appWindow);
99+
100+
uri += L"&appPath=";
101+
uri += GetAppPath();
102+
103+
uri += L"&runtimePath=";
104+
uri += GetRuntimePath(appWindow);
105+
106+
return uri;
107+
}
108+
109+
}; // namespace AppStartPage
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "stdafx.h"
8+
#include <string>
9+
10+
class AppWindow;
11+
12+
namespace AppStartPage
13+
{
14+
std::wstring GetUri(AppWindow* appWindow);
15+
};

0 commit comments

Comments
 (0)