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;
}