74645

Print labels via a redirected printer

Question:

My task is to print labels via a thermal printer. To this purpose a string of tspl(programming language that the printer understands) commands is generated and sent to the printer. The latter is done in C# with the following method taken from the microsoft example <a href="http://support.microsoft.com/en-us/kb/322091" rel="nofollow">"How to send raw data to a printer by using Visual C# .NET"</a>

public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; // Assume failure unless you specifically succeed. di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW"; // Open the printer. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { // Start a document. if (StartDocPrinter(hPrinter, 1, di)) { // Start a page. if (StartPagePrinter(hPrinter)) { // Write your bytes. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } // If you did not succeed, GetLastError may give more information // about why not. if (bSuccess == false) { dwError = Marshal.GetLastWin32Error(); MessageBox.Show("Error " + dwError); } return bSuccess; }

The actual printer is selected in a PrintDialog

private void button2_Click(object sender, EventArgs e) { string s = "CLS\nPRINT 1\n"; // device-dependent string, need a FormFeed? // Allow the user to select a printer. PrintDialog pd = new PrintDialog(); pd.PrinterSettings = new PrinterSettings(); if (DialogResult.OK == pd.ShowDialog(this)) { // Send a printer-specific to the printer. RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s); } }

In my working environment everything works fine. The environment is a LAN consisting of 3 computers and 1 shared thermal printer:

<ol><li><strong><em>PC_with_printer</em></strong> - it runs on Windows 7 and has the printer connected to it via USB</li> <li><strong><em>PC_aware</em></strong> - it runs on Win 8 and knows about the shared printer</li> <li><strong><em>PC_unaware</em></strong> - it runs on WinXP and doens't have the shared printer installed</li> </ol>

From PC_aware I connect to PC_unaware via rdp and the printer appears in the list of printers as redirected. Then I run my application , choose the redirected printer and get my empty label printed. So far so good.<br /> Problems begin when I replace PC_unaware with another computer. It runs on Windows Server 2008 R2 and is not in our LAN. Let's call it SERVER. So I carry out the same experiment:

<ol><li>From PC_aware I connect to SERVER via rdp using its public ip address</li> <li>My thermal printer appears in "Printers and Devices" as "TSC_TDP-244 (redirected 20)"</li> <li>I go to the printer's properties and click Print Test Page, and it gets printed</li> <li>I run my app and the printer doesn't print anything.</li> </ol>

I have checked return values of all winapi functions that are used in SendBytesToPrinter method (OpenPrinter,StartDocPrinter, StartPagePrinter, WritePrinter,EndPagePrinter,EndDocPrinter, ClosePrinter), and none of them indicates an error. Any idea why it happens and how it may be fixed?

Answer1:

PRINTER_DEFAULTS pd; pd.DesiredAccess = PRINTER_ACCESS_USE; pd.pDatatype = NULL; pd.pDevMode = NULL; if (!OpenPrinter(szPrinterName, &hPrinter, &pd)) return false;

Recommend

  • How can I use SetJob in win32print?
  • RawPrinterHelper not printing and not raising error?
  • StretchDIBits failed, sometimes it draw nothing,
  • GetAltTabInfo usage?
  • Duplicated password validation messages ONLY IF new password is 1 char long
  • eC (Ecere) how to not worry about private data fields of a class
  • Window screenshot using WinAPI
  • What components I need to create VS 2017 offline layout for UWP development?
  • How do I know what reference to include to import a specific .NET namespace?
  • Error Installing Windows 10 development tools for Visual Studio 2015 RC on Windows 8.1
  • Hibernate - Cannot connect to DB
  • why fullscreen functionality is not working in chrome?
  • How can I make a 100% stacked chart with nvd3?
  • openssl handshake failed
  • Launch powershell script from UWP app with FullTrustProcessLauncher class
  • jinja2 template not found and internal server error
  • iOS 6 dateFromString returns wrong date
  • Create a link to a web page that runs a Javascript function on the page
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Compress a file with GZipStream while maintaining its meta-data
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How can I include If-None-Match header in HttpRequestMessage
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • Parse a date string in a specific locale (not timezone!)
  • copying resource to sdcard gives a damaged file in android
  • Spark fat jar to run multiple versions on YARN
  • Email format validation in mvc3 view
  • Display issues when we change from one jquery mobile page to another in firefox
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Optimizing database types to compact database (SQLite)
  • How to add date and time under each post in guestbook in google app engine
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • Do I've to free mysql result after storing it?
  • A cron job substitute?
  • Buffer size for converting unsigned long to string
  • coudnt use logback because of log4j
  • Is it possible to post an object from jquery to bottle.py?
  • reshape alternating columns in less time and using less memory