镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

Description

@gcaughey

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Oct 3, 2022
  2. jkotas commented on Oct 3, 2022

    @jkotas
    Member

    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*.

  3. added this to the 7.0.0 milestone on Oct 3, 2022
  4. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Oct 3, 2022
  5. ericstj commented on Oct 3, 2022

    @ericstj
    Member

    @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

  6. jeffschwMSFT commented on Oct 6, 2022

    @jeffschwMSFT
    Member

    Closing via #76582

  7. ghost locked as resolved and limited conversation to collaborators on Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions