473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Printing to Remote Printers from windows service

I have a program written in C++ we are converting to C#. It is a Windows
Service, running on an application server farm which takes an asynchronus RPC
request from a user (through a bunch of different user applications),
generates a complicated Text report, and pushes it to a Laser printer of the
user's choosing (the path is passed in to the app). The printer is never
installed on the Application Server. I simply open a file on the print
share, write the text to the file, and close the file. The closing of the
file triggers to printer to print. The printer uses the default fonts and
prints the text with basic text formatting (spaces and tabs).

Is there any way to do this in C#? I haven't found any reference to it on
line, although maybe I'm making it more complicated than it needs to be. Has
anyone else ever done this in C++? Have you had any problems with it? We've
heard that what we are doing is actually only working because of a security
hole in Windows, but it has been working for 4 years now, and generally works
without an problems (unless the permissions on the share prevent it)

The C++ code is listed below.

bool CJSMessage::OutputToLaser()
{
// Only output if there is text to output
if (m_csOutputText.GetLength() > 0)
{
HANDLE hHandle = NULL;
long lpNumberOfBytesWritten;

DWORD dwError;

// Open the file on the Laser Printer Share
CString csNetworkPath = m_cWXPrinter.sNetworkPath;

csNetworkPath.Replace("\"","");

if ((hHandle = CreateFile((LPCTSTR)csNetworkPath, GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();

Log("Error %d opening file on device %s\n", dwError,
csNetworkPath);
return FALSE;
}

m_csOutputText.Replace("\n", "\r\n");

if (!WriteFile(hHandle, m_csOutputText, m_csOutputText.GetLength(),
(unsigned long *)&lpNumberOfBytesWritten, NULL))
{
dwError = GetLastError();
Log("Error %d writing to device %s\n", dwError, csNetworkPath);
return FALSE;
}

// Close the output handle... this will trigger the print job.
if ((hHandle != NULL) &&
(hHandle != INVALID_HANDLE_VALUE))
{
CloseHandle(hHandle);
}
}

return true;
}

Nov 22 '05 #1
0 1255

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Brian Hanson | last post by:
Hi, I am trying to programmatically print a pdf file via an asp.net application. I have seen other postings that mention the following code used or something similar to it in a vb.net app. ...
11
by: Brian Hanson | last post by:
Hi, I am trying to programmatically print a pdf file via an asp.net application. I have seen other postings that mention the following code used or something similar to it in a vb.net app. ...
2
by: ThunderMusic | last post by:
Hi, I must print a PDF report from a windows service (so without human action). The pdf is located in a specific place on hard drive that is accessible to the application. Until now, I've only...
0
by: Linda Liu[MSFT] | last post by:
Hi Al, Based on my understanding, you have an application which prints something to printers. The problem is that the font of "OCR A Extended" cannot be printed properly on some of printers. If...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.