Hi All

I have written a code to change the settings of printer Orientation(i.e. Landscape or Portrait)

I have written following code

void SetDefaultPrinterOrientation(short dmOrientation)
{
    HANDLE hPrinter = NULL;
    LPTSTR pPrinterName = NULL;

    DWORD dwNeeded = 0;
    PRINTER_INFO_2 *pi2 = NULL;
    DEVMODE *pDevMode = NULL;
    PRINTER_DEFAULTS pd;
    BOOL bFlag;
    LONG lFlag;
    
    DWORD size;
    
    //    Get the printer's Name
    pPrinterName = GetPrinterNameFunc();
    
    ZeroMemory(&pd, sizeof(pd));
    pd.DesiredAccess = PRINTER_ALL_ACCESS;
    
    //     Open a printer
    //     Put the instace of printer in hPrinter
    bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);    

    //    Fill the value of dwNeeded
    bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);

    pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
   
   bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
   
   if (pi2->pDevMode == NULL)
   {
       dwNeeded = DocumentProperties(NULL, hPrinter, 
                         pPrinterName,NULL, NULL, 0);        
                         
       pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
       
       lFlag = DocumentProperties(NULL, hPrinter, 
                     pPrinterName, pDevMode, NULL,DM_OUT_BUFFER);
                     
       pi2->pDevMode = pDevMode;
    }
     
    pi2->pDevMode->dmFields = DM_PAPERSIZE; 
    // Now, change it to whatever was requested by the calling application


int OriginalOrientation = pi2->pDevMode->dmOrientation;


    pi2->pDevMode->dmOrientation = dmOrientation;

    pi2->pSecurityDescriptor = NULL;
    
    lFlag = DocumentProperties(NULL, hPrinter, pPrinterName, 
            pi2->pDevMode, pi2->pDevMode, 
            DM_IN_BUFFER | DM_OUT_BUFFER);
    
   bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
   

   
   SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L, 
             (LPARAM)(LPCSTR)pPrinterName, SMTO_NORMAL, 1000, NULL);
    //  Dispose all the resources
    
   
    if (pi2)
        GlobalFree(pi2);
    if (hPrinter)
        ClosePrinter(hPrinter);
    if (pDevMode)
        GlobalFree(pDevMode);

}

But the settings are not changed

Am I missing something.

Kindly advice

Regards
Karan

Recommended Answers

All 2 Replies

Shouldn't you be using pi2->pDevMode->dmFields = [B]DM_ORIENTATION[/B]; instead of pi2->pDevMode->dmFields = DM_PAPERSIZE;

It works Thanks :)

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.