• Advertisement
Sign in to follow this  

Win32: Getting printer name from DEVNAME or DEVMODE struct

This topic is 2908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am programming in Vista 32 bit. I use PrintDlg/PrintDlgEx to open a print dialog box. I don't use the PD_RETURNDC flag. The dialog box returns with no errors but the device name that appears in DEVMODE is corrupt, and the offset that appears in DEVNAMES is bad. Here is the code (copied from MSDN article):
 HRESULT hResult;

    // Allocate the PRINTDLGEX structure.
    pPDX = (LPPRINTDLGEX)GlobalAlloc(GPTR, sizeof(PRINTDLGEX));
    if (!pPDX)
        return E_OUTOFMEMORY;

    // Allocate an array of PRINTPAGERANGE structures.
    pPageRanges = (LPPRINTPAGERANGE) GlobalAlloc(GPTR, 10 * sizeof(PRINTPAGERANGE));
    if (!pPageRanges)
        return E_OUTOFMEMORY;

    //  Initialize the PRINTDLGEX structure.
    pPDX->lStructSize = sizeof(PRINTDLGEX);
 pPDX->hwndOwner = m_pd.hwndOwner;
    pPDX->hDevMode = NULL;
    pPDX->hDevNames = NULL;
    pPDX->hDC = NULL;
    pPDX->Flags = PD_COLLATE;
    pPDX->Flags2 = 0;
    pPDX->ExclusionFlags = 0;
    pPDX->nPageRanges = 0;
    pPDX->nMaxPageRanges = 10;
    pPDX->lpPageRanges = pPageRanges;
    pPDX->nMinPage = 1;
    pPDX->nMaxPage = 1000;
    pPDX->nCopies = 1;
    pPDX->hInstance = 0;
    pPDX->lpPrintTemplateName = NULL;
    pPDX->lpCallback = NULL;
    pPDX->nPropertyPages = 0;
    pPDX->lphPropertyPages = NULL;
    pPDX->nStartPage = START_PAGE_GENERAL;
    pPDX->dwResultAction = 0;
    //  Invoke the Print property sheet.
    hResult = PrintDlgEx(pPDX);

 DEVNAMES *dn = (DEVNAMES *) pPDX->hDevNames;
 DEVMODE *dm = (DEVMODE *) pPDX->hDevMode;
 STRING str = dn + (dn->wDeviceOffset);

Share this post

Link to post
Share on other sites
This is wrong here:

STRING str = dn + (dn->wDeviceOffset); // <-

The wDeviceOffset variable is the offset in bytes, and not in DEVNAMES structures, and you probably don't want a 'STRING' type either.

A simple cast to a constant string like

const char * str = (const char *) dn + dn->wDeviceOffset;

will probably already work for you.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement