Skip to content
30 changes: 30 additions & 0 deletions dev/Common/AppModel.Identity.IsPackagedProcess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#ifndef __APPMODEL_IDENTITY_ISPACKAGEDPROCESS_H
Comment thread
agniuks marked this conversation as resolved.
Resolved
#define __APPMODEL_IDENTITY_ISPACKAGEDPROCESS_H

#include <appmodel.h>

namespace AppModel::Identity
{
inline HRESULT IsPackagedProcess_nothrow(bool& isPackagedProcess) noexcept
{
isPackagedProcess = false;
UINT32 n{};
Comment thread
agniuks marked this conversation as resolved.
Resolved
const auto rc{ ::GetCurrentPackageFullName(&n, nullptr) };
RETURN_HR_IF(HRESULT_FROM_WIN32(rc), (rc != APPMODEL_ERROR_NO_PACKAGE) && (rc != ERROR_INSUFFICIENT_BUFFER));
isPackagedProcess = (rc == ERROR_INSUFFICIENT_BUFFER);
return S_OK;
}

inline bool IsPackagedProcess()
{
UINT32 n{};
const auto rc{ ::GetCurrentPackageFullName(&n, nullptr) };
THROW_HR_IF_MSG(HRESULT_FROM_WIN32(rc), (rc != APPMODEL_ERROR_NO_PACKAGE) && (rc != ERROR_INSUFFICIENT_BUFFER), "GetCurrentPackageFullName rc=%d", rc);
return rc == ERROR_INSUFFICIENT_BUFFER;
}
}

#endif // __APPMODEL_IDENTITY_ISPACKAGEDPROCESS_H
9 changes: 2 additions & 7 deletions dev/Common/AppModel.Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
#include <memory>
#include <stdint.h>

#include "AppModel.Identity.IsPackagedProcess.h"

namespace AppModel::Identity
{
inline bool IsPackagedProcess()
{
UINT32 n{};
const auto rc{ ::GetCurrentPackageFullName(&n, nullptr) };
THROW_HR_IF_MSG(HRESULT_FROM_WIN32(rc), (rc != APPMODEL_ERROR_NO_PACKAGE) && (rc != ERROR_INSUFFICIENT_BUFFER), "GetCurrentPackageFullName rc=%d", rc);
return rc == ERROR_INSUFFICIENT_BUFFER;
}

template <typename Tstring>
inline Tstring GetCurrentPackageFullName()
Expand Down
17 changes: 17 additions & 0 deletions dev/Common/WindowsAppRuntime.SelfContained.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ inline bool IsSelfContained()
THROW_IF_FAILED(WindowsAppRuntime_IsSelfContained(&isSelfContained));
return !!isSelfContained;
}

/// Non-throwing variant using dynamic loading (safe before DLL is loaded).
inline HRESULT IsSelfContained_nothrow(bool& isSelfContained) noexcept
{
isSelfContained = false;
auto module = ::GetModuleHandleW(L"Microsoft.WindowsAppRuntime.dll");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silently swallows errors. if !module it would be better to report what happened

WIL is your friend :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!module is the normal case when the DLL isn't loaded yet (like during bootstrap). If we RETURN_LAST_ERROR_IF_NULL here it'd bubble up as a real error. Would something like LOG_LAST_ERROR_IF_NULL + still returning S_OK be a good middle ground?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, but in this case it is an error. It's the caller's decision if that's a problem for them, or if they want to ignore it as 'expected'

If this is called before the DLL is loaded (e.g. bootstrap) then would the caller hit this again later and get a different answer?

Given the TraceLogging callsite calls this on 1st use and caches the result the question is, does that 1st call happen after bootstrapping? If not how would that cached answer be revised - or would it always be wrong 'cause too early?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic still works out: self-contained apps already have the DLL loaded (no
bootstrap), so GetModuleHandleW succeeds on first call. Framework-dependent apps use the bootstrapper, and they aren't self-contained anyway, so the cached false is correct either way?

RETURN_LAST_ERROR_IF_NULL(module);
using IsSelfContainedFn = HRESULT(__stdcall*)(BOOL*);
auto fn = reinterpret_cast<IsSelfContainedFn>(
::GetProcAddress(module, "WindowsAppRuntime_IsSelfContained"));
RETURN_LAST_ERROR_IF_NULL(fn);
BOOL result{};
const auto hr{ fn(&result) };
RETURN_IF_FAILED(hr);
isSelfContained = !!result;
return S_OK;
}
}
#endif // defined(__cplusplus)

