-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathWindowsAppRuntimeInsights.h
More file actions
137 lines (120 loc) · 5.56 KB
/
WindowsAppRuntimeInsights.h
File metadata and controls
137 lines (120 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#pragma once
#if __has_include(<wil/tracelogging.h>)
#ifndef __WINDOWSAPPRUNTIMEINSIGHTS_INCLUDED
#define __WINDOWSAPPRUNTIMEINSIGHTS_INCLUDED
#ifdef __WIL_TRACELOGGING_H_INCLUDED
#error "WIL Tracelogging.h must not be explicitly included when including this file"
#endif
#include <wil/resource.h>
#include <string>
#include <appmodel.h>
#include <FrameworkUdk/Containment.h>
// Bug 61555948: [1.8.6 servicing] AppRuntime_Insights - Add IsPackagedProcess and IsSelfContained to common insights PartB fields
#define WINAPPSDK_CHANGEID_61555948 61555948, WinAppSDK_1_8_7
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:
static std::string WindowsAppRuntimeVersion()
{
const uint32_t c_versionResourceId{ 10000 };
static std::string version{ LoadStringFromResource(c_versionResourceId) };
return version;
}
static std::string WindowsAppRuntimeChannel()
{
const uint32_t c_channelResourceId{ 10001 };
static std::string channel{ LoadStringFromResource(c_channelResourceId) };
return channel;
}
// Inlined from the canonical implementations (see for reference):
// IsPackagedProcess: dev/Common/AppModel.Identity.IsPackagedProcess.h
// IsSelfContained: dev/Common/WindowsAppRuntime.SelfContained.h / .cpp
// Duplicated here to avoid exposing those headers as public API.
static std::uint32_t TraceLoggingInformationFlags()
{
static std::uint32_t flags{ []() -> std::uint32_t {
auto f{ Insights::TraceLoggingInformationFlags::None };
if (wil::details::IsDebuggerPresent())
{
f |= Insights::TraceLoggingInformationFlags::IsDebuggerPresent;
}
if (WinAppSdk::Containment::IsChangeEnabled<WINAPPSDK_CHANGEID_61555948>())
{
{
UINT32 n{};
const auto rc{ ::GetCurrentPackageFullName(&n, nullptr) };
if ((rc != APPMODEL_ERROR_NO_PACKAGE) && (rc != ERROR_INSUFFICIENT_BUFFER))
{
LOG_HR(HRESULT_FROM_WIN32(rc));
}
else if (rc == ERROR_INSUFFICIENT_BUFFER)
{
f |= Insights::TraceLoggingInformationFlags::IsPackagedProcess;
}
}
{
auto module{ ::GetModuleHandleW(L"Microsoft.WindowsAppRuntime.dll") };
if (module)
{
using IsSelfContainedFn = HRESULT(__stdcall*)(BOOL*);
auto fn{ reinterpret_cast<IsSelfContainedFn>(::GetProcAddress(module, "WindowsAppRuntime_IsSelfContained")) };
if (fn)
{
BOOL result{};
if (SUCCEEDED_LOG(fn(&result)) && result)
{
f |= Insights::TraceLoggingInformationFlags::IsSelfContained;
}
}
}
}
}
return static_cast<std::uint32_t>(f);
}() };
return flags;
}
private:
static std::string LoadStringFromResource(uint32_t id)
{
const uint32_t c_ResourceMaxLength{ 100 };
char resourceValue[c_ResourceMaxLength]{};
static wil::unique_hmodule module{ LoadResourceModule() };
if (module)
{
const auto resourceValueLength{ ::LoadStringA(module.get(), id, resourceValue, ARRAYSIZE(resourceValue)) };
LOG_LAST_ERROR_IF_MSG(resourceValueLength == 0, "Failed to load resource string. id: %u", id);
}
return resourceValue;
}
static wil::unique_hmodule LoadResourceModule()
{
const PCWSTR c_resourceDllName{ L"Microsoft.WindowsAppRuntime.Insights.Resource.dll" };
wil::unique_hmodule resourceDllHandle(::LoadLibraryW(c_resourceDllName));
LOG_LAST_ERROR_IF_NULL_MSG(resourceDllHandle, "Unable to load resource dll. %ls", c_resourceDllName);
return resourceDllHandle;
}
};
}
#define _GENERIC_PARTB_FIELDS_ENABLED \
TraceLoggingStruct(4, "COMMON_WINDOWSAPPSDK_PARAMS"), \
TraceLoggingString(::Microsoft::WindowsAppRuntime::Insights::RuntimeInformation::WindowsAppRuntimeVersion().c_str(), "Version"), \
TraceLoggingString(::Microsoft::WindowsAppRuntime::Insights::RuntimeInformation::WindowsAppRuntimeChannel().c_str(), "WindowsAppSDKChannel"), \
TraceLoggingUInt32(::Microsoft::WindowsAppRuntime::Insights::RuntimeInformation::TraceLoggingInformationFlags(), "Flags"), \
TraceLoggingBool(true, "UTCReplace_AppSessionGuid")
#include <wil/tracelogging.h>
#endif // __WINDOWSAPPRUNTIMEINSIGHTS_INCLUDED
#else
#error "WIL package must be referenced before including this header"
#endif