Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Created January 27, 2025 05:28
Show Gist options
  • Save Aetopia/f9f339b0907c640d64a8699d4dd68772 to your computer and use it in GitHub Desktop.
Save Aetopia/f9f339b0907c640d64a8699d4dd68772 to your computer and use it in GitHub Desktop.
Some C++/WinRT class, I made.
#include <windows.h>
#include <initguid.h>
#include <shobjidl.h>
#include <winrt/base.h>
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.System.Diagnostics.h>
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.System.Threading.h>
using namespace winrt;
using namespace winrt::Windows::System::Threading;
using namespace winrt::Windows::ApplicationModel;
using namespace winrt::Windows::System;
using namespace winrt::Windows::System::Diagnostics;
using namespace winrt::Windows::Foundation;
class App
{
public:
App(std::wstring_view value)
{
AppInfo = AppInfo::GetFromAppUserModelId(value);
}
bool Running()
{
return std::ranges::any_of(
AppDiagnosticInfo::RequestInfoForAppAsync(AppInfo.AppUserModelId()).get(), [](AppDiagnosticInfo const &_) {
return std::ranges::any_of(_.GetResourceGroups(), [](AppResourceGroupInfo const &_) {
return _.GetProcessDiagnosticInfos().Size();
});
});
}
ProcessDiagnosticInfo Launch()
{
DWORD processId = {};
Manager->ActivateApplication(AppInfo.AppUserModelId().c_str(), nullptr, AO_NOERRORUI | AO_NOSPLASHSCREEN,
&processId);
return ProcessDiagnosticInfo::TryGetForProcessId(processId);
}
void Terminate(bool value = false)
{
auto packageFullName = AppInfo.Package().Id().FullName();
if (value)
Settings->TerminateAllProcesses(packageFullName.c_str());
else
{
Settings->StartServicing(packageFullName.c_str());
Settings->StopServicing(packageFullName.c_str());
}
}
IAsyncOperation<ProcessDiagnosticInfo> LaunchAsync()
{
ProcessDiagnosticInfo _ = nullptr;
co_await ThreadPool::RunAsync([&_, this](auto const &) { _ = Launch(); });
co_return _;
}
IAsyncAction TerminateAsync(bool value = false)
{
co_await ThreadPool::RunAsync([&value, this](auto const &) { Terminate(value); });
}
private:
AppInfo AppInfo = nullptr;
inline static const com_ptr<IApplicationActivationManager> Manager =
create_instance<IApplicationActivationManager>(CLSID_ApplicationActivationManager);
inline static const com_ptr<IPackageDebugSettings> Settings =
create_instance<IPackageDebugSettings>(CLSID_PackageDebugSettings);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment