How to show printer properties/preferences dialog and save changes?

后端 未结 4 631
日久生厌
日久生厌 2020-12-09 00:23

EDIT: My fault! I expected the changes to be written back to the default printer settings when in fact only the local instance of the PrinterSettings are changed. -

4条回答
  •  执念已碎
    2020-12-09 00:56

    If you target x86 compilation and run from a x64 machine, the code from Jeff Roe will not work: when allocating devModeData, DocumentPropreties will always fail and returns a sizeNeeded of -1, with a LastError code 13.

    To solve the problem, either make sure you target AnyCPU or just change the call to DocumentPropreties to the following:

    int sizeNeeded = DocumentProperties(pHandle, 
                                        IntPtr.Zero, 
                                        printerSettings.PrinterName, 
                                        IntPtr.Zero, // This solves it
                                        pDevMode, 
                                        fMode);
    

    Using IntPtr.Zero instead of a proper pointer to a DevMode structure looks wrong, but that first call to DocumentProperties does not attempt to modify the memory at that position. The only data returned by the call is the memory size needed to store the device mode data that represents the internal parameters of the print driver.

    Reference:

    • PInvoke.Net page on DocumentProperties

提交回复
热议问题