[英]Print labels via a redirected printer
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. 为此,将生成一串tspl(打印机可以理解的编程语言)命令,并将其发送到打印机。 The latter is done in C# with the following method taken from the microsoft example "How to send raw data to a printer by using Visual C# .NET" 后者是在C#中完成的,方法是从Microsoft示例“如何使用Visual C#.NET将原始数据发送到打印机”中采用以下方法
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 在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: 环境是由3台计算机和1台共享热敏打印机组成的LAN:
From PC_aware I connect to PC_unaware via rdp and the printer appears in the list of printers as redirected. 从PC_aware,我通过rdp连接到PC_unaware,并且打印机以重定向的形式出现在打印机列表中。 Then I run my application , choose the redirected printer and get my empty label printed. 然后运行我的应用程序,选择重定向的打印机并打印我的空标签。 So far so good. 到现在为止还挺好。
Problems begin when I replace PC_unaware with another computer. 当我用另一台计算机替换PC_unaware时,问题就开始了。 It runs on Windows Server 2008 R2 and is not in our LAN. 它运行在Windows Server 2008 R2上,不在我们的LAN中。 Let's call it SERVER. 我们称它为SERVER。 So I carry out the same experiment: 因此,我进行了相同的实验:
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. 我已经检查了SendBytesToPrinter方法(OpenPrinter,StartDocPrinter,StartPagePrinter,WritePrinter,EndPagePrinter,EndDocPrinter,ClosePrinter)中使用的所有winapi函数的返回值,但没有一个指示错误。 Any idea why it happens and how it may be fixed? 知道为什么会发生以及如何解决?
PRINTER_DEFAULTS pd;
pd.DesiredAccess = PRINTER_ACCESS_USE;
pd.pDatatype = NULL;
pd.pDevMode = NULL;
if (!OpenPrinter(szPrinterName, &hPrinter, &pd))
return false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.