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

Posted on 1999-01-24
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++.
Question by:arjunram
  • 4
  • 3

Author Comment

ID: 1419200
Adjusted points to 200

Expert Comment

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

LVL 15

Expert Comment

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_#]


void GetDefPrinter() {

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

   // 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,
      if(nTrays==DWORD(-1) || nTrays==0) {
         cout<<"Cannot get trays count!"<<endl;
      typedef char trayNameT[24];
      trayNameT* trayName=new char[nTrays][24];
      LPWORD trayID=new WORD[nTrays];

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

      HDC hdcPrinter=CreateDC(LPTSTR(dn)+dn->wDriverOffset,

      // 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);
      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) {
      DeleteObject(SelectObject(hdcPrinter, GetStockObject(BLACK_PEN)));

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

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.
LVL 15

Expert Comment

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

LVL 15

Accepted Solution

NickRepin earned 200 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?

Author Comment

ID: 1419206
     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.
LVL 15

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Fox Pro commands 15 50
SQL to ElasticSearch Query 1 702
Visual Studio 2015 locks debug executable 9 214
Need a complete list of ASP.NET page load events 3 59
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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.…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

860 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