Hi,

I'm trying to get the printer name selected by the user with the PrintDlg() dialog.

I made a sample Win32 project with VS 2008, (with "Use Multi-Byte Character Set") and inserted the following code in the "About" callback part:

    PRINTDLG pd;
    BOOL rc;
    DEVMODE *dev;
    DEVNAMES *dvn;
    char *name;
    unsigned char *nme;

    memset(&pd, 0, sizeof(PRINTDLG));
    pd.lStructSize = sizeof(PRINTDLG);

    pd.hwndOwner   = hWnd;
    pd.Flags       = PD_NOPAGENUMS|PD_NOSELECTION|PD_RETURNDC|PD_COLLATE;
    pd.hDevMode    = NULL;     // Don't forget to free or store hDevMode.
    pd.hDevNames   = NULL;     // Don't forget to free or store hDevNames.
    pd.nCopies     = 1;
    pd.nFromPage   = 0xFFFF;
    pd.nToPage     = 0xFFFF;
    pd.nMinPage    = 1;
    pd.nMaxPage    = 0xFFFF;
    rc = PrintDlg(&pd);

    dev = (DEVMODE *) pd.hDevMode;
    nme = dev->dmDeviceName;
    dvn = (DEVNAMES *) pd.hDevNames;
    name = (char *)dvn;
    name = name + dvn->wDeviceOffset;

But neither nme nor name contain a meaningfull string.

What am I doing wrong?

I found my error. I have to use:

dvn = (DEVNAMES *) GlobalLock(pd.hDevNames);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.