Solved

DocumentProperties & SetForm

Posted on 2003-11-27
4
691 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
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now