Skip to content

Simplify dwrite.dll loading process #6538

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

Merged
merged 2 commits into from
Apr 27, 2023
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
30 changes: 1 addition & 29 deletions src/Microsoft.DotNet.Wpf/src/Shared/cpp/dwriteloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,9 @@
namespace WPFUtils
{

#if defined(__cplusplus_cli)
#endif
HMODULE LoadDWriteLibraryAndGetProcAddress(void **pfncptrDWriteCreateFactory)
{
HMODULE hDWriteLibrary = nullptr;

// KB2533623 introduced the LOAD_LIBRARY_SEARCH_SYSTEM32 flag. It also introduced
// the AddDllDirectory function. We test for presence of AddDllDirectory as an
// indirect evidence for the support of LOAD_LIBRARY_SEARCH_SYSTEM32 flag.
HMODULE hKernel32 = GetModuleHandle(L"kernel32.dll");
if (hKernel32 != nullptr)
{
if (GetProcAddress(hKernel32, "AddDllDirectory") != nullptr)
{
// All supported platforms newer than Vista SP2 shipped with dwrite.dll.
// On Vista SP2, the .NET servicing process will ensure that a MSU containing
// dwrite.dll will be delivered as a prerequisite - effectively guaranteeing that
// this following call to LoadLibraryEx(dwrite.dll) will succeed, and that it will
// not be susceptible to typical DLL planting vulnerability vectors.
hDWriteLibrary = LoadLibraryEx(L"dwrite.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
}
else
{
// LOAD_LIBRARY_SEARCH_SYSTEM32 is not supported on this OS.
// Fall back to using plain ol' LoadLibrary
// There is risk that this call might fail, or that it might be
// susceptible to DLL hijacking.
hDWriteLibrary = LoadLibrary(L"dwrite.dll");
}
}

HMODULE hDWriteLibrary = LoadLibraryEx(L"dwrite.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
Copy link
Member

@lindexi lindexi May 5, 2022

Choose a reason for hiding this comment

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

Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623 to be installed. See https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa

Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Argh! I miss that. But only Windows 7 SP1 is possible applicable since other OS does not supported by .NET 7
And even that seems to be KB2533623 required for .NET see dotnet/docs#20459

But I hardly can understand is ESU has this patch or not.

Copy link
Contributor

@Symbai Symbai May 5, 2022

Choose a reason for hiding this comment

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

Vista and 2008 Server are not in the list of supported OS (at least for .NET 6) (research took too long, at time of answering kant2002 answer was not there)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That should be slated for .NET 8 then. That way Win 7 would be out of support at release date (Nov 2023) and not important anymore.

if (hDWriteLibrary)
{
*pfncptrDWriteCreateFactory = GetProcAddress(hDWriteLibrary, "DWriteCreateFactory");
Expand Down