Home > Net >  PrintDlgEx invalid argument, while PrintDlg works
PrintDlgEx invalid argument, while PrintDlg works

Time:10-04

Problem: I need to get PrintDlgEx working for my project, but no combination of options or arguments works for me. It gives E_INVALIDARG for any combinations of options, as the ones I copied from Microsoft samples or other online samples.

Replacing PRINTDLGEX with PRINTDLG and PrintDlgEx with PrintDlg (and eliminating the group of options only from PRINTDLGEX) works fine.

Unfortunately I need PrintDlgEx, because I really need the Apply button, to change printers or property sheet without printing, for design and preview.

Please help me find why I can't get the dialog to show.

Code: while I simplified pieces, like what should happen on successful return, or setting DEVMODE and DEVNAMES, I tried this function exactly, with the same result: Invalid Argument.

#include <QDebug>
#include <QWindow>
#include <windows.h>
void showPrintDialog()
{
    // Simplifying the setup: real code passes in a QWidget *
    QWidget *caller = this;
    // Not returning a value or doing any work. I just want the dialog to pop up for now

    // Create the standard windows print dialog
    PRINTDLGEX printDialog;
    memset(&printDialog, 0, sizeof(PRINTDLGEX));
    printDialog.lStructSize = sizeof(PRINTDLGEX);

    printDialog.Flags = PD_RETURNDC |           // Return a printer device context. Without this, HDC may be undefined
                        PD_USEDEVMODECOPIESANDCOLLATE |
                        PD_NOSELECTION |        // Don't allow selecting individual document pages to print
                        PD_NOPAGENUMS |         // Disables some boxes
                        PD_NOCURRENTPAGE |      // Disables some boxes
                        PD_NONETWORKBUTTON |    // Don't allow networking  (but it show "Find printer") so what does this do ?
                        PD_HIDEPRINTTOFILE;     // Don't allow print to file

    // Only on PRINTDLGEX
        // Theis block copied from https://learn.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes?redirectedfrom=MSDN
        // I have tried multiple combinations of options, including none, I really don't want any of them
        printDialog.nStartPage = START_PAGE_GENERAL;
        printDialog.nPageRanges = 1;
        printDialog.nMaxPageRanges = 10;
        LPPRINTPAGERANGE pageRange = (LPPRINTPAGERANGE) GlobalAlloc(GPTR, 10 * sizeof(PRINTPAGERANGE));
        printDialog.lpPageRanges = pageRange;
        printDialog.lpPageRanges[0].nFromPage = 1;
        printDialog.lpPageRanges[0].nToPage = 1;
        printDialog.Flags2 = 0;
        printDialog.ExclusionFlags = 0;
        printDialog.dwResultAction = 0; // This will tell me if PRINT

        // Rest of options are also on PRINTDLG
        printDialog.nMinPage = 1;
        printDialog.nMaxPage = 10;

    // The only options I really need
    printDialog.nCopies = 1;
    printDialog.hDevMode = Q_NULLPTR;   // which will be better once this works
    printDialog.hDevNames = Q_NULLPTR;   // which will be better once this works
    printDialog.hwndOwner = reinterpret_cast<HWND>(caller->windowHandle()->winId());

    // Calling...
    int result = PrintDlgEx(&printDialog);
    qDebug() << (result == E_INVALIDARG ? "Invalid Argument\n" : "Success\n");

    // It always is E_INVALIDARG

    // Cleanup
    if (printDialog.hDevMode)
       GlobalFree(printDialog.hDevMode);
    if (printDialog.hDevNames)
       GlobalFree(printDialog.hDevNames);
    if (printDialog.hDC)
       DeleteDC(printDialog.hDC);
}

Platform: Windows 10, latest update;
Qt version: 5.12.7 or higher (since in VM I have 5.15.1)
The fact that I am running in Qt should be irrelevant, since this is all WIN API, beyond the c version (11)

CodePudding user response:

I can make your example work if I remove PD_NONETWORKBUTTON flag.

Please note that while it is documented for PRINTDLGA struct, it is NOT listed in PRINTDLGEXA

NOTE: I did get the same error with that flag.

  • Related