none
System.Windows.Controls.PrintDialog throws exception when I run under a different Windows Account RRS feed

  • Question

  • I have a code that works in my application if I am running the program with the same user I am logged as to windows:

    PrintDialog printDialog = new PrintDialog()
    printDialog.PrintDocument(((IDocumentPaginatorSource)flowDocument).DocumentPaginator, description);

    However, when I try to run my application with a different account (for example elevate as an administrator or Run By Different User), my application crashes on this part of the code. 

    I get the following exception:

    PrintTicket XML is not well-formed. Root element is missing.Root element is missing. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.Load(Stream inStream) at MS.Internal.Printing.Configuration.InternalPrintTicket..ctor(Stream xmlStream)

    The object PrintDialog would have errors too in it as shown:

    Also, I noticed that this exception wont happen if I build my program with Platform Target Any CPU or x64. However, I have to make i built as x86 for DLLS that I have.

    What could be the issue?



    Saturday, September 14, 2019 10:43 AM

All replies

  • Using the debugger, look at the XML, does it appear correct? Seems printing is not the issue from the stack trace shown.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, September 14, 2019 1:36 PM
  • Thank you for replying. I am not using XML at all. The same code works fine when I am not running my app as an elevated admin or a different user account. The errors in the PrintDialog object happens in the moment I create it (new PrintDialog()). I believe that for some reason my PrintTicket can't get access to a file the printer has for setting or something, and the XML exception happens for that!

     
    Saturday, September 14, 2019 10:06 PM
  • Thank you for replying. I am not using XML at all. The same code works fine when I am not running my app as an elevated admin or a different user account. The errors in the PrintDialog object happens in the moment I create it (new PrintDialog()). I believe that for some reason my PrintTicket can't get access to a file the printer has for setting or something, and the XML exception happens for that!

     
    For a simple test, can you move said file to the same folder as the executable, does the error still get thrown?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, September 15, 2019 2:10 AM
  • I am not fully understanding the test. Do I move a file to the folder pragmatically or with windows drag and drop?

    I had an a comment from someone on StackOverFlow that says:

    The instance constructor of PrintDialog is decorated with the SecurityCriticalAttribute. This means user elevation is not allowed for security reasons. Only the logged in user is allowed to open the dialog.

    Is this correct? I am confused since if I build my code as x64 or AnyCPU then I can print even if I run it as a different user. However, I must build it as x86 for DLLS that I can't change.


    Sunday, September 15, 2019 10:17 AM
  • Hi Muhannad,

    Thank you for posting here.

    I find System.Windows.Controls.PrintDialog comes from WPF app, so I will move it to Windows Presentation Foundation (WPF) forum to get professional support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 16, 2019 6:18 AM

  • Hi  Muhannad Alghamdi,

    >>The instance constructor of PrintDialog is decorated with the SecurityCriticalAttribute. This means user elevation is not allowed for security reasons. Only the logged in user is allowed to open the dialog.

    Is this correct? I am confused since if I build my code as x64 or AnyCPU then I can print even if I run it as a different user. However, I must build it as x86 for DLLS that I can't change.

    From your description, We don't know the exact cause of this phenomenon. I suggest you can go to the Developer Community  NET/Visual Studio and report your issue.

    It may can give you a reasonable Explanation. Thank you for your understanding.

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 17, 2019 2:49 AM
    Moderator