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

CommandBarFlyout static initialization creates winrt objects using COM API prohibited during DllMain() #6826

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Changes from all 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
39 changes: 19 additions & 20 deletions dev/CommandBarFlyout/CommandBarFlyout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@
bool CommandBarFlyoutTrace::s_IsDebugOutputEnabled{ false };
bool CommandBarFlyoutTrace::s_IsVerboseDebugOutputEnabled{ false };

// List of AppBarButton dependency properties being listened to for raising the CommandBarFlyoutCommandBar::OnCommandBarElementDependencyPropertyChanged notifications.
// AppBarButton::IsCompact and AppBarButton::LabelPosition having no effect on an AppBarButton's rendering, when used as a secondary command, they are not present in the list.
winrt::DependencyProperty CommandBarFlyout::s_appBarButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount]
{
winrt::AppBarButton::IconProperty(),
winrt::AppBarButton::LabelProperty(),
nullptr
};

// List of AppBarToggleButton dependency properties being listened to for raising the CommandBarFlyoutCommandBar::OnCommandBarElementDependencyPropertyChanged notifications.
// AppBarToggleButton::IsCompact and AppBarToggleButton::LabelPosition having no effect on an AppBarToggleButton's rendering, when used as a secondary command, they are not present in the list.
winrt::DependencyProperty CommandBarFlyout::s_appBarToggleButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount]
{
winrt::AppBarToggleButton::IconProperty(),
winrt::AppBarToggleButton::LabelProperty(),
nullptr
};
// List of AppBarButton/AppBarToggleButton dependency properties being listened to for raising the CommandBarFlyoutCommandBar::OnCommandBarElementDependencyPropertyChanged notifications.
// IsCompact and LabelPosition have no effect on an AppBarButton's rendering, when used as a secondary command, they are not present in the list.
// These two arrays are initialized in the constructor instead of being statically initialized here because that would result in the initialization happening during
// dllmain and it is not valid to call COM apis at that time.
winrt::DependencyProperty CommandBarFlyout::s_appBarButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount]{ nullptr, nullptr, nullptr };
Copy link
Member Author

Choose a reason for hiding this comment

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

{ nullptr, nullptr, nullptr };

I don't love that this is required to initialize the array to nullptrs.
It is not possible to just use "{}", because the default construction of a winrt::Foo object is not null - it creates a new instance of the object. Or at least it does if the winrt type itself has a default constructor. DependencyProperty does not have a constructor, since they are never instantiated directly (instead they are returned by DependencyProperty::Register). So in this case "{}" would be a compiler error.

winrt::DependencyProperty CommandBarFlyout::s_appBarToggleButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount]{ nullptr, nullptr, nullptr };

CommandBarFlyout::CommandBarFlyout()
{
__RP_Marker_ClassById(RuntimeProfiler::ProfId_CommandBarFlyout);

if (SharedHelpers::IsRS4OrHigher() && s_appBarButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount - 1] == nullptr)
// Initialize s_appBarButtonDependencyProperties and s_appBarToggleButtonDependencyProperties if needed.
if (s_appBarButtonDependencyProperties[0] == nullptr)
{
s_appBarButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount - 1] = winrt::AppBarButton::KeyboardAcceleratorTextOverrideProperty();
s_appBarToggleButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount - 1] = winrt::AppBarToggleButton::KeyboardAcceleratorTextOverrideProperty();
s_appBarButtonDependencyProperties[0] = winrt::AppBarButton::IconProperty();
s_appBarButtonDependencyProperties[1] = winrt::AppBarButton::LabelProperty();

s_appBarToggleButtonDependencyProperties[0] = winrt::AppBarToggleButton::IconProperty();
s_appBarToggleButtonDependencyProperties[1] = winrt::AppBarToggleButton::LabelProperty();

if (SharedHelpers::IsRS4OrHigher())
{
s_appBarButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount - 1] = winrt::AppBarButton::KeyboardAcceleratorTextOverrideProperty();
s_appBarToggleButtonDependencyProperties[s_commandBarElementDependencyPropertiesCount - 1] = winrt::AppBarToggleButton::KeyboardAcceleratorTextOverrideProperty();
}
}

if (winrt::IFlyoutBase6 thisAsFlyoutBase6 = *this)
Expand Down