DocumentProperties (winspool)
Last changed: Anisan-179.178.48.53

.
Summary
Brings up a printer properties page

C# Signature:

[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);

VB Signature:

Declare Function DocumentProperties Lib "winspool.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Doesn't work on a x64 system. (i.e. this declaration is wrong for x64, I'm just not quite sure why.). MH:I tested the code extensivly and I think the param "IntPtr pDevModeInput" must not have the "ref" flag. If it has the initial state of the dialog is wrong. EP: Yup, you're right! Thanks!!! I've been trying to figure out why it doesn't work on some drivers for a few weeks now! "ref" removed.

Hello and thx this sample was a great help however i had this x64 issue 2 so i invstigated a bit and found out that "DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, pDevMode, 0);" returns -1 so the devModeData will not be created! I am still working and trying to find a workaround for this

Tips & Tricks:

Please add some!

Sample Code:

private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)
    {
        IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
        IntPtr pDevMode = GlobalLock(hDevMode);
        int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, pDevMode, 0);
        IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
        DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, 14);
        GlobalUnlock(hDevMode);
        printerSettings.SetHdevmode(devModeData);
        printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
        GlobalFree(hDevMode);
        Marshal.FreeHGlobal(devModeData);
    }

Documentation