-
Notifications
You must be signed in to change notification settings - Fork 285
Updating CPP and CS winui instancing samples. #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhoehna
wants to merge
4
commits into
main
Choose a base branch
from
user/dahoehna/AppLifecycle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...fecycle/Instancing/cpp-winui-packaged/CppWinUiDesktopInstancing/ActivationTrackerHelper.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
#include <Windows.h> | ||
#include <wil/result.h> | ||
#include <memory> | ||
#include <string> | ||
#include <string_view> | ||
|
||
constexpr std::wstring_view c_redirectionKeyName{ L"SOFTWARE\\AppLifecycleExample"}; | ||
constexpr std::wstring_view c_redirectionValueName{ L"NumberOfActivations" }; | ||
|
||
inline wil::unique_hkey GetRegistryKey() | ||
{ | ||
wil::unique_hkey keyToRedirectionTracker{}; | ||
THROW_IF_WIN32_ERROR(RegCreateKeyEx(HKEY_CURRENT_USER, c_redirectionKeyName.data(), 0, nullptr, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, nullptr, keyToRedirectionTracker.put(), nullptr)); | ||
|
||
return std::move(keyToRedirectionTracker); | ||
} | ||
|
||
inline void SetNumberOfActivations(DWORD newNumberOfActivations) | ||
{ | ||
auto keyToRedirectionTracker{GetRegistryKey()}; | ||
THROW_IF_WIN32_ERROR(RegSetValueEx(keyToRedirectionTracker.get(), c_redirectionValueName.data(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&newNumberOfActivations), sizeof(newNumberOfActivations))); | ||
} | ||
|
||
inline DWORD GetNumberOfActivations() | ||
{ | ||
DWORD numberOfActivations = 0; | ||
auto instanceTrackerKey = GetRegistryKey(); | ||
|
||
DWORD sizeOfEntry{}; | ||
auto getResult{ RegQueryValueEx(instanceTrackerKey.get(), c_redirectionValueName.data(), nullptr, nullptr, reinterpret_cast<byte*>(&numberOfActivations), &sizeOfEntry) }; | ||
|
||
if (getResult == ERROR_FILE_NOT_FOUND) | ||
{ | ||
THROW_IF_WIN32_ERROR(RegSetValueEx(instanceTrackerKey.get(), c_redirectionValueName.data(), 0, REG_DWORD, reinterpret_cast<byte*>(&numberOfActivations), sizeof(numberOfActivations))); | ||
return numberOfActivations; | ||
} | ||
else if (getResult != ERROR_MORE_DATA) | ||
{ | ||
THROW_WIN32(getResult); | ||
} | ||
|
||
THROW_IF_WIN32_ERROR(RegQueryValueEx(instanceTrackerKey.get(), c_redirectionValueName.data(), nullptr, nullptr, reinterpret_cast<BYTE*>(&numberOfActivations), &sizeOfEntry)); | ||
return numberOfActivations; | ||
} | ||
|
||
inline DWORD IncrementNumberOfRedirections() | ||
{ | ||
auto numberOfActivations{GetNumberOfActivations()}; | ||
numberOfActivations++; | ||
SetNumberOfActivations(numberOfActivations); | ||
return numberOfActivations; | ||
} |
3 changes: 3 additions & 0 deletions
3
Samples/AppLifecycle/Instancing/cpp-winui-packaged/CppWinUiDesktopInstancing/App.idl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace CppWinUiDesktopInstancing | ||
{ | ||
} |
3 changes: 1 addition & 2 deletions
3
...ancing/CppWinUiDesktopInstancing/App.xaml → ...ckaged/CppWinUiDesktopInstancing/App.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
Samples/AppLifecycle/Instancing/cpp-winui-packaged/CppWinUiDesktopInstancing/App.xaml.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "pch.h" | ||
|
||
#include "App.xaml.h" | ||
#include "MainWindow.xaml.h" | ||
#include "ActivationTrackerHelper.h" | ||
#include <winrt/Microsoft.Windows.AppLifecycle.h> | ||
|
||
using namespace winrt; | ||
using namespace Windows::Foundation; | ||
using namespace winrt::Microsoft::UI::Xaml; | ||
using namespace winrt::Microsoft::UI::Xaml::Controls; | ||
using namespace winrt::Microsoft::UI::Xaml::Navigation; | ||
using namespace CppWinUiDesktopInstancing; | ||
using namespace CppWinUiDesktopInstancing::implementation; | ||
|
||
// To learn more about WinUI, the WinUI project structure, | ||
// and more about our project templates, see: http://aka.ms/winui-project-info. | ||
|
||
/// <summary> | ||
/// Initializes the singleton application object. This is the first line of authored code | ||
/// executed, and as such is the logical equivalent of main() or WinMain(). | ||
/// </summary> | ||
App::App() | ||
{ | ||
InitializeComponent(); | ||
|
||
#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION | ||
UnhandledException([this](IInspectable const&, UnhandledExceptionEventArgs const& e) | ||
{ | ||
if (IsDebuggerPresent()) | ||
{ | ||
auto errorMessage = e.Message(); | ||
__debugbreak(); | ||
} | ||
}); | ||
#endif | ||
} | ||
|
||
/// <summary> | ||
/// Invoked when the application is launched normally by the end user. Other entry points | ||
/// will be used such as when the application is launched to open a specific file. | ||
/// </summary> | ||
/// <param name="e">Details about the launch request and process.</param> | ||
void App::OnLaunched(LaunchActivatedEventArgs const&) | ||
{ | ||
auto numberOfActivations{ IncrementNumberOfRedirections() }; | ||
window = make<MainWindow>(); | ||
|
||
// GetInstances also gets the current instance. | ||
// Meat and potatoes of redirection. | ||
auto instances = winrt::Microsoft::Windows::AppLifecycle::AppInstance::GetInstances(); | ||
|
||
// Number will be incorrect if instance (instanceNumber < c_maxNumberOfInstances) is closed. | ||
// Can be fixed on an OnExit event handler. | ||
std::wstring windowTitle{L"Instances: "}; | ||
windowTitle.append(std::to_wstring(instances.Size() - 1)); | ||
window.Title(windowTitle); | ||
|
||
|
||
// Need activated event args to pass into the redirected instance. | ||
auto activatedArgs = winrt::Microsoft::Windows::AppLifecycle::AppInstance::GetCurrent().GetActivatedEventArgs(); | ||
bool isActivationRedirected = false; | ||
if (instances.Size() > c_maxNumberOfInstances) | ||
{ | ||
// Time to redirect | ||
// Choose what instance to redirect to. | ||
int instanceToRedirectTo = (numberOfActivations % c_maxNumberOfInstances); | ||
isActivationRedirected = true; | ||
|
||
// Need to call RedirectActivationToAsync on the instance to redirect to. | ||
// Not passing in the instance into RedirectionActivationToAsync. | ||
instances.GetAt(instanceToRedirectTo).RedirectActivationToAsync(activatedArgs).GetResults(); | ||
} | ||
|
||
if (isActivationRedirected) | ||
{ | ||
this->Exit(); | ||
} | ||
else | ||
{ | ||
window.Activate(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Samples/AppLifecycle/Instancing/cpp-winui-packaged/CppWinUiDesktopInstancing/App.xaml.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "App.xaml.g.h" | ||
|
||
namespace winrt::CppWinUiDesktopInstancing::implementation | ||
{ | ||
struct App : AppT<App> | ||
{ | ||
const DWORD c_maxNumberOfInstances{ 2 }; | ||
App(); | ||
|
||
void OnLaunched(Microsoft::UI::Xaml::LaunchActivatedEventArgs const&); | ||
|
||
private: | ||
winrt::Microsoft::UI::Xaml::Window window{ nullptr }; | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.