Expand Down
2 changes: 1 addition & 1 deletion dev/MRTCore/mrt/Core/src/MRM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<ClCompile>
<PreprocessorDefinitions>WIN32;_WINDOWS;_USRDLL;UNICODE;_UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatWarningAsError>true</TreatWarningAsError>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(MRTCoreRoot)\mrt\mrm\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(MRTCoreRoot)\mrt\mrm\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalDependencies>$(OutDir)..\mrmmin\mrmmin.lib;rpcrt4.lib;onecoreuap.lib;%(AdditionalDependencies)</AdditionalDependencies>
Expand Down
4 changes: 2 additions & 2 deletions dev/MRTCore/mrt/mrm/UnitTests/MrmBaseUnitTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
<ClCompile>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(MRTCoreRoot)\mrt\mrm\include;$(MRTCoreRoot)\mrt\mrm\mrmmin;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(MRTCoreRoot)\mrt\mrm\include;$(MRTCoreRoot)\mrt\mrm\mrmmin;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4309;4838;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(MRTCoreRoot)\mrt\mrm\include;$(MRTCoreRoot)\mrt\mrm\mrmmin;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(MRTCoreRoot)\mrt\mrm\include;$(MRTCoreRoot)\mrt\mrm\mrmmin;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4309;4838;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<Link>
Expand Down
2 changes: 1 addition & 1 deletion dev/MRTCore/mrt/mrm/mrmex/mrmex.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(MRTCoreRoot)\mrt\mrm\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(MRTCoreRoot)\mrt\mrm\include</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
Expand Down
2 changes: 1 addition & 1 deletion dev/MRTCore/mrt/mrm/mrmmin/mrmmin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(MRTCoreRoot)\mrt\mrm\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(MRTCoreRoot)\mrt\mrm\include</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -112,7 +112,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -122,7 +122,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -132,7 +132,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -143,7 +143,7 @@
<ClCompile>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -154,7 +154,7 @@
<ClCompile>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(RepoRoot)\dev\WindowsAppRuntime_Insights;$(RepoRoot)\dev\common;$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
40 changes: 39 additions & 1 deletion dev/WindowsAppRuntime_Insights/WindowsAppRuntimeInsights.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@

#include <wil/resource.h>
#include <string>
#include "AppModel.Identity.IsPackagedProcess.h"
#include "WindowsAppRuntime.SelfContained.h"
namespace Microsoft::WindowsAppRuntime::Insights
{
enum class TraceLoggingInformationFlags : std::uint32_t
{
None = 0,
IsDebuggerPresent = 0x00000001,
IsPackagedProcess = 0x00000002,
IsSelfContained = 0x00000004,
};
DEFINE_ENUM_FLAG_OPERATORS(TraceLoggingInformationFlags)

class RuntimeInformation
{
public:
Expand All @@ -32,6 +43,33 @@
return channel;
}

static std::uint32_t TraceLoggingInformationFlags()
{
static std::uint32_t flags{ []() -> std::uint32_t {
Comment thread
agniuks marked this conversation as resolved.
Resolved
auto f{ Insights::TraceLoggingInformationFlags::None };

if (wil::details::IsDebuggerPresent())
{
f |= Insights::TraceLoggingInformationFlags::IsDebuggerPresent;
}

bool isPackagedProcess{};
if (SUCCEEDED_LOG(::AppModel::Identity::IsPackagedProcess_nothrow(isPackagedProcess)) && isPackagedProcess)
{
f |= Insights::TraceLoggingInformationFlags::IsPackagedProcess;
}

bool isSelfContained{};
if (SUCCEEDED_LOG(::WindowsAppRuntime::SelfContained::IsSelfContained_nothrow(isSelfContained)) && isSelfContained)
{
f |= Insights::TraceLoggingInformationFlags::IsSelfContained;
}

return static_cast<std::uint32_t>(f);
}() };
return flags;
}

private:
static std::string LoadStringFromResource(uint32_t id)
{
Expand Down Expand Up @@ -60,7 +98,7 @@
TraceLoggingStruct(4, "COMMON_WINDOWSAPPSDK_PARAMS"), \
TraceLoggingString(::Microsoft::WindowsAppRuntime::Insights::RuntimeInformation::WindowsAppRuntimeVersion().c_str(), "Version"), \
TraceLoggingString(::Microsoft::WindowsAppRuntime::Insights::RuntimeInformation::WindowsAppRuntimeChannel().c_str(), "WindowsAppSDKChannel"), \
TraceLoggingBool(wil::details::IsDebuggerPresent(), "IsDebugging"), \
TraceLoggingUInt32(::Microsoft::WindowsAppRuntime::Insights::RuntimeInformation::TraceLoggingInformationFlags(), "Flags"), \
TraceLoggingBool(true, "UTCReplace_AppSessionGuid")

#include <wil/tracelogging.h>
Expand Down
Loading