Skip to content
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

Updating CPP and CS winui instancing samples. #235

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
</PropertyGroup>
<Import Project="$(WapProjPath)\Microsoft.DesktopBridge.props" />
<PropertyGroup>
<ProjectGuid>2a6ee91d-5eff-4074-a8b1-7f222eb217cd</ProjectGuid>
<TargetPlatformVersion>10.0.19041.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<ProjectGuid>9cb90dc4-272b-4414-9bb7-ba5412038211</ProjectGuid>
<TargetPlatformVersion>10.0.22621.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<DefaultLanguage>en-US</DefaultLanguage>
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
<EntryPointProjectUniqueName>..\CppWinUiDesktopInstancing\CppWinUiDesktopInstancing.vcxproj</EntryPointProjectUniqueName>
Expand All @@ -63,10 +63,10 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0">
<PackageReference Include="Microsoft.WindowsAppSDK" Version="[1.1.4]">
<IncludeAssets>build</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.196">
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="[10.0.22000.194]">
<IncludeAssets>build</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
IgnorableNamespaces="uap rescap">

<Identity
Name="c4a3b17f-0039-493a-a7b5-a82222ffe041"
Publisher="CN=Microsoft Corporation"
Name="316a7f97-7cd3-4657-87b2-fd866c21e546"
Publisher="CN=dahoehna"
dhoehna marked this conversation as resolved.
Show resolved Hide resolved
Version="1.0.0.0" />

<Properties>
<DisplayName>CsWinUiDesktopInstancing</DisplayName>
<PublisherDisplayName>Microsoft Corporation</PublisherDisplayName>
<DisplayName>CppWinUiDesktopInstancing (Package)</DisplayName>
<PublisherDisplayName>dahoehna</PublisherDisplayName>
<Logo>Images\StoreLogo.png</Logo>
</Properties>

Expand All @@ -31,26 +31,14 @@
Executable="$targetnametoken$.exe"
EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="CsWinUiDesktopInstancing"
Description="CsWinUiDesktopInstancing"
DisplayName="CppWinUiDesktopInstancing (Package)"
Description="CppWinUiDesktopInstancing (Package)"
BackgroundColor="transparent"
Square150x150Logo="Images\Square150x150Logo.png"
Square44x44Logo="Images\Square44x44Logo.png">
<uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png" />
<uap:SplashScreen Image="Images\SplashScreen.png" />
</uap:VisualElements>

<Extensions>
<uap:Extension Category="windows.fileTypeAssociation">
<uap:FileTypeAssociation Name="moo">
<uap:Logo>Images\StoreLogo.png</uap:Logo>
<uap:SupportedFileTypes>
<uap:FileType>.moo</uap:FileType>
</uap:SupportedFileTypes>
</uap:FileTypeAssociation>
</uap:Extension>
</Extensions>

</Application>
</Applications>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31624.102
# Visual Studio Version 17
VisualStudioVersion = 17.3.32811.315
MinimumVisualStudioVersion = 10.0.40219.1
Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "CppWinUiDesktopInstancing (Package)", "CppWinUiDesktopInstancing\CppWinUiDesktopInstancing (Package)\CppWinUiDesktopInstancing (Package).wapproj", "{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}"
Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "CppWinUiDesktopInstancing (Package)", "CppWinUiDesktopInstancing (Package)\CppWinUiDesktopInstancing (Package).wapproj", "{9CB90DC4-272B-4414-9BB7-BA5412038211}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CppWinUiDesktopInstancing", "CppWinUiDesktopInstancing\CppWinUiDesktopInstancing\CppWinUiDesktopInstancing.vcxproj", "{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CppWinUiDesktopInstancing", "CppWinUiDesktopInstancing\CppWinUiDesktopInstancing.vcxproj", "{67844494-855A-468D-8D92-FA34D7E2F680}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -17,41 +17,41 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|arm64.ActiveCfg = Debug|arm64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|arm64.Build.0 = Debug|arm64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|arm64.Deploy.0 = Debug|arm64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|x64.ActiveCfg = Debug|x64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|x64.Build.0 = Debug|x64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|x64.Deploy.0 = Debug|x64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|x86.ActiveCfg = Debug|x86
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|x86.Build.0 = Debug|x86
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Debug|x86.Deploy.0 = Debug|x86
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|arm64.ActiveCfg = Release|arm64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|arm64.Build.0 = Release|arm64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|arm64.Deploy.0 = Release|arm64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|x64.ActiveCfg = Release|x64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|x64.Build.0 = Release|x64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|x64.Deploy.0 = Release|x64
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|x86.ActiveCfg = Release|x86
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|x86.Build.0 = Release|x86
{2A6EE91D-5EFF-4074-A8B1-7F222EB217CD}.Release|x86.Deploy.0 = Release|x86
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Debug|arm64.ActiveCfg = Debug|arm64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Debug|arm64.Build.0 = Debug|arm64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Debug|x64.ActiveCfg = Debug|x64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Debug|x64.Build.0 = Debug|x64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Debug|x86.ActiveCfg = Debug|Win32
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Debug|x86.Build.0 = Debug|Win32
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Release|arm64.ActiveCfg = Release|arm64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Release|arm64.Build.0 = Release|arm64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Release|x64.ActiveCfg = Release|x64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Release|x64.Build.0 = Release|x64
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Release|x86.ActiveCfg = Release|Win32
{49AC0C00-DF6B-4F6C-A5DD-D178BF133A2C}.Release|x86.Build.0 = Release|Win32
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|arm64.ActiveCfg = Debug|arm64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|arm64.Build.0 = Debug|arm64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|arm64.Deploy.0 = Debug|arm64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|x64.ActiveCfg = Debug|x64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|x64.Build.0 = Debug|x64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|x64.Deploy.0 = Debug|x64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|x86.ActiveCfg = Debug|x86
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|x86.Build.0 = Debug|x86
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Debug|x86.Deploy.0 = Debug|x86
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|arm64.ActiveCfg = Release|arm64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|arm64.Build.0 = Release|arm64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|arm64.Deploy.0 = Release|arm64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|x64.ActiveCfg = Release|x64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|x64.Build.0 = Release|x64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|x64.Deploy.0 = Release|x64
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|x86.ActiveCfg = Release|x86
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|x86.Build.0 = Release|x86
{9CB90DC4-272B-4414-9BB7-BA5412038211}.Release|x86.Deploy.0 = Release|x86
{67844494-855A-468D-8D92-FA34D7E2F680}.Debug|arm64.ActiveCfg = Debug|arm64
{67844494-855A-468D-8D92-FA34D7E2F680}.Debug|arm64.Build.0 = Debug|arm64
{67844494-855A-468D-8D92-FA34D7E2F680}.Debug|x64.ActiveCfg = Debug|x64
{67844494-855A-468D-8D92-FA34D7E2F680}.Debug|x64.Build.0 = Debug|x64
{67844494-855A-468D-8D92-FA34D7E2F680}.Debug|x86.ActiveCfg = Debug|Win32
{67844494-855A-468D-8D92-FA34D7E2F680}.Debug|x86.Build.0 = Debug|Win32
{67844494-855A-468D-8D92-FA34D7E2F680}.Release|arm64.ActiveCfg = Release|arm64
{67844494-855A-468D-8D92-FA34D7E2F680}.Release|arm64.Build.0 = Release|arm64
{67844494-855A-468D-8D92-FA34D7E2F680}.Release|x64.ActiveCfg = Release|x64
{67844494-855A-468D-8D92-FA34D7E2F680}.Release|x64.Build.0 = Release|x64
{67844494-855A-468D-8D92-FA34D7E2F680}.Release|x86.ActiveCfg = Release|Win32
{67844494-855A-468D-8D92-FA34D7E2F680}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EF56AF65-60A9-4FB9-BCE7-4CA06BEDC705}
SolutionGuid = {5C838CC3-9397-401F-A1E9-08FD04D02E7A}
EndGlobalSection
EndGlobal
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 IncrementAndSetNumberOfRedirectionsReturnNewNumber()
{
auto numberOfActivations{GetNumberOfActivations()};
numberOfActivations++;
SetNumberOfActivations(numberOfActivations);
return numberOfActivations;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace CppWinUiDesktopInstancing
{
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<!-- Copyright (c) Microsoft Corporation.
Licensed under the MIT License. -->
dhoehna marked this conversation as resolved.
Show resolved Hide resolved
<Application
<Application
x:Class="CppWinUiDesktopInstancing.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down
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{ IncrementAndSetNumberOfRedirectionsReturnNewNumber() };
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();
}
}
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 };
};
}
Loading