Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

DocumentProperties & SetForm

I want to change the PaperSize of the printer in PrinterProperties
When we get the printerproperties Dialouge, Under the General tab we have Printing Preferences..On chosing that & clicking Advanced Button, we get the advanced properties of the printer.I want to change the PaperSize option in that.
How can we go about it.
I am attaching the code of what i have done.Can anybody tell me wha's wrong in that.
Suppose On installing the printer , PapaerSize is set to "AAA" & now i want to change it to "BBB" How am i supposed to do.
Here is the code snippet.

Paper Sizes are not the standard paper size.
It is printer specific.

HANDLE hPrinter = NULL;
DWORD dwNeeded = 0 , dwReturned = 0  ;
PRINTER_INFO_2 *pi2 = NULL;
DEVMODE *pDevMode = NULL;
FORM_INFO_1 *forminfo = NULL ;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;
unsigned int wd = 0 , ht = 0 ;
SIZEL size ;
FORM_INFO_1 temp ;
char printername[] = "My Printer" ;

ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
bFlag = OpenPrinter(printername, &hPrinter, &pd);
if (!bFlag || (hPrinter == NULL))
      return 0 ;
// The first GetPrinter tells you how big the buffer should be in
// order to hold all of PRINTER_INFO_2. Note that this should fail with
// ERROR_INSUFFICIENT_BUFFER.  If GetPrinter fails for any other reason
// or dwNeeded isn't set for some reason, then there is a problem...
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))
{
      ClosePrinter(hPrinter);
      return 0 ;
}
// Allocate enough space for PRINTER_INFO_2...
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
{
      ClosePrinter(hPrinter);
      return 0;
}
// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
                GlobalFree(pi2);
      ClosePrinter(hPrinter);
      return 0;
}
bFlag =  EnumForms(hPrinter,1,      0, 0, &dwNeeded, &dwReturned );
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))
{
      ClosePrinter(hPrinter);
      return 0 ;
}
forminfo = (FORM_INFO_1 *)GlobalAlloc(GPTR, dwNeeded);
if (forminfo == NULL)
{
      ClosePrinter(hPrinter);
      return 0;
}
bFlag =  EnumForms(hPrinter, 1, (LPBYTE)forminfo,              
                              dwNeeded ,&dwNeeded,&dwReturned );
if (!bFlag)
{
      GlobalFree(forminfo);
      ClosePrinter(hPrinter);
      return 0;
}
for(unsigned int i = 0 ; i < dwReturned ; i++)
{
                temp = forminfo[i] ;
      int j = _stricmp(temp.pName , "BBB") ;
      if(j == 0)
      {
           printf("\n %s" , temp.pName) ;
           bFlag =  SetForm(hPrinter,temp.pName,      1, (LPBYTE)&temp);
           unsigned int width = temp.Size.cx ; // width in thousandths of millimeters
           unsigned int height = temp.Size.cy ; //length in thousandths of millimeters
               wd = width * 100 ; // width in tenths of millimeters
          ht = height * 100 ; // length in tenths of millimeters
         //wd = width / 100 ; // width in tenths of millimeters
            //ht = height / 100 ; // length in tenths of millimeters
        break ;
      }
}
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
{
      dwNeeded = DocumentProperties(NULL, hPrinter,printername,NULL, NULL, 0);
      if (dwNeeded <= 0)
      {
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      return 0;
      }
      pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
     if (pDevMode == NULL)
     {
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      return 0;
     }
     lFlag = DocumentProperties(NULL, hPrinter,printername,pDevMode,
                       NULL,DM_OUT_BUFFER);
    if (lFlag != IDOK || pDevMode == NULL)
    {
      GlobalFree(pDevMode);
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      return 0;
    }
    pi2->pDevMode = pDevMode;
}
// Driver is reporting that it doesn't support this change...
if (!(pi2->pDevMode->dmFields & DM_PAPERSIZE))
{
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      if (pDevMode)
             GlobalFree(pDevMode);
      return 0;
}
if (!(pi2->pDevMode->dmFields & DM_PAPERLENGTH))
{
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      if (pDevMode)
           GlobalFree(pDevMode);
      return 0;
}
if (!(pi2->pDevMode->dmFields & DM_PAPERWIDTH))
{
                GlobalFree(pi2);
      ClosePrinter(hPrinter);
      if (pDevMode)
          GlobalFree(pDevMode);
      return 0;
}
// Driver is reporting that it doesn't support this change...
if (!(pi2->pDevMode->dmFields & DM_FORMNAME))
{
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      if (pDevMode)
            GlobalFree(pDevMode);
      return 0;
}
pi2->pDevMode->dmFields = DM_PAPERSIZE | DM_FORMNAME | DM_PAPERLENGTH
                                     | DM_PAPERWIDTH ;
for(int k = 0  ; k < 32 ; k++)
      pi2->pDevMode->dmFormName[k] = '\0' ;
int len = strlen(temp.pName) ;
for(k = 0  ; k < len ; k++)
      pi2->pDevMode->dmFormName[k] = temp.pName[k] ;
pi2->pDevMode->dmSize = sizeof(DEVMODE) ;
pi2->pDevMode->dmPaperSize = 0;
pi2->pDevMode->dmPaperLength = ht ;
pi2->pDevMode->dmPaperWidth = wd ;

// Do not attempt to set security descriptor...
pi2->pSecurityDescriptor = NULL;

// Make sure the driver-dependent part of devmode is updated...
lFlag = DocumentProperties(NULL, hPrinter,  printername,  pi2->pDevMode,
                    pi2->pDevMode,  DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      if (pDevMode)
            GlobalFree(pDevMode);
      return 0;
}
// Update printer information...
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
if (!bFlag)
// The driver doesn't support, or it is unable to make the change...
{
      GlobalFree(pi2);
      ClosePrinter(hPrinter);
      if (pDevMode)
            GlobalFree(pDevMode);
      return 0;
}
// Clean up...
if (pi2)
      GlobalFree(pi2);
if(forminfo)
      GlobalFree(forminfo) ;
if (hPrinter)
      ClosePrinter(hPrinter);
if (pDevMode)
      GlobalFree(pDevMode);
 
0
umaiyer
Asked:
umaiyer
1 Solution
 
DanRollinsCommented:
What errors are you getting?

That looks like a lot more code than should be needed.  Check out this example:
      http:/Cplusplus/Q_10264749.html
and this one in VB, uses API calls...
     http:/Visual_Basic/Q_20763455.html

Alos doa a sanity check... using the Control Panel, are you able to select the default desired paper using the interactive printer settings?   That is, make sure the printer supports the desired paper size.

-- Dan
0
 
Computer101Commented:
PAQed - no points refunded (of 250)

Computer101
E-E Admin
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now