[C++] Setting Printer to Duplex

Discussion in 'Web Design & Coding' started by MdSalih, May 8, 2005.

  1. MdSalih

    MdSalih The Boss

    Birmingham, UK
    I am trying to put together a program which will change the settings of a printer to Duplex Printing (Printing on both sides).

    Here is the code I am using:
       char PortName[256]    = {0};
       char DriverName[256]  = {0};
       char PrinterName[256] = {0};
       HANDLE hDevMode;
       LPDEVMODE pDevMode;
       // Lets load up all the printers into a ComboxBox so we can see them
       ListBox1->Items = Printer()->Printers;
       //Now lets go through them all and make the changes.
       for(int i = 0; i < ListBox1->Items->Count; i++)
         Printer()->PrinterIndex = i;
         Printer()->GetPrinter( PrinterName, DriverName, PortName, (unsigned int)hDevMode );
         if( hDevMode )
            String PrtName = PrinterName;
            // cast to a LPDEVMODE not PDEVMODE
            pDevMode = (LPDEVMODE) GlobalLock( hDevMode );
            if( pDevMode )
               if( pDevMode->dmFields & DM_DUPLEX )
                 pDevMode->dmDuplex = DMDUP_HORIZONTAL;
                  ShowMessage("Duplex Detected");
                 ShowMessage("Not Duplex");
                GlobalUnlock( hDevMode );
             Printer()->SetPrinter(PrinterName, DriverName, PortName, (unsigned int)hDevMode);
        }//loop end
    Now I've run this code, and it does change the dmDuplex to DMDUP_HORIZONTAL successfully, however when you go to print it does not print duplex.

    In addition to this, if I manually change the printer settings to print Duplex from the Printer Settings Dialog box you get in something like word, and then check the value of dmDuplex I find that it is unchanged, i.e. if it was DMDUP_SIMPLEX and I set it to Duplex printing in the dialog box, it will remain as DMDUP_SIMPLEX.

    Now, I am not sure what I am doing wrong. Do I need to update something once I have made the changes to DMDUP_HORIZONTAL so it will refresh it? Or am I barking up the wrong tree - do I need to look somewhere else?

    Would be v. grateful if someone can anyone shed some light on this. Thank you.