我的任务是通过热敏打印机打印标签。为此目的,生成一串tspl(打印机理解的编程语言)命令并将其发送到打印机。后者是在C#中完成的,其中以下方法取自microsoft示例"How to send raw data to a printer by using 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;
}
在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);
}
}
在我的工作环境中一切正常。环境是由3台计算机和1台共享热敏打印机组成的LAN:
从PC_aware我通过rdp连接到PC_unaware,打印机显示在重定向的打印机列表中。然后我运行我的应用程序,选择重定向的打印机并打印我的空标签。到目前为止一切顺利。
当我用另一台计算机替换PC_unaware时出现问题。它在Windows Server 2008 R2上运行,不在我们的LAN中。我们称之为SERVER。所以我进行了同样的实验:
我已经检查了SendBytesToPrinter方法(OpenPrinter,StartDocPrinter,StartPagePrinter,WritePrinter,EndPagePrinter,EndDocPrinter,ClosePrinter)中使用的所有winapi函数的返回值,并且它们都没有指示错误。知道它为什么会发生以及如何修复它?
答案 0 :(得分:0)
file