Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DocumentProperties & SetForm

Posted on 2003-11-27
4
Medium Priority
?
709 Views
Last Modified: 2013-12-03
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
Comment
Question by:umaiyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 9843825
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10883584
PAQed - no points refunded (of 250)

Computer101
E-E Admin
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question