Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:34:41+00:00 2026-05-29T05:34:41+00:00

Hi i want to save the changes i made in printer preferences but they

  • 0

Hi i want to save the changes i made in printer preferences but they are ignored . I am doing this in c#
Please help me thanks

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    PrintDocument pd = new PrintDocument();

    [DllImport("kernel32.dll")]
    static extern IntPtr GlobalLock(IntPtr hMem);

    [DllImport("kernel32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] static extern bool GlobalUnlock(IntPtr hMem);

    [DllImport("kernel32.dll")]
    static extern IntPtr GlobalFree(IntPtr hMem);

    [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,[MarshalAs(UnmanagedType.LPWStr)] string pDeviceName,IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);                                                                                                                                                           
    private const int DM_IN_BUFFER = 8;
    private const int DM_OUT_BUFFER = 2;
    private const int DM_IN_PROMPT = 4;

    private void ShowPrinterProperties(PrinterSettings settings)
    {
        IntPtr hDevMode = settings.GetHdevmode(settings.DefaultPageSettings);

        IntPtr pDevMode = GlobalLock(hDevMode);

        DocumentProperties(this.Handle, IntPtr.Zero, settings.PrinterName, pDevMode, pDevMode, DM_IN_PROMPT);

        GlobalUnlock(hDevMode);

        settings.SetHdevmode(hDevMode);

        settings.DefaultPageSettings.SetHdevmode(hDevMode);

        GlobalFree(hDevMode);
    }

    private void Form1_Load(object sender, EventArgs e)
    {  
            // Add list of installed printers found to the combo box.
           // The pkInstalledPrinters string will be used to provide the display string.
           String pkInstalledPrinters;
           for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
           {
               pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
               comboBox1.Items.Add(pkInstalledPrinters);
               //selectedPrinter = comboBox1.SelectedItem.ToString();
           }
    }

    private void button1_Click(object sender, EventArgs e)
    {
           if (pd.PrinterSettings.IsValid)
               ShowPrinterProperties(pd.PrinterSettings);
           else
               MessageBox.Show("Invalid printer name");
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        pd.PrinterSettings.PrinterName = comboBox1.Text;
       // selectedPrinter = comboBox1.SelectedItem.ToString();         

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:34:41+00:00Added an answer on May 29, 2026 at 5:34 am

    This should do the trick, tried and tested (make sure you have references to System.Drawing, System.Printing and ReachFramework assemblies):

        public static bool GetPrinterProperties(PrinterDescription selectedPrinter, bool showPrintingPreferencesDialog)
        {
            int modeGetSizeOfBuffer = 0;
            int modeCopy = 2;
            int modeOutBuffer = 14;
    
            IntPtr pointerHDevMode = new IntPtr();
            IntPtr pointerDevModeData = new IntPtr();
    
            try
            {
                PrintTicketConverter printTicketConverter = new PrintTicketConverter(selectedPrinter.FullName, selectedPrinter.ClientPrintSchemaVersion);
                IntPtr mainWindowPtr = new WindowInteropHelper(Application.Current.MainWindow).Handle;
                PrinterSettings printerSettings = new PrinterSettings();
    
                pointerHDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
                IntPtr pointerDevMode = GlobalLock(pointerHDevMode);
    
                int sizeNeeded = DocumentProperties(mainWindowPtr, IntPtr.Zero, selectedPrinter.FullName, IntPtr.Zero, pointerDevMode, modeGetSizeOfBuffer);
                pointerDevModeData = Marshal.AllocHGlobal(sizeNeeded);
    
                int result = -1;
    
                if (!showPrintingPreferencesDialog)
                {
                    result = DocumentProperties(mainWindowPtr, IntPtr.Zero, selectedPrinter.FullName, pointerDevModeData, pointerDevMode, modeCopy);
                }
                else
                {
                    result = DocumentProperties(mainWindowPtr, IntPtr.Zero, selectedPrinter.FullName, pointerDevModeData, pointerDevMode, modeOutBuffer);
                }
    
                GlobalUnlock(pointerHDevMode);
    
                if (result == 1)
                {
                    byte[] devMode = new byte[sizeNeeded];
                    Marshal.Copy(pointerDevModeData, devMode, 0, sizeNeeded);
    
                    // set back new printing settings to selected printer.
                    selectedPrinter.DefaultPrintTicket = printTicketConverter.ConvertDevModeToPrintTicket(devMode);
    
                    return true;
                }
            }
            finally
            {
                GlobalFree(pointerHDevMode);
                Marshal.FreeHGlobal(pointerDevModeData);
            }
    
            return false;
        }
    

    The PrinterDescription class is as follows:

        public class PrinterDescription
        {
            public string FullName { get; set; }
            public int ClientPrintSchemaVersion { get; set; }
            public PrintTicket DefaultPrintTicket { get; set; }
        }
    

    And the following imports are used:

        [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);
    
        [DllImport("kernel32.dll")]
        private static extern IntPtr GlobalLock(IntPtr hMem);
    
        [DllImport("kernel32.dll")]
        private static extern bool GlobalUnlock(IntPtr hMem);
    
        [DllImport("kernel32.dll")]
        private static extern bool GlobalFree(IntPtr hMem);
    

    After the dialog is ok’d, the print ticket will be updated. You may want to return the print ticket here, or use a copy of the default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I've made changes to some files but I don't want to check them
I want to save and load my xml data using XmlReader. But I don't
I want to save a remote img-file to my server, but I don't know
If I want to save any changes in a table, previously saved in SQL
I want to create previous versions so a user can undo changes made and
I Want to save the value of an inplace if it was changed. The
I want save the username and password when user login, and I added the
I want to save my program's DataModel objects to a file, and be able
I want to save a local copy of xml being ouput by a certain
I want to save a matrix to a text file, so I can read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.