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);
 
umaiyerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.