473,399 Members | 2,159 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,399 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 1256

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.