?
Solved

Setting printer paper trays to a network printer  for a print job

Posted on 1999-01-24
8
Medium Priority
?
342 Views
Last Modified: 2013-12-03
How does one set the printer tray for a particular print job in Windows SDK programming ? I have tried CreateDC, ResetDC Setprinter? In dire need of this please help. This is VC++.
0
Comment
Question by:arjunram
[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
  • 3
8 Comments
 

Author Comment

by:arjunram
ID: 1419200
Adjusted points to 200
0
 
LVL 1

Expert Comment

by:arbitrary
ID: 1419201
what do u mean by tray -  paper size?

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1419202
Here is the working sample (console app)
1) run it a.exe - it'll print all trays & ids
2) run a.exe tray_id - it'll print test page

#include <windows.h>
#include <iostream.h>
#include <stdio.h>

// Usage: a.exe [tray_#]

PRINTDLG pdSetup;

void GetDefPrinter() {
  memset(&pdSetup,0,sizeof(PRINTDLG));
  pdSetup.lStructSize=sizeof(PRINTDLG);
  pdSetup.Flags=PD_RETURNDEFAULT;
  PrintDlg(&pdSetup);
}

void main(int argc,char* argv[])
{
   // Fill PRINTDLG struct
   GetDefPrinter();

   // Obtain printer info
   LPDEVMODE dm=LPDEVMODE(GlobalLock(pdSetup.hDevMode));
   LPDEVNAMES dn=LPDEVNAMES(GlobalLock(pdSetup.hDevNames));

   if(argc<2) { // Print trays info
      // Check trays
      DWORD nTrays=DeviceCapabilities(LPTSTR(dn)+dn->wDeviceOffset,
         LPTSTR(dn)+dn->wOutputOffset,DC_BINNAMES,0,0);
      if(nTrays==DWORD(-1) || nTrays==0) {
         cout<<"Cannot get trays count!"<<endl;
         exit(1);
      }
   
      typedef char trayNameT[24];
      trayNameT* trayName=new char[nTrays][24];
      DeviceCapabilities(LPTSTR(dn)+dn->wDeviceOffset,
         LPTSTR(dn)+dn->wOutputOffset,DC_BINNAMES,LPTSTR(trayName),0);
      LPWORD trayID=new WORD[nTrays];
      DeviceCapabilities(LPTSTR(dn)+dn->wDeviceOffset,
         LPTSTR(dn)+dn->wOutputOffset,DC_BINS,LPTSTR(trayID),0);

      for(DWORD i=0;i<nTrays;i++) {
         cout<<trayID[i]<<' '<<trayName[i]<<endl;
      }
   }
   else {
      // Get tray ID
      DWORD id=strtoul(argv[1],0,0);  
      dm->dmDefaultSource=id;

      HDC hdcPrinter=CreateDC(LPTSTR(dn)+dn->wDriverOffset,
         LPTSTR(dn)+dn->wDeviceOffset,0,dm);

      // Test printing
      TEXTMETRIC tmPrinter; // Printer metricies
      int cxPage, cyPage; // Width and Height of page
      char szText[120]; // Buffer for output
      cxPage=GetDeviceCaps(hdcPrinter, HORZRES);
      SelectObject(hdcPrinter, CreatePen(PS_SOLID, 1, 0));
      DOCINFO di={sizeof(DOCINFO),"Test",NULL};
      int jobId=StartDoc(hdcPrinter,&di);
      StartPage(hdcPrinter);
      Rectangle(hdcPrinter, 0, 0, cxPage, 500);
      SelectObject(hdcPrinter, CreatePen(PS_SOLID, 10, 0));
      Rectangle(hdcPrinter, 2000, 2000, 2600, 2600);
      SelectObject(hdcPrinter, CreatePen(PS_SOLID, 50, 0));
      Rectangle(hdcPrinter, 200, 1000, 300, 1300);
      wsprintf(szText, "Printer Test");
      TextOut(hdcPrinter, 50, (20 * 5), szText, strlen(szText));
      if (EndPage(hdcPrinter) > 0) {
         EndDoc(hdcPrinter);
      }
      DeleteObject(SelectObject(hdcPrinter, GetStockObject(BLACK_PEN)));
      DeleteDC(hdcPrinter);
   }
   GlobalUnlock(pdSetup.hDevNames);
   GlobalUnlock(pdSetup.hDevMode);
}

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:arjunram
ID: 1419203
Crash occurs when doing a createDC. Please let me knwo on any further advances on this issue. I dire need. If you are close to the fix please let me know.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1419204
This code works fine.
At least, I've told you what to do.
Thank you for polite answer.

0
 
LVL 15

Accepted Solution

by:
NickRepin earned 400 total points
ID: 1419205
arjunram, you've asked also the Q about crash while using StretchDIBits. It seems that all crashes in your PC.
May be, the problem is in your code?

Anyway, I try to post the answer again. I hope you will be more kind to me. I've wasted a half of hour to write this example to you. Did you try it alone, I mean, without pasting to your app?
0
 

Author Comment

by:arjunram
ID: 1419206
Hi,
     None of the windows applications work(print to a different tray) if the printer is a network printer. Looks like there is a problem in the setting. ThanX a lot for the sample code.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1419207
I've checked this code at network printer before post it here.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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