dotnet / dotnet/wpf

.NET applications are crashing after the MCF application sets the FontWeight and FontType

Open
#3,143 2 comments 0 reactions 0 assignees View on GitHub
.NET Framework Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

.NET applications are crashing after the MCF application sets the FontWeight and FontType using the below code :
SystemParamerterInfo(SPI_SETNONCLIENTMETRICS,0,&ncm,0)

**Workaround :**
The simple workaround is for the C++ application to ensure it is setting the font weight between 1 and 999 prior to calling SystemParametersInfo(SPI_SETNONCLIENTMETRICS). Use FW_NORMAL (400) instead of FW_DONTCARE (0). The C++ application should also be at least system DPI aware if the application will be running on systems with the DPI scale >= 150%.

**Repro Code :**
1. We need to first run the C++ application and then try running the WPF application to reproduce the issue.
2. We tried creating a simple WPF application, using the following XAML for the main window:
```








Paragraph in a Table Cell.










```
3. We then created a small C++ console application that does the following:
```
int wmain()
{
NONCLIENTMETRICSW ncm = {};
ncm.cbSize = sizeof(NONCLIENTMETRICSW);

BOOL fResult = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
if (fResult)
{
LONG lfWeightPrev = ncm.lfMessageFont.lfWeight;
ncm.lfMessageFont.lfWeight = 0; // FW_DONTCARE

fResult = SystemParametersInfo(SPI_SETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
if (fResult)
{
_getwch();
ncm.lfMessageFont.lfWeight = lfWeightPrev;
fResult = SystemParametersInfo(SPI_SETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
}
}
return GetLastError();
}
```

 4. We are able to reproduce the crash when running the WPF application.

**Expected Results :**
1. The WPF application should work fine.

**Actual Results :**
1. The WPF application crashes with below exception :
0:000> !PrintException /d 02bc06c0
Exception object: 02bc06c0
Exception type: System.ArgumentOutOfRangeException
Message: The parameter value must be between '1' and '999'.
InnerException:
StackTrace (generated):
SP IP Function
00AFDE78 68CD472B PresentationCore_ni!System.Windows.FontWeight.FromOpenTypeWeight(Int32)+0x7d272b
00AFDE8C 5615BE6A PresentationFramework_ni!System.Windows.Documents.TextElement..cctor()+0x192

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.