Solved

DocumentProperties & SetForm

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application vs Workbook.applicaiton 3 41
Need to create an object factory 2 47
Windows Process running 3 58
VS2015 Redefinition errors 4 71
This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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