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

System.AccessViolationException thrown by PrintDocument.Print when platform is X86 #76538

Closed
gcaughey opened this issue Oct 3, 2022 · 5 comments

Comments

@gcaughey
Copy link

gcaughey commented Oct 3, 2022

Description

PrintDocument.Print works fine for x86 builds with .NET 6.0.9 but throws exception in .NET 7 RC1. This only happens if the platform is x86.

Reproduction Steps

Create new WPF desktop application.

Change csproj so it looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

Add a button to MainWindow and change MainWindow.xaml.cs so it looks like this:

using System.Drawing;
using System.Drawing.Printing;
using System.Windows;


namespace TestPrintNet7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string text = "Hello world";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (PrintDocument doc = new PrintDocument())
            {
                doc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                doc.Print();   // exception here!!!
                doc.PrintPage -= doc_PrintPage;
            }
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            using (Font font = new Font("Consolas", 9, System.Drawing.FontStyle.Regular))
            {
                e.Graphics.DrawString(text, font, System.Drawing.Brushes.Black, 0, 0);
            }
        }
    }
}

Expected behavior

Text should be printed to file selected by Microsoft Print to PDF printer.

Actual behavior

Exception thrown on the line
doc.Print();

Regression?

Works fine in .NET 6.

Known Workarounds

none

Configuration


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

The problem only happens on .NET 7 with x86 platform target.

Tested on Windows build 25211.1001. The machine architecture is x64.

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 3, 2022
@ghost
Copy link

ghost commented Oct 3, 2022

Tagging subscribers to this area: @dotnet/area-system-drawing
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

PrintDocument.Print works fine for x86 builds with .NET 6.0.9 but throws exception in .NET 7 RC1. This only happens if the platform is x86.

Reproduction Steps

Create new WPF desktop application.

Change csproj so it looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

Add a button to MainWindow and change MainWindow.xaml.cs so it looks like this:

using System.Drawing;
using System.Drawing.Printing;
using System.Windows;


namespace TestPrintNet7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string text = "Hello world";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (PrintDocument doc = new PrintDocument())
            {
                doc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                doc.Print();   // exception here!!!
                doc.PrintPage -= doc_PrintPage;
            }
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            using (Font font = new Font("Consolas", 9, System.Drawing.FontStyle.Regular))
            {
                e.Graphics.DrawString(text, font, System.Drawing.Brushes.Black, 0, 0);
            }
        }
    }
}

Expected behavior

Text should be printed to file selected by Microsoft Print to PDF printer.

Actual behavior

Exception thrown on the line
doc.Print();

Regression?

Works fine in .NET 6.

Known Workarounds

none

Configuration


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

The problem only happens on .NET 7 with x86 platform target.

Tested on Windows build 25211.1001. The machine architecture is x64.

Other information

No response

Author: gcaughey
Assignees: -
Labels:

area-System.Drawing, untriaged

Milestone: -

@ghost
Copy link

ghost commented Oct 3, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

PrintDocument.Print works fine for x86 builds with .NET 6.0.9 but throws exception in .NET 7 RC1. This only happens if the platform is x86.

Reproduction Steps

Create new WPF desktop application.

Change csproj so it looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

Add a button to MainWindow and change MainWindow.xaml.cs so it looks like this:

using System.Drawing;
using System.Drawing.Printing;
using System.Windows;


namespace TestPrintNet7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string text = "Hello world";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (PrintDocument doc = new PrintDocument())
            {
                doc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                doc.Print();   // exception here!!!
                doc.PrintPage -= doc_PrintPage;
            }
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            using (Font font = new Font("Consolas", 9, System.Drawing.FontStyle.Regular))
            {
                e.Graphics.DrawString(text, font, System.Drawing.Brushes.Black, 0, 0);
            }
        }
    }
}

Expected behavior

Text should be printed to file selected by Microsoft Print to PDF printer.

Actual behavior

Exception thrown on the line
doc.Print();

Regression?

Works fine in .NET 6.

Known Workarounds

none

Configuration


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

The problem only happens on .NET 7 with x86 platform target.

Tested on Windows build 25211.1001. The machine architecture is x64.

Other information

No response

Author: gcaughey
Assignees: -
Labels:

area-System.Drawing, area-System.Runtime.InteropServices, untriaged

Milestone: -

@jkotas
Copy link
Member

jkotas commented Oct 3, 2022

AV at:

00 02f7f1ac 76c7c7f6     gdi32full!StartDocWImpl+0xe9 [clientcore\windows\core\ntgdi\client\full\output.c @ 3639] 
01 02f7f1c0 58cc1503     GDI32!StartDocW+0x26 [onecoreuap\windows\core\ntgdi\client\base\stubs.c @ 1167] 
02 02f7f270 58cb654b     System_Drawing_Common!Interop.Gdi32.StartDoc+0x93 [_/src/libraries/System.Drawing.Common/src/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @ 24221] 
03 02f7f29c 58cb4731     System_Drawing_Common!System.Drawing.Printing.StandardPrintController.OnStartPrint+0xdb [_/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs @ 43] 

Looks like a bug in the LibraryImportGenerator refactoring. DOCINFO argument needs to be passed in as DOCINFO*.

@jkotas jkotas added this to the 7.0.0 milestone Oct 3, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Oct 3, 2022
@jkotas jkotas added the bug label Oct 3, 2022
@ericstj
Copy link
Member

ericstj commented Oct 3, 2022

@jkoritzinsky can you have a look as it seems caused by ccd67b0#diff-0004b77bd28bade5f8de26f55217352ec12d8af3c7de3d2c344f8e4ff8c38cebR51

Given we don't have 100% coverage of all of System.Drawing.Common I wonder if we should audit the generator conversion for other mistakes - or revert it for that assembly.

cc @JeremyKuhne

@jeffschwMSFT
Copy link
Member

Closing via #76582

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants