Skip to content

Commit

Permalink
[Refactor]Port C++/CX to C++/WinRT (#34198)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request
Removes all C++/CX code, replacing it with C++/WinRT.

## Detailed Description of the Pull Request / Additional comments
Removes all C++/CX code.
Renames interop namespaces to be better consumed by CsWinRT.
Standardizes all projects on net8.0-windows10.0.20348.0, which is a
requirement for C++/WinRT usage.
FileLocksmithLibInterop brought to stdcpplatest and static analysis
errors were corrected.
Removed now unneeded string conversion code from
FileLocksmithLibInterop.
Changed interop KeyboardHook to use a single hook across all instances.
Required because on C++/WinRT we don't have the .NET runtime to bind a
object instance to a delegate and be able to pass it to a C function
pointer argument (still no idea why this worked correctly on C++/CX to
be honest). This change actually makes us create less low level keyboard
hooks.
Changed some code that depended on arrays since WinRT/C++ returns null
instead of an empty array through the interface.

## Validation Steps Performed
Built and tested runtime.
  • Loading branch information
jaimecbernardo authored Aug 8, 2024
1 parent b16e82c commit fb5ed13
Show file tree
Hide file tree
Showing 136 changed files with 1,486 additions and 1,094 deletions.
31 changes: 15 additions & 16 deletions PowerToys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Settings.UI.Library", "src\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj", "{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PowerToys.Interop", "src\common\interop\PowerToys.Interop.vcxproj", "{F055103B-F80B-4D0C-BF48-057C55620033}"
ProjectSection(ProjectDependencies) = postProject
{CC6E41AC-8174-4E8A-8D22-85DD7F4851DF} = {CC6E41AC-8174-4E8A-8D22-85DD7F4851DF}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Plugin.Folder", "src\modules\launcher\Plugins\Microsoft.Plugin.Folder\Microsoft.Plugin.Folder.csproj", "{787B8AA6-CA93-4C84-96FE-DF31110AD1C4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerLauncher.Telemetry", "src\modules\launcher\PowerLauncher.Telemetry\PowerLauncher.Telemetry.csproj", "{08C8C05F-0362-41BC-818C-724572DF8B06}"
Expand Down Expand Up @@ -585,6 +580,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerToys.Settings.DSC.Sche
{020A7474-3601-4160-A159-D7B70B77B15F} = {020A7474-3601-4160-A159-D7B70B77B15F}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PowerToys.Interop", "src\common\interop\PowerToys.Interop.vcxproj", "{F055103B-F80B-4D0C-BF48-057C55620033}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Expand Down Expand Up @@ -955,16 +952,6 @@ Global
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.ActiveCfg = Release|x64
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.Build.0 = Release|x64
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x86.ActiveCfg = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|ARM64.ActiveCfg = Debug|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|ARM64.Build.0 = Debug|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x64.ActiveCfg = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x64.Build.0 = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x86.ActiveCfg = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|ARM64.ActiveCfg = Release|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|ARM64.Build.0 = Release|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x64.ActiveCfg = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x64.Build.0 = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x86.ActiveCfg = Release|x64
{787B8AA6-CA93-4C84-96FE-DF31110AD1C4}.Debug|ARM64.ActiveCfg = Debug|ARM64
{787B8AA6-CA93-4C84-96FE-DF31110AD1C4}.Debug|ARM64.Build.0 = Debug|ARM64
{787B8AA6-CA93-4C84-96FE-DF31110AD1C4}.Debug|x64.ActiveCfg = Debug|x64
Expand Down Expand Up @@ -2601,6 +2588,18 @@ Global
{1D6893CB-BC0C-46A8-A76C-9728706CA51A}.Release|x64.Build.0 = Release|x64
{1D6893CB-BC0C-46A8-A76C-9728706CA51A}.Release|x86.ActiveCfg = Release|x64
{1D6893CB-BC0C-46A8-A76C-9728706CA51A}.Release|x86.Build.0 = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|ARM64.ActiveCfg = Debug|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|ARM64.Build.0 = Debug|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x64.ActiveCfg = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x64.Build.0 = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x86.ActiveCfg = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Debug|x86.Build.0 = Debug|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|ARM64.ActiveCfg = Release|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|ARM64.Build.0 = Release|ARM64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x64.ActiveCfg = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x64.Build.0 = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x86.ActiveCfg = Release|x64
{F055103B-F80B-4D0C-BF48-057C55620033}.Release|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2647,7 +2646,6 @@ Global
{748417CA-F17E-487F-9411-CAFB6D3F4877} = {2F305555-C296-497E-AC20-5FA1B237996A}
{217DF501-135C-4E38-BFC8-99D4821032EA} = {2F305555-C296-497E-AC20-5FA1B237996A}
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{F055103B-F80B-4D0C-BF48-057C55620033} = {5A7818A8-109C-4E1C-850D-1A654E234B0E}
{787B8AA6-CA93-4C84-96FE-DF31110AD1C4} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{08C8C05F-0362-41BC-818C-724572DF8B06} = {C140A3EF-6DBF-4084-9D4C-4EB5A99FEE68}
{5D00D290-4016-4CFE-9E41-1E7C724509BA} = {1AFB6476-670D-4E80-A464-657E01DFF482}
Expand Down Expand Up @@ -2817,6 +2815,7 @@ Global
{3A9A791E-94A9-49F8-8401-C11CE288D5FB} = {D1D6BC88-09AE-4FB4-AD24-5DED46A791DD}
{C0974915-8A1D-4BF0-977B-9587D3807AB7} = {D1D6BC88-09AE-4FB4-AD24-5DED46A791DD}
{1D6893CB-BC0C-46A8-A76C-9728706CA51A} = {557C4636-D7E1-4838-A504-7D19B725EE95}
{F055103B-F80B-4D0C-BF48-057C55620033} = {5A7818A8-109C-4E1C-850D-1A654E234B0E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}
Expand Down
8 changes: 3 additions & 5 deletions installer/PowerToysSetup/KeyboardManager.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
<?if $(sys.BUILDARCH) = x64 ?>
<File Source="$(var.BinDir)KeyboardManagerEditor\vcruntime140_1_app.dll" />
<?endif ?>
<!-- Latest CppWinRT upgrade made Keyboard Manager Editor depend on additional VC Runtime libraries. -->
<!-- These are not in the Keyboard Manager Editor build output. So we copy them from the base build directory. -->
<File Source="$(var.BinDir)vcruntime140.dll" />
<File Source="$(var.BinDir)vcruntime140_1.dll" />
<File Source="$(var.BinDir)msvcp140.dll" />
<File Source="$(var.BinDir)KeyboardManagerEditor\vcruntime140.dll" />
<File Source="$(var.BinDir)KeyboardManagerEditor\vcruntime140_1.dll" />
<File Source="$(var.BinDir)KeyboardManagerEditor\msvcp140.dll" />
</Component>
</DirectoryRef>

Expand Down
3 changes: 3 additions & 0 deletions src/common/AllExperiments/AllExperiments.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<ProjectReference Include="..\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
<ProjectReference Include="..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
</ItemGroup>

<!-- Experimentation is live, forcing inclusion -->
<ItemGroup Condition="'$(IsExperimentationLive)'!=''">
Expand Down
3 changes: 2 additions & 1 deletion src/common/Common.UI/Common.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@


<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.20348.0</TargetFramework>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<UseWPF>true</UseWPF>
<AssemblyName>PowerToys.Common.UI</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
<PackageReference Include="ControlzEx" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/common/FilePreviewCommon/FilePreviewCommon.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.20348.0</TargetFramework>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<Description>PowerToys FilePreviewCommon</Description>
<AssemblyName>PowerToys.FilePreviewCommon</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion src/common/ManagedCommon/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Globalization;
using System.IO.Abstractions;
using System.Reflection;
using interop;
using PowerToys.Interop;

namespace ManagedCommon
{
Expand Down
10 changes: 9 additions & 1 deletion src/common/ManagedCommon/ManagedCommon.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.20348.0</TargetFramework>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<Description>PowerToys ManagedCommon</Description>
<AssemblyName>PowerToys.ManagedCommon</AssemblyName>
</PropertyGroup>

<!-- See https://learn.microsoft.com/windows/apps/develop/platform/csharp-winrt/net-projection-from-cppwinrt-component for more info -->
<PropertyGroup>
<CsWinRTIncludes>PowerToys.Interop</CsWinRTIncludes>
<CsWinRTGeneratedFilesDir>$(OutDir)</CsWinRTGeneratedFilesDir>
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
<PackageReference Include="System.IO.Abstractions" />
<PackageReference Include="System.Management" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.20348.0</TargetFramework>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<Description>PowerToys Telemetry</Description>
<AssemblyName>PowerToys.ManagedTelemetry</AssemblyName>
Expand Down
39 changes: 39 additions & 0 deletions src/common/interop/CommonManaged.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "pch.h"
#include "CommonManaged.h"
#include "CommonManaged.g.cpp"
#include <common/version/version.h>
#include "../../modules/videoconference/VideoConferenceShared/MicrophoneDevice.h"
#include "../../modules/videoconference/VideoConferenceShared/VideoCaptureDeviceList.h"

namespace winrt::PowerToys::Interop::implementation
{
hstring CommonManaged::GetProductVersion()
{
return hstring{ get_product_version() };
}
winrt::Windows::Foundation::Collections::IVector<hstring> CommonManaged::GetAllActiveMicrophoneDeviceNames()
{
auto names = std::vector<winrt::hstring>();
for (const auto& device : MicrophoneDevice::getAllActive())
{
names.push_back(device->name().data());
}
return winrt::multi_threaded_vector(std::move(names));
}
winrt::Windows::Foundation::Collections::IVector<hstring> CommonManaged::GetAllVideoCaptureDeviceNames()
{
auto names = std::vector<winrt::hstring>();
VideoCaptureDeviceList vcdl;
vcdl.EnumerateDevices();

for (UINT32 i = 0; i < vcdl.Count(); ++i)
{
auto name = vcdl.GetDeviceName(i).data();
if (name != L"PowerToys VideoConference Mute")
{
names.push_back(name);
}
}
return winrt::multi_threaded_vector(std::move(names));
}
}
20 changes: 20 additions & 0 deletions src/common/interop/CommonManaged.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "CommonManaged.g.h"

namespace winrt::PowerToys::Interop::implementation
{
struct CommonManaged : CommonManagedT<CommonManaged>
{
CommonManaged() = default;

static hstring GetProductVersion();
static winrt::Windows::Foundation::Collections::IVector<hstring> GetAllActiveMicrophoneDeviceNames();
static winrt::Windows::Foundation::Collections::IVector<hstring> GetAllVideoCaptureDeviceNames();
};
}
namespace winrt::PowerToys::Interop::factory_implementation
{
struct CommonManaged : CommonManagedT<CommonManaged, implementation::CommonManaged>
{
};
}
11 changes: 11 additions & 0 deletions src/common/interop/CommonManaged.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace PowerToys
{
namespace Interop
{
[default_interface] static runtimeclass CommonManaged {
static String GetProductVersion();
static Windows.Foundation.Collections.IVector<String> GetAllActiveMicrophoneDeviceNames();
static Windows.Foundation.Collections.IVector<String> GetAllVideoCaptureDeviceNames();
}
}
}
146 changes: 146 additions & 0 deletions src/common/interop/Constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include "pch.h"
#include "Constants.h"
#include "Constants.g.cpp"
#include "shared_constants.h"
#include <ShlObj.h>

namespace winrt::PowerToys::Interop::implementation
{
uint32_t Constants::VK_WIN_BOTH()
{
return CommonSharedConstants::VK_WIN_BOTH;
}
hstring Constants::AppDataPath()
{
PWSTR local_app_path;
winrt::check_hresult(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &local_app_path));
winrt::hstring result{ local_app_path };
CoTaskMemFree(local_app_path);
result = result + L"\\" + CommonSharedConstants::APPDATA_PATH;
return result;
}
hstring Constants::PowerLauncherSharedEvent()
{
return CommonSharedConstants::POWER_LAUNCHER_SHARED_EVENT;
}
hstring Constants::PowerLauncherCentralizedHookSharedEvent()
{
return CommonSharedConstants::POWER_LAUNCHER_CENTRALIZED_HOOK_SHARED_EVENT;
}
hstring Constants::RunSendSettingsTelemetryEvent()
{
return CommonSharedConstants::RUN_SEND_SETTINGS_TELEMETRY_EVENT;
}
hstring Constants::RunExitEvent()
{
return CommonSharedConstants::RUN_EXIT_EVENT;
}
hstring Constants::FZEExitEvent()
{
return CommonSharedConstants::FZE_EXIT_EVENT;
}
hstring Constants::FZEToggleEvent()
{
return CommonSharedConstants::FANCY_ZONES_EDITOR_TOGGLE_EVENT;
}
hstring Constants::ColorPickerSendSettingsTelemetryEvent()
{
return CommonSharedConstants::COLOR_PICKER_SEND_SETTINGS_TELEMETRY_EVENT;
}
hstring Constants::ShowColorPickerSharedEvent()
{
return CommonSharedConstants::SHOW_COLOR_PICKER_SHARED_EVENT;
}
hstring Constants::ShowAdvancedPasteSharedEvent()
{
return CommonSharedConstants::SHOW_ADVANCED_PASTE_SHARED_EVENT;
}
hstring Constants::AdvancedPasteMarkdownEvent()
{
return CommonSharedConstants::ADVANCED_PASTE_MARKDOWN_EVENT;
}
hstring Constants::AdvancedPasteJsonEvent()
{
return CommonSharedConstants::ADVANCED_PASTE_JSON_EVENT;
}
hstring Constants::ShowPowerOCRSharedEvent()
{
return CommonSharedConstants::SHOW_POWEROCR_SHARED_EVENT;
}
hstring Constants::MouseJumpShowPreviewEvent()
{
return CommonSharedConstants::MOUSE_JUMP_SHOW_PREVIEW_EVENT;
}
hstring Constants::AwakeExitEvent()
{
return CommonSharedConstants::AWAKE_EXIT_EVENT;
}
hstring Constants::ShowPeekEvent()
{
return CommonSharedConstants::SHOW_PEEK_SHARED_EVENT;
}
hstring Constants::PowerAccentExitEvent()
{
return CommonSharedConstants::POWERACCENT_EXIT_EVENT;
}
hstring Constants::ShortcutGuideTriggerEvent()
{
return CommonSharedConstants::SHORTCUT_GUIDE_TRIGGER_EVENT;
}
hstring Constants::RegistryPreviewTriggerEvent()
{
return CommonSharedConstants::REGISTRY_PREVIEW_TRIGGER_EVENT;
}
hstring Constants::MeasureToolTriggerEvent()
{
return CommonSharedConstants::MEASURE_TOOL_TRIGGER_EVENT;
}
hstring Constants::GcodePreviewResizeEvent()
{
return CommonSharedConstants::GCODE_PREVIEW_RESIZE_EVENT;
}
hstring Constants::QoiPreviewResizeEvent()
{
return CommonSharedConstants::QOI_PREVIEW_RESIZE_EVENT;
}
hstring Constants::DevFilesPreviewResizeEvent()
{
return CommonSharedConstants::DEV_FILES_PREVIEW_RESIZE_EVENT;
}
hstring Constants::MarkdownPreviewResizeEvent()
{
return CommonSharedConstants::MARKDOWN_PREVIEW_RESIZE_EVENT;
}
hstring Constants::PdfPreviewResizeEvent()
{
return CommonSharedConstants::PDF_PREVIEW_RESIZE_EVENT;
}
hstring Constants::SvgPreviewResizeEvent()
{
return CommonSharedConstants::SVG_PREVIEW_RESIZE_EVENT;
}
hstring Constants::ShowHostsSharedEvent()
{
return CommonSharedConstants::SHOW_HOSTS_EVENT;
}
hstring Constants::ShowHostsAdminSharedEvent()
{
return CommonSharedConstants::SHOW_HOSTS_ADMIN_EVENT;
}
hstring Constants::CropAndLockThumbnailEvent()
{
return CommonSharedConstants::CROP_AND_LOCK_THUMBNAIL_EVENT;
}
hstring Constants::CropAndLockReparentEvent()
{
return CommonSharedConstants::CROP_AND_LOCK_REPARENT_EVENT;
}
hstring Constants::ShowEnvironmentVariablesSharedEvent()
{
return CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_EVENT;
}
hstring Constants::ShowEnvironmentVariablesAdminSharedEvent()
{
return CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_ADMIN_EVENT;
}
}
Loading

0 comments on commit fb5ed13

Please sign in to comment